通过对等方重置连接:套接字写入错误在发送对象时

时间:2016-06-08 12:56:58

标签: java serialization server

所以我通过对等方式重置连接:套接字写入错误,同时发送对象,我不知道为什么。这是我大学项目的一小部分,我花了很多时间来使它工作,但我真的没有想法。我会非常感谢你的帮助。 我试图向客户端发送一个简单的字符串消息,它工作正常。

服务器代码:

public class Server {
static ServerSocket serverSocket;

public static void main(String args[]) {
    try {
    serverSocket = new ServerSocket(7777);
    Map map = new Map();
    while(true)
    {
        Socket incoming  = serverSocket.accept();
        System.out.println(incoming.toString());
        Runnable r = new ServerConnection(incoming, map);
        Thread t = new Thread(r);
        t.start();
    }       
  }
}

ServerConnection代码:

public class ServerConnection implements Runnable {
private Socket s;
private ObjectOutputStream out;
private Map map;
public ServerConnection(Socket out, Map map){
    this.s = out;
    this.map = map;
    System.out.println(map);
}

public void sendMap(){
    //map.setRotation(rotated);
    try {
        out = new ObjectOutputStream(s.getOutputStream());
        System.out.println(out);
        System.out.println(this.map);
        out.writeObject(this.map);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void closeSocket(){
    try {
        s.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void run() {
    //this.connect();
    this.sendMap();
}
}

print stacktrace提供了以下信息:

java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)`
at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Server.ServerConnection.sendMap(ServerConnection.java:39)
at Server.ServerConnection.run(ServerConnection.java:59)
at java.lang.Thread.run(Unknown Source)

是的,我检查了Map是否为空并且输出是否存在等等。我不知道触发问题的原因。

编辑: - 对象图是可序列化的。

客户端控制台输出:

Exception in thread "main" java.lang.NullPointerException
    at Client.ClientConnection.getMap(ClientConnection.java:37)
    at Client.Client.main(Client.java:28)

客户代码:

public static void main(String[] args) {
    ClientConnection c = new ClientConnection();
    c.connect();
    Map map = c.getMap();
    System.out.println(map);
    c.closeSocket();
}

ClientConnection: - serverString是本地地址" 127.0.0.1"

public void connect(){
        try
        {
            Socket s = new Socket(serverString,7777);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

    public Map getMap(){
    Map map;
    try {
        input = new ObjectInputStream(s.getInputStream());
        map = (Map)(input.readObject());
        return map;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
    }
public void closeSocket(){
        try {
            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:1)

通过对等方重置连接意味着另一方关闭了连接。由于您正在显示服务器端程序,因此客户端必须是关闭它的人。检查客户端代码和控制台输出。

编辑:您没有将s赋给类字段,因此在调用getMap时它始终为null。

更改为:

Socket s = new Socket(serverString,7777);
this.s = s;