使用ObjectOutputStream发送String数据时获取“java.net.SocketException:Connection reset”

时间:2018-03-21 22:05:22

标签: socketexception objectoutputstream

我正在尝试使用ObjectOutputStream通过TCP发送字符串数据,但我总是得到“java.net.SocketException:Connection reset”。这是我的代码:

客户端:

clientSocket = new Socket("127.0.0.1", 9000);
oo = new ObjectOutputStream(clientSocket.getOutputStream());
oo.writeObject(data);
oo.flush();

服务器:

    ServerSocket server = new ServerSocket(9000);
    Socket client = server.accept();
    ObjectInputStream oi = new ObjectInputStream(client.getInputStream());
    String data = (String) oi.readObject();
    System.out.println(data);

0 个答案:

没有答案