按照老师的代码写在本地运行报错的问题
报错内容 15:39:54: Executing task 'UdpServer.main()'... Executing tasks: [UdpServer.main()] in project F:\AndoridProject\Socket\Socket_https FAILURE: Build failed with an exception. * Where: Initialization script 'C:\Users\31851\AppData\Local\Temp\UdpServer_main__.gradle' line: 20 * What went wrong: A problem occurred configuring project ':app'. > Could not create task ':app:UdpServer.main()'. > SourceSet with name 'main' not found. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings BUILD FAILED in 1s 15:39:57: Task execution finished 'UdpServer.main()'.
服务端代码:
package com.example.socket_https.udp; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Scanner; /** * @author 31851 */ public class UdpServer { private InetAddress mInetAddres; private int mPort = 7777; private DatagramSocket mSocket; private Scanner mScanner; public UdpServer(){ try { //获取本地地址 mInetAddres = InetAddress.getLocalHost(); mSocket = new DatagramSocket(mPort,mInetAddres); //System.out.println("服务端发消息:"); mScanner = new Scanner(System.in); mScanner.useDelimiter("\n"); } catch (UnknownHostException | SocketException e) { e.printStackTrace(); } } public void start(){ while (true){ byte[] buf = new byte[1024]; try { //内容包 DatagramPacket receivedPacket = new DatagramPacket(buf, buf.length); //receive会阻塞 //从该DatagramSocket中接收数据报 mSocket.receive(receivedPacket); //获取客户端的地址和端口 InetAddress mAddress = receivedPacket.getAddress(); int port = receivedPacket.getPort(); //获取内容 String clientMsg = new String(receivedPacket.getData(), 0, receivedPacket.getLength()); System.out.println("客户端address:" + mAddress + " Port:" + port + " 客户端Message:" + clientMsg); //返回数据 String returnedMsg = mScanner.nextLine(); byte[] returnedMsgBytes = returnedMsg.getBytes(); //封装返回的数据包 DatagramPacket sentPacket = new DatagramPacket(returnedMsgBytes, returnedMsgBytes.length, receivedPacket.getSocketAddress()); mSocket.send(sentPacket); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { new UdpServer().start(); } }
客户端代码:
package com.example.socket_https.udp; import android.net.InetAddresses; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Scanner; public class UdpClient { private String mServerIp = "192.168.1.4"; private InetAddress mServerAddress; private int mServerPort = 7777; private DatagramSocket mSocket; private String clientMessage = ""; private Scanner mScanner; public UdpClient(){ try { mSocket = new DatagramSocket(); mServerAddress = InetAddress.getByName(mServerIp); System.out.println("客户端发消息:"); mScanner = new Scanner(System.in); mScanner.useDelimiter("\n"); } catch (SocketException | UnknownHostException e) { e.printStackTrace(); } } public void start(){ while (true){ String returnedMsg = mScanner.nextLine(); //将需要发送的String转为bytes byte[] returnedMsgBytes = returnedMsg.getBytes(); try { DatagramPacket clientPacket = new DatagramPacket(returnedMsgBytes, returnedMsgBytes.length, mServerAddress, mServerPort); //Sent数据 mSocket.send(clientPacket); byte[] buf = new byte[1024]; DatagramPacket mMsgPacket = new DatagramPacket(buf, buf.length); mSocket.receive(mMsgPacket); String clientMsg = new String(mMsgPacket.getData(), 0, mMsgPacket.getLength()); System.out.println("服务端address:" + mSocket.getLocalSocketAddress() + " 服务端 Message:" + clientMsg); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { new UdpClient().start(); } }
20
收起
正在回答
1回答
只要,你的IP写的没错,老师亲测没有问题。应该是你环境的问题吧,你可以在main中只增加System.out.println("hello")这一行试试。或者就是运行的问题
要在main的左侧点这个运行按钮噢。祝:学习愉快
3.Android 高级应用与Kotlin综合实战
- 参与学习 人
- 提交作业 116 份
- 解答问题 1012 个
本阶段是提升项目经验的必备,除Android开发的高级控件,还有Android官方大力推荐的开发语言Kotlin,未来Android发展的方向,最后使用Kotlin来开发热门电商项目。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星