2013-04-15 71 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

我建议你在题目“编辑但不工作”的标题下添加一个部分,以便你的原始代码保持在那里,我们可以看到新代码。 –