Dreik
Dreik

Reputation: 140

Connection reset by peer: socket write error While tring to send object

So I'm getting Connection reset by peer: socket write error while tring to send object and I don't know why. This is small part of my university project, I have spent a lot of time tring to make it work but I'm really out of ideas. I would be really greatful for your help. I tried to send a simple string message to client and it works fine.

Server code:

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 code:

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();
}
}

The print stacktrace gives this information :

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)

Yes, I checked if Map is not null and if output exists etc. I don't know what triggers the problem.

Edit: - Object Map is serializable.

Client Console output:

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

Client code:

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

ClientConnection: - serverString is local adres "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();
        }
    }

Upvotes: 0

Views: 22717

Answers (1)

DiogoSantana
DiogoSantana

Reputation: 2434

Connection reset by peer means that the other side closed the connection. Since you are showing the server side program, then the client side must be the one closing it. Check the client side code and console output.

EDIT: you are not assigning s to the class field, so it is always null when getMap is called.

Change to this:

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

Upvotes: 2

Related Questions