我有一個等待客戶端連接的最小服務器,然後他啓動一個線程,將回復給客戶端的答覆,問題是答覆。Java - 套接字 - 凍結
這是服務器的代碼:路過
int port = 1234;
ServerSocket servSock = null;
servSock = new ServerSocket(port);
while (true) {
Socket link = servSock.accept();
serverThread st = new serverThread(link);
st.start();
}
這是線程的run()方法的代碼,該代碼發送答案回一個,SK是插座「鏈接」參數在服務器代碼
public void run() {
String dato = "";
InputStream i = null;
try {
i = sk.getInputStream();
} catch (IOException ex) {
Logger.getLogger(serverThread.class.getName()).log(Level.SEVERE, null, ex);
}
Scanner input = new Scanner(i);
//i receive the data sent
while (input.hasNext()) {
dato += input.nextLine();
}
// then i send a reply
DataOutputStream outputStream=new DataOutputStream(sk.getOutputStream());
outputStream.writeInt(1);
outputStream.close();
客戶端側(只有代碼應該接收來自服務器的應答):
Socket link;
int valid = 0;
String url="localhost";
int port=1234;
link = new Socket(InetAddress.getByName(url), port);
//i've to send some data to the server
PrintWriter output = new PrintWriter(link.getOutputStream(), true);
String a = new String(Base64.encode(mex));
output.println(createXml(tag, a));
output.flush();
//then i need to receive an answer from the server
DataInputStream answerI = new DataInputStream(link.getInputStream());
while(answerI.available()!=0)// but answerI.available() is always equal 0
valid = answerI.readInt();
answerI.close();
output.close();
link.close();
使用此代碼istruction valid = answerI.readInt();未達到。
沒有while循環,客戶端凍結在line:valid = answerI.readInt();
任何人都可以幫助我嗎?
預先感謝您
[什麼是調試器,它如何幫助我診斷問題](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help -me-diagnose-problems) – Raedwald 2016-01-29 03:38:59