简体   繁体   English

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

[英]Tcp Socket: readine hangs if used in while loop at Client : java

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. 我在java中尝试一个简单的服务器客户端TCP套接字。 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. 我在客户端使用while((str = in.readLine())!= null)并在读取响应后挂起。 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. 我尝试过\\ n,\\ r \\ n的组合,但readLine没有检测到行尾。
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();

          } 
     }
}

You have a deadlock : 你有一个僵局:

  1. client sends login 客户端发送登录
  2. server reads login line and sends 200 OK, and waits for the next line 服务器读取登录行并发送200 OK,然后等待下一行
  3. client reads 200 OK 客户端读取200 OK
  4. client sends servershare 客户端发送服务器共享
  5. server reads servershare line, sends 200 OK, and waits for the next line 服务器读取服务器共享行,发送200 OK,然后等待下一行
  6. client reads 200 OK, and waits for the next line 客户端读取200 OK,然后等待下一行

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. 您应该使用输入/输出流编写器包装流,并在构造它们时指定特定的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM