我开发了一个简单的视频游戏,我想用多人功能使用websockets进行更新。我想要有两个版本的游戏。第一个用作服务器,另一个用作客户端。我想从服务器初始化游戏并等待客户端的反应。我的第一个问题:是否有可能在同一台机器上运行服务器和客户端(给出相同的IP作为输入)?其次我使用,以便将下面的代码创建一个从服务器端的插口:与java中的服务器和客户端websockets进行通信
try {
serverSocket = new ServerSocket(10007);
}
catch (IOException e)
{
System.err.println("Could not listen on port: 10007.");
System.exit(1);
}
System.out.println ("Waiting for connection.....");
try {
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println ("Connection successful");
当我试图从客户端连接,似乎整个事情它不工作,因为我收到消息waiting for connection...
。我的代码连接客户端如下:
String serverHostname = new String("ip");
if (args.length > 0)
serverHostname = args[0];
System.out.println("Attemping to connect to host " +
serverHostname + " on port 10007.");
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket(serverHostname, 10007);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: " + serverHostname);
System.exit(1);
}
这是什么问题在这里?
你的意思是[WebSocket](https://tools.ietf.org/html/rfc6455)? –