【问题标题】:Java SocketServer is accepting input from a Socket client but the Socket client is not receiving input from the SocketServerJava SocketServer 正在接受来自 Socket 客户端的输入,但 Socket 客户端没有从 SocketServer 接收输入
【发布时间】:2016-08-31 17:53:00
【问题描述】:

我正在尝试学习 Java 网络编程,但遇到了一些障碍。我已经编写了服务器和客户端,但是每次尝试连接它们时,我都会立即收到连接关闭错误。然后,我尝试对其进行编辑,但现在出现连接被拒绝错误。放弃这一点,我决定在一个非常简单的服务器上工作,以测试 Sockets 和 ServerSockets 的基础知识。这样做,我想出了这两个类:

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

public class SimpleServer {
    public static void main(String[] args) throws Exception {
        System.out.println("hey there");
        ServerSocket server = new ServerSocket(50010);
        Socket socket = server.accept();
        System.out.println("Connection at " + socket);

        InputStream in = socket.getInputStream();
        int c = 0;
        while ((c = in.read()) != -1) {
            System.out.print((char)c);
        }


        OutputStream out = socket.getOutputStream();
        for (byte b : (new String("Thanks for connecting!")).getBytes()) {
            out.write(b);
            out.flush();
        }

        in.close();
        out.close();
        socket.close();
        server.close();
    }
}

import java.net.Socket;
import java.io.*;

public class SimpleClient {
    public static void main(String[] args) throws Exception {
        System.out.println("Attempting connection");
        Socket s = new Socket("130.49.89.208", 50010);
        System.out.println("Cool");

        OutputStream out = s.getOutputStream();
        for (byte b : (new String("Hey server\r\nThis message is from the client\r\nEnd of message\r\n")).getBytes()) {
            out.write(b);
            out.flush();
        }

        InputStream in = s.getInputStream();
        int c = 0;
        System.out.println("this message will print");
        while ((c = in.read()) != -1) {
            System.out.print((char)c);
            System.out.println("this does not print");
        }

        out.close();
        in.close();
        s.close();
    }
}

服务器完美地接收到客户端的消息,但是当轮到服务器写入客户端时,一切都阻塞了。

服务器的输出:

-java SimpleServer 
----hey there 
----Connection at Socket[addr=/130.49.89.208,port=59136,localport=50010]
----Hey server
----This message is from the client
----End of message

客户的输出:

-java SimpleClient
----Attempting connection
----Cool
----this message will print

客户端和服务器都在我的笔记本电脑上运行,通过以太网连接到大学的互联网连接,如果有帮助的话。

【问题讨论】:

    标签: java sockets network-programming inputstream blocking


    【解决方案1】:

    根据 Javadocs,InputStream.read() 被描述为:

    如果由于到达流的末尾而没有可用的字节,则返回值 -1。此方法阻塞,直到输入数据可用、检测到流结束或抛出异常

    在您的情况下,中断 while 循环的唯一可能性是客户端关闭连接,从而导致流结束。

    根据您的编码,这是预期的!

    【讨论】:

      【解决方案2】:

      您的服务器代码卡在这里,因此永远不会写回客户端

         while ((c = in.read()) != -1) {
              System.out.print((char)c);
          }
      

      【讨论】:

        猜你喜欢
        • 2017-07-29
        • 2014-08-29
        • 1970-01-01
        • 2013-12-05
        • 2016-05-06
        • 1970-01-01
        • 2021-06-29
        • 1970-01-01
        • 2014-03-02
        相关资源
        最近更新 更多