-1

I have a basic TCP connection where i send an object from client to server through serialization. But i'm getting an error when performing this simple task, below is the code:

Client:

public client() throws IOException{
    Socket socket = new Socket("127.0.0.1", 4390);
    System.out.println("Client connected with server");
    
    Student student = new Student(1, "jemoi", "lerry");
    
    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    oos.writeObject(student);
}

Server:

public server() throws IOException, ClassNotFoundException{
    ServerSocket serverSocket = new ServerSocket(4390);
    
    System.out.println("Server initialized successfully");
    
    Socket socket = serverSocket.accept();
    
    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    Student student = (Student)ois.readObject();
    
    System.out.println("Object send from client: " + student.getFirstName());
}
    

Error:

Exception in thread "main" java.net.SocketException: Connection reset

The error occurs at line:

Student student = (Student)ois.readObject();
Joni
  • 108,737
  • 14
  • 143
  • 193
syclone
  • 99
  • 13

1 Answers1

0

Found the solution. I forgot to set the close() from ObjectOutputStream in client.

syclone
  • 99
  • 13