0
我想用套接字讀寫byte[]
數據,但無法停止數據流。如何停止DataInputStream get byte []?
這是我的服務器:
public class Server {
public static void main(String[] args) throws IOException {
System.out.println("Welcome to Server side");
DataInputStream in;
DataOutputStream out;
ServerSocket servers = null;
Socket fromclient = null;
// create server socket
try {
servers = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Couldn't listen to port 4444");
System.exit(-1);
}
try {
System.out.print("Waiting for a client...");
fromclient = servers.accept();
System.out.println("Client connected");
} catch (IOException e) {
System.out.println("Can't accept");
System.exit(-1);
}
in = new DataInputStream(fromclient.getInputStream());
out = new DataOutputStream(fromclient.getOutputStream());
String input = "", output;
System.out.println("Wait for messages");
byte[] bytes = new byte[16*1024];
int count;
while ((count = in.read(bytes)) > 0) {
byte[] mass = "some data".getBytes("UTF-8");
out.write(mass, 0, count);
System.out.println(Arrays.toString(bytes));
}
out.close();
in.close();
fromclient.close();
servers.close();
}
}
當我從客戶端接收數據,它會打開一個無限流不停止。
我該如何阻止這個DataInputStream
?
「我無法停止流」不是一個問題描述。再試一次。 NB'System.out.println(Arrays.toString(bytes));'應該是'System.out.println(Arrays.toString(bytes,0,count));', – EJP
「System.out.println(Arrays。 toString(bytes,0,count));「顯示錯誤參數 – Pavel