我正在做一個tcp客戶端服務器聊天程序。我的服務器是通過螺紋製成,其代碼如下:錯誤使用線程java的TCP客戶端服務器聊天程序
System.out.println("Server binded at "+((client.getInetAddress()).getLocalHost()).getHostAddress()+":9867");
System.out.println("Run the Client");
//ready to accept client request
//opening the input stream to read data from client connection
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
//using output stream responsing data
out=new PrintStream(client.getOutputStream());
out.print("Welcome by server\n");
System.out.println("1");
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
while(! in.readLine().trim().equals("*")) {
//using output stream responsing data
System.out.println("3");
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
str = bufferRead.readLine();
out.println(str);
out.flush();
//opening the input stream to read data from client connection
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
}
我的客戶端文件是:(簡單不帶螺紋)
client =new Socket("127.0.0.1", 9867);
System.out.println("Client connected ");
//getting the o/p stream of that connection
out=new PrintStream(client.getOutputStream());
//sending the message to server
out.print("Hello from client\n");
//reading the response using input stream
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
bufferRead = new BufferedReader(new InputStreamReader(System.in));
str = bufferRead.readLine();
out=new PrintStream(client.getOutputStream());
out.print(str);
out.flush();
while(! in.readLine().trim().equals("*")) {
// opening the input stream to read data from server connection
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println("4");
System.out.println(in.readLine());
System.out.println("5");
out=new PrintStream(client.getOutputStream());
bufferRead = new BufferedReader(new InputStreamReader(System.in));
str = bufferRead.readLine();
out.print(str);
out.flush();
}
現在奇怪的事情發生了我。當我註釋掉兩個文件上的while循環時,程序工作正常。但是,當我取消註釋它時,我的客戶端發送msg到服務器後進入while循環,我的服務器不斷等待響應。
所以輸出是:(客戶端)
Client connected
Welcome by server
l;skc
4
和服務器的是:
Server binded at 192.168.1.242:9867
Run the Client
Hello from client
1
我認爲這是由於使用線程/ asynchoronous process.Plz幫助
編輯代碼
服務器端:
System.out.println("Server binded at "+((client.getInetAddress()).getLocalHost()).getHostAddress()+":9867");
System.out.println("Run the Client");
//ready to accept client request
//opening the input stream to read data from client connection
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
//using output stream responsing data
out=new PrintStream(client.getOutputStream());
out.print("Welcome by server\n");
System.out.println("1");
str = in.readLine();
System.out.println(str);
while(!str.trim().equals("*")) {
//using output stream responsing data
System.out.println("3");
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
str = bufferRead.readLine();
out.println(str);
out.flush();
//opening the input stream to read data from client connection
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
}
客戶端:
客戶=新的Socket( 「127.0.0.1」,9867);
System.out.println("Client connected ");
//getting the o/p stream of that connection
out=new PrintStream(client.getOutputStream());
//sending the message to server
out.print("Hello from client\n");
//reading the response using input stream
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
bufferRead = new BufferedReader(new InputStreamReader(System.in));
str = bufferRead.readLine();
out=new PrintStream(client.getOutputStream());
out.print(str);
out.flush();
while(! str.trim().equals("*")) {
// opening the input stream to read data from server connection
str = in.readLine();
System.out.println("4");
System.out.println(str);
out=new PrintStream(client.getOutputStream());
str = bufferRead.readLine();
out.print(str);
out.flush();
}
爲什麼要在服務器的每個循環中重新打開輸入和輸出流? – KevinDTimm
我查過了......但沒有任何反應......結果是一樣的 – ssaaddii