0
試圖獲取套接字的句柄。服務器和客戶端運行在兩個不同的程序中。Java套接字 - 服務器在客戶端發送響應後掛起
它們似乎彼此很好地連接,但客戶端不會正確地將其輸出發送到服務器。服務器掛起。下面的代碼:
服務器:
private ServerSocket serverSocket;
private Socket client;
public void run() throws Exception {
serverSocket = new ServerSocket(20005);
while(currentState == Game.State.NORMAL) {
client = serverSocket.accept();
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String clientInput = in.readLine();
// Takes the client input string and does some simple game logic that returns a Gson object
Gson serverResponse = processInput(clientInput);
out.write(serverResponse.toString());
out.flush();
}
}
客戶:
插座ClientSocket的;
void run() throws Exception {
clientSocket = new Socket("192.168.0.24", 20005);
PrintWriter out;
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Print the state of the game - returns false if state is win or lose.
while(printState()) {
out = new PrintWriter(clientSocket.getOutputStream(), true);
// This method just takes some input from the console
String clientInput = getInput();
out.write(clientInput);
out.flush();
String serverResponse = in.readLine();
updateState(serverResponse);
}
}
}
有一些潛在的遊戲邏輯正在發生,但它很小,應該是無關緊要的。我想我只是誤解了這裏的根本。
謝謝大家。
哇,我是個白癡。謝謝。 – Slims 2013-02-10 01:25:23