简体   繁体   English

在 Java 套接字中使用 ObjectInputStream 不读取任何内容

[英]does not read anything using ObjectInputStream in Java socket

I created a simple server and a client, but the server could not read anything that was sent from the client.我创建了一个简单的服务器和一个客户端,但服务器无法读取从客户端发送的任何内容。 I also add a print statement after I sent the string, but it cannot be printed either.我还在发送字符串后添加了一个打印语句,但它也无法打印。

public class Server {
                public static void main(String[] args) throws IOException, ClassNotFoundException {
                    ServerSocket serverSocket = new ServerSocket(6666);

                    Socket clientSocket = serverSocket.accept();
                    System.out.println("accepting client at address " + clientSocket.getRemoteSocketAddress());
                    ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
                    ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());

                    String input = (String) in.readObject();
                    System.out.println(input);
                    out.writeObject("Received");
                    out.flush();
                }
}

Below is the client, and I just want to send a string "?????does not send":下面是客户端,我只想发送一个字符串“?????不发送”:

 public class Test {

        public static void main(String[] args) throws IOException, ClassNotFoundException {
            Client client = new Client();
            client.sentInfo();
        }

        private static class Client {

            private ObjectInputStream objectInputStream;
            private ObjectOutputStream objectOutputStream;

            public Client() throws IOException {
                Socket socket = new Socket("127.0.0.1", 6666);
                this.objectInputStream = new ObjectInputStream(socket.getInputStream());
                this.objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            }

            public void sentInfo() throws IOException, ClassNotFoundException {
                this.objectOutputStream.writeObject("?????does not send");
                this.objectOutputStream.flush();
                System.out.println("????????");
                Message resp = (Message) this.objectInputStream.readObject();
                System.out.println(resp.getMessage());
            }

        }
 }

I tried something else, if I just use InputStream and use a buffer to read bytes, like this: Server code我尝试了别的东西,如果我只是使用 InputStream 并使用缓冲区来读取字节,就像这样:服务器代码

This is the client code: client code这是客户端代码:客户端代码

The code in the two link above would work.上面两个链接中的代码可以工作。 However, it would not work if I tried to use ObjectInputStream:但是,如果我尝试使用 ObjectInputStream,它将不起作用:

This is the server: server这是服务器:服务器

This is the client: client这是客户:客户

This is the Message object I want to send: Message class这是我要发送的消息对象:消息类

Can someone explain this for me please?有人可以为我解释一下吗? Thanks!谢谢!

To read Strings from a socket use something like this:要从套接字读取字符串,请使用以下内容:

DataInputStream input = new DataInputStream(clientSocket.getInputStream()); String message = input.readUTF();

You can open multiple streams from a socket, so if you want to read something else that really needs the ObjectInputStream than it can be open as well.你可以从一个套接字打开多个流,所以如果你想读取其他真正需要 ObjectInputStream 的东西,那么它也可以打开。 Don't forget to properly close the streams & sockets.不要忘记正确关闭流和套接字。

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

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