未收到套接字输出

时间:2016-09-02 19:50:12

标签: java sockets tcp network-programming

我有一个Server和一个Client类,Server和Client之间互相交换消息。代码是相当愚蠢的,用于测试目的,它应该工作的方式如下:当聚会(客户端/服务器)发送消息时,它接收到目前为止另一方发送的所有消息(直到InputStream是空的)。实现此功能的代码片段位于客户端内部,而服务器代码用于循环:

客户:

 public class ClientSocketRunner {
    private static String ip="localhost";
    private static int port=440;
    private static String client="Client";
    private static String server="Server";

    public static void main(String[] args) throws Exception 
    {
        ClientSocketRunner socket_runner=new ClientSocketRunner();
        socket_runner.run();
    }

    private void run() throws Exception
    {
         Socket client_socket=new Socket(ip, port);
        OutputStream os=client_socket.getOutputStream();
        InputStream is=client_socket.getInputStream();

        PrintStream make_output=new PrintStream(os);
        BufferedReader read_input=new BufferedReader(new                      
         InputStreamReader(is));

        make_output.println("Hello, I'm the Client");
        System.out.println(read_input.readLine());
        System.out.println("my port: "+client_socket.getLocalPort());

       String line=null;
       BufferedReader user_input=new BufferedReader(new 
          InputStreamReader(System.in));
       while (!(line=user_input.readLine()).equals("-1"))
       {
              make_output.println(client+": "+line);
              make_output.flush();
              System.out.println("sent");
               while (is.available()>0)
        {
            System.out.println(read_input.readLine());
        }
              System.out.println("bytes: "+is.available());
       }

        while (is.available()>0)
        {
            System.out.println(read_input.readLine());
        }

    }

服务器:

public class ServerSocketRunner {
    private static int port=440;
    private static String client="Client";
    private static String server="Server";




    public static void main(String[] args) throws Exception
    {
        ServerSocketRunner ssRunner=new ServerSocketRunner();
        ssRunner.run();
    }

    public void run() throws Exception 
    {

        ServerSocket server_socket=new ServerSocket(port);
        Socket socket=server_socket.accept();
        InputStream sin=socket.getInputStream();
        OutputStream sout=socket.getOutputStream();
        BufferedReader read_input=new BufferedReader(new InputStreamReader(sin));
        PrintStream make_output=new PrintStream(sout);



        System.out.println(read_input.readLine());
        make_output.println("Good to meet you, I'm the Server");
        System.out.println("my port: "+socket.getLocalPort());

        String line;
        BufferedReader user_input=new BufferedReader(new InputStreamReader(System.in));



        for (line=user_input.readLine(); !(line.equals("-1")); line=user_input.readLine())
            {
               make_output.println(server+": "+line);
               make_output.flush();
               System.out.println("sent");
               while (sin.available()>0)
               {
                  System.out.println(read_input.readLine());
               }
            }

        }

    }

但它并没有像预期的那样发挥作用。当我进行多行输入时,它会为每一行打印出来("发送")但是当我去另一方时,发送一些预期输入的行不会显示出来。直到各方之间的下一次互动之后...... 有什么问题?

1 个答案:

答案 0 :(得分:0)

实际上这段代码按预期运行

但是如果你想要,例如,服务器立即显示客户端的输入使用这个 代码:

public class ServerSocketRunner {
    private static int port=440;
    private static String client="Client";
    private static String server="Server";

public static void main(String[] args) throws Exception
{
    ServerSocketRunner ssRunner=new ServerSocketRunner();
    ssRunner.run();
}

public void run() throws Exception 
{

    ServerSocket server_socket=new ServerSocket(port);
    Socket socket=server_socket.accept();
    InputStream sin=socket.getInputStream();
    OutputStream sout=socket.getOutputStream();
    BufferedReader read_input=new BufferedReader(new InputStreamReader(sin));
    PrintStream make_output=new PrintStream(sout);

    System.out.println(read_input.readLine());
    make_output.println("Good to meet you, I'm the Server");
    System.out.println("my port: "+socket.getLocalPort());

    String line;
    BufferedReader user_input=new BufferedReader(new InputStreamReader(System.in));

    // here starts updated code

    while(true)
    {
        if (user_input.ready())
        {
           line=user_input.readLine();

           if(line.equals("-1")) {
             break;
           }
           make_output.println(server+": "+line);
           make_output.flush();
           System.out.println("sent");
        }

         while (sin.available()>0)
         {
            System.out.println(read_input.readLine());
         }
        Thread.sleep(500);
    }
  }
}


public class ClientSocketRunner {
    private static String ip="localhost";
    private static int port=1440;
    private static String client="Client";
    private static String server="Server";

    public static void main(String[] args) throws Exception 
    {
        ClientSocketRunner socket_runner=new ClientSocketRunner();
        socket_runner.run();
    }

    private void run() throws Exception
    {
         Socket client_socket=new Socket(ip, port);
        OutputStream os=client_socket.getOutputStream();
        InputStream is=client_socket.getInputStream();

        PrintStream make_output=new PrintStream(os);
        BufferedReader read_input=new BufferedReader(new InputStreamReader(is));

        make_output.println("Hello, I'm the Client");
        System.out.println(read_input.readLine());
        System.out.println("my port: "+client_socket.getLocalPort());

       String line=null;
       BufferedReader user_input=new BufferedReader(new InputStreamReader(System.in));
       // here starts updated code

        while(true)
        {
            if (user_input.ready())
            {
               line=user_input.readLine();

               if(line.equals("-1")) {
                 break;
               }
               make_output.println(server+": "+line);
               make_output.flush();
               System.out.println("sent");
            }

             while (is.available()>0)
             {
                System.out.println(read_input.readLine());
             }
            Thread.sleep(500);
        }
    }
}