Tcp Socket:如果在客户端的 while 循环中使用,则 readine 会挂起:java

发布于 2024-12-11 11:59:21 字数 3315 浏览 0 评论 0原文

我一直在敲着头试图弄清楚出了什么问题。

我正在尝试用 java 编写一个简单的服务器客户端 TCP 套接字。服务器可以发送多行作为响应。
我在客户端使用 while((str = in.readLine())!= null) ,它在读取响应后挂起。请找到下面的代码。我发帖前已经搜索过。我确保以新行结束响应。

我尝试过 \n、\r\n 的组合,但 readLine 未检测到行尾。
但它在服务器端运行良好。

请帮忙。

谢谢。

服务器:

import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {

    ServerSocket s = new ServerSocket(55555);
    Socket socket = s.accept();

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

    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

    String inputLine= null;
    System.out.println("call processInput"); 

       while ((inputLine = in.readLine()) != null) {
           System.out.println("before call processInput");           
           out.print("200 Success \r\n");
           out.flush();
           System.out.println("after call processInput: ");            
    }
    System.out.println("after while");
    out.close();
    in.close();
    socket.close();
    }
}

客户端:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.CharBuffer;

public class Test {
     public static void main(String[] args) throws IOException {

          try {

              System.out.println(":Connect");
                  Socket s = new Socket("192.168.1.114",55555);
                  System.out.println("Socket:" + s); 


                  System.out.println("after :Connect");



                  OutputStream s1out = s.getOutputStream();


                  PrintWriter output = new PrintWriter(s1out, true);   
                  output.println("login user root");
                  output.flush();


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

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



                  output.println("servershare");
                  output.flush();
                  System.out.println( "servershare");
                  String str = null;


             while((str = in.readLine())!= null) // hangs here
             {
                  System.out.println(str);

              }


               System.out.println( "share");
               output.println("share file1.txt file1.html, roopa ramesh");
                   str = null;
                   while((str = in.readLine())!= null && !str.equals(""))
                  System.out.println(str);

                  in.close();
                  output.close();
                  s.close();







          } catch (UnknownHostException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

          } catch (IOException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

          } 
     }
}

I have been banging my head trying to figure out what is going wrong.

I am trying a simple server client TCP socket in java. Server can send multiple lines as response.
I am using while((str = in.readLine())!= null) at client side and it hangs after reading the response. Please find the code below. I have searched before posting. I am making sure I am ending the response with new line.

I have tried combinations of \n, \r\n but readLine is not detecting the end of line.
But it works fine at server end.

Please help.

Thanks.

SERVER:

import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {

    ServerSocket s = new ServerSocket(55555);
    Socket socket = s.accept();

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

    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

    String inputLine= null;
    System.out.println("call processInput"); 

       while ((inputLine = in.readLine()) != null) {
           System.out.println("before call processInput");           
           out.print("200 Success \r\n");
           out.flush();
           System.out.println("after call processInput: ");            
    }
    System.out.println("after while");
    out.close();
    in.close();
    socket.close();
    }
}

CLIENT:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.CharBuffer;

public class Test {
     public static void main(String[] args) throws IOException {

          try {

              System.out.println(":Connect");
                  Socket s = new Socket("192.168.1.114",55555);
                  System.out.println("Socket:" + s); 


                  System.out.println("after :Connect");



                  OutputStream s1out = s.getOutputStream();


                  PrintWriter output = new PrintWriter(s1out, true);   
                  output.println("login user root");
                  output.flush();


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

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



                  output.println("servershare");
                  output.flush();
                  System.out.println( "servershare");
                  String str = null;


             while((str = in.readLine())!= null) // hangs here
             {
                  System.out.println(str);

              }


               System.out.println( "share");
               output.println("share file1.txt file1.html, roopa ramesh");
                   str = null;
                   while((str = in.readLine())!= null && !str.equals(""))
                  System.out.println(str);

                  in.close();
                  output.close();
                  s.close();







          } catch (UnknownHostException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

          } catch (IOException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

          } 
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-12-18 11:59:21

你有一个死锁:

  1. 客户端发送登录
  2. 服务器读取登录行并发送 200 OK,并等待下一行
  3. 客户端读取 200 OK
  4. 客户端发送 servershare
  5. 服务器读取 servershare 行,发送 200 OK,并等待下一行
  6. 客户端读取 200 OK,并等待下一行

客户端和服务器都在等待另一端的下一行。您的协议不正确。

此外,您在服务器端和客户端使用平台默认编码来读取和写入消息。如果客户端和服务器没有相同的默认编码,就会遇到问题。您应该使用输入/输出流编写器包装流,并在构建它们时指定特定的编码。

You have a deadlock :

  1. client sends login
  2. server reads login line and sends 200 OK, and waits for the next line
  3. client reads 200 OK
  4. client sends servershare
  5. server reads servershare line, sends 200 OK, and waits for the next line
  6. client reads 200 OK, and waits for the next line

Both the client and the server are waiting for the next line from the other end. Your protocol isn't correct.

Moreover, you're using the platform default encoding at server-side and client-side to read and write the messages. If the client and server don't have the same default encoding, you'll have a problem. You should wrap your streams with Input/Output stream writers and specify a specific encoding when constructing them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文