ここにクライアント/サーバーのペア用の(不完全な)コードがいくつかあります。ここにサーバークラスがありますが、何らかの理由でコードがserverSocket.accept()の下で実行を停止しているように見えます
私は何を間違っていますか?ありがとう
class MPTagServer{
public String serverName = "MPTag Server";
public int gSize = 16;
public int maxPlayers = 16;
ServerSocket serverSocket = null;
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
MPTagServer(String sn, int gs, int mp){
serverName = sn;
gSize = gs;
maxPlayers = mp;
}
public void start() throws Exception{
Task serverTask = new Task<Void>(){
@Override protected Void call() throws Exception{
int port = 6789;
try{
serverSocket = new ServerSocket(port);
}
catch(IOException e){
System.err.println("Could not listen on port: " + port);
System.exit(1);
}
try{
System.out.println("This will print");
clientSocket = serverSocket.accept(); //Code won't run below here
System.out.println("This won't print");
}
catch(IOException e){
System.err.println("Accept failed.");
System.exit(1);
}
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
ComProtocol cp = new ComProtocol();
outputLine = cp.init();
out.println(outputLine);
out.close();
in.close();
clientSocket.close();
serverSocket.close();
return null;
}
};
Thread serverThread = new Thread(serverTask);
serverThread.setDaemon(true);
serverThread.start();
}
}