0
我使用ServerSocket在while循環中從客戶端獲取數據,它在第一次運行時工作,但在第二輪後失敗。ServerSocket在循環中沒有收到數據
我做了一些搜索,但仍然無法弄清楚發生了什麼事。
服務器端代碼
package com.gorilla.main;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server2 {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(44444);
while(true){
System.out.println("another round");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
System.out.println("available: "+ inputStream.available());
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
System.out.println(new String(b));
System.out.println("=======================");
socket.close();
}
}
}
克倫特端代碼
package com.gorilla.main;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client2 {
public static void main(String [] args) throws Exception{
Socket socket = new Socket("127.0.0.1", 44444);
String s = "Hello World";
byte [] b = s.getBytes();
socket.getOutputStream().write(b);;
socket.close();
}
}
,並在服務器端控制檯輸出後,我跑了客戶端的3倍。
another round
available: 11
Hello World
=======================
another round
available: 0
=======================
another round
available: 0
=======================
another round
任何建議,將不勝感激。謝謝。
它的工作原理。但是當inputStream.available()爲0時,爲什麼我可以從inputStream讀取一些內容?你有想法嗎? –
並感謝您的回答! –
你的用例不需要'available()'。只需閱讀(這是一個阻塞呼叫)並配置超時 – Raffaele