0

正如我在这个问题中提到的,我得到了一个 SocketException: Connection reset。
实施亚伦的回答后,它似乎按预期工作。但是今天我再次运行代码,我再次收到连接重置错误。
问题是,如果我运行我的代码 5 次,它似乎工作了 2/5 次,其余的给了我错误......

服务器:

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

public class Server {

    public static void main(String[] args) throws IOException, ClassNotFoundException {       
            ServerSocket server = new ServerSocket(444);
            while (true) {
                Socket socket = server.accept();
                ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());
                Object objRead = objIn.readObject();
                if (objRead != null) {
                    System.out.println(objRead);
                }
            }       
    }
}

客户:

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.HashMap;

public class Client {

    public static void main(String[] args) throws IOException {
        Socket sock;
        int port = 444;
        HashMap<Integer, String>  mapSend= new HashMap<>();
        mapSend.put(1,"row1");
        mapSend.put(2,"row2");

        sock = new Socket(InetAddress.getLocalHost(), port);
        ObjectOutputStream objOut = new ObjectOutputStream(sock.getOutputStream());
        
        objOut.writeObject(mapSend);
        objOut.flush();
    }
}

这是错误:(它出现在服务器的输出上,在我运行客户端之后):

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:189)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2308)
    at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2716)
    at java.io.ObjectInputStream$BlockDataInputStream.readFully(ObjectInputStream.java:2740)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1978)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1913)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at java.util.HashMap.readObject(HashMap.java:1154)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1891)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at iotest.Server.main(Server.java:16)

在客户端,我没有收到任何错误。

编辑
我可以调试客户端服务器的任何方式吗?这可能有助于为你们提供更多关于此的信息。

4

1 回答 1

1

关闭输出流,而不是仅仅刷新它。

注意 readObject() 不会返回 null 除非你写 null。测试毫无意义。

于 2013-09-23T08:07:42.903 回答