【问题标题】:Error TCP Client server chat program using thread java使用线程java的错误TCP客户端服务器聊天程序
【发布时间】:2013-04-08 00:20:18
【问题描述】:

我正在制作一个 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 循环时,程序运行良好。但是当我在向服务器发送消息后取消评论时,我的客户端进入了 while 循环,我的服务器继续等待响应。

所以输出是:(客户端)

Client connected 
Welcome by server
l;skc
4

和服务器是:

Server binded at 192.168.1.242:9867
Run the Client
Hello from client
1

我认为这是由于使用了线程/异步进程。请帮助

修改后的代码

服务器端:

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());

                }

客户端:

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(! 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();

            }

【问题讨论】:

  • 为什么要在服务器的每个循环中重新打开输入和输出流?
  • 我检查了...但没有任何反应...结果是一样的

标签: multithreading tcp thread-safety tcpclient tcpserver


【解决方案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);

这意味着你的线程将通过输入流持续监听输入,然后你可以在服务器返回响应后对 str 变量做任何你想做的事情

【讨论】:

    【解决方案2】:

    第一条线索。看看你能用这个做什么:

    这一行:

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

    将从传入连接中读取一行并回显它。当它这样做时,行内容消失了,如果您再次尝试调用“in.readLine()”,它将尝试读取另一行,而不是再次读取同一行。

    此外,这一行:

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

    通常只会出现一次。当你构建阅读器时,你只需要使用它。您不必一遍又一遍地重建它。

    【讨论】:

    • 我建议您在问题底部添加一个标题为“已编辑但仍无法工作”的部分,以便您的原始代码保留在那里,我们可以看到新代码。
    • 你的编辑版本需要根据我给你的线索再编辑一些。这两个提示仍然适用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多