我正在用java编写一个简单的客户端/服务器,我遇到了这个我无法解决的问题。我在客户端和服务器上都使用 DatagramSocket,而我的服务器无法接收任何数据。我没有收到任何错误,但它不起作用。这是我的服务器源代码:public class GameServer { public static final String serverBuild = "0.00 (050319.milestone0-main)"; public static final String protocolBuild = "1"; public DatagramSocket serverSocket; public boolean isRunning = false; public Thread clientHandler; public GameServer(int port, String serverName) { System.out.println("Server> Starting a server on port: " + port + "."); System.out.println("Server> " + serverName + " running on server build " + serverBuild + "."); System.out.println("Server> Using protocol ID: " + protocolBuild + "."); isRunning = true; try { serverSocket = new DatagramSocket(port); }catch(Exception ex) { System.out.print("Server> "); ex.printStackTrace(); } clientHandler(); } public void clientHandler() { clientHandler = new Thread(new Runnable() { public void run() { while(isRunning) { byte[] buffer = new byte[256]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); try { serverSocket.receive(packet); System.out.println("Server> " + new String(packet.getData(), 0, packet.getData().length)); } catch (IOException e) { System.out.print("Server> "); e.printStackTrace(); } } } }); clientHandler.start(); }}客户端非常简单,因为我在寻找为什么我的服务器不工作。
添加回答
举报
0/150
提交
取消