2013-04-15 68 views
0

我正在做一個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(); 

      } 
+0

爲什麼要在服務器的每個循環中重新打開輸入和輸出流? – KevinDTimm

+0

我查過了......但沒有任何反應......結果是一樣的 – ssaaddii

回答

1

爲了您的例子,我會強烈建議敲通過以下鏈接敲協議http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html

對我來說,在代碼中昭然若揭主要錯誤是

while(! str.trim().equals("*")) 

當它應該是

while ((str = in.readLine()) != null) { 
System.out.println("Server: " + str); 

這意味着你的線程將通過輸入流持續監聽輸入,然後你可以做任何你想要的功能h從服務器返回響應後的str變量

1

第一個線索。見你可以用這個做什麼:

這條線:

System.out.println(in.readLine()); 

將讀取從傳入的連接線和回聲它。當它這樣做時,行內容不見了,如果你再次嘗試調用「in.readLine()」,它將嘗試讀取另一行,而不是再次讀取同一行。

此外,這條線:

in= new BufferedReader(new InputStreamReader(client.getInputStream())); 

通常只會出現一次。當你建立閱讀器時,你只需要使用它。你不必一遍又一遍地重建它。

+0

k.thanks。我會試試這個。 – ssaaddii

+0

我改變了我的代碼到你所說的......但同樣的問題仍然存在 – ssaaddii

+0

我建議你在題目「編輯但不工作」的標題下添加一個部分,以便你的原始代碼保持在那裏,我們可以看到新代碼。 –