The client end is continuing to crash with the error "java.net.SocketException: Connection reset", and unfortunately I have no clue what exactly is causing it.
This client / server pair are sharing a Serialized object between them ...
Shared class
import java.io.Serializable;
public class Test implements Serializable{
int a;
String b;
public Test(int i, String s){
a = i;
b = s;
}
public void say(){
System.out.println(a + " " + b);
}
}
Client
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class ChatClient {
public static void main (String[] x){
Boolean b = true;
try {
Socket socket = new Socket("127.0.0.1", 9999);
ObjectInputStream in = null;
ObjectOutputStream out = null;
while (b){
System.out.println(" <Client> ");
if (out == null) {
out = new ObjectOutputStream(socket.getOutputStream());
}
out.writeObject(new Test(11,"Client") );
out.flush();
if (in == null) {
in = new ObjectInputStream(socket.getInputStream() );
}
Test tst = (Test) in.readObject();
tst.say();
}
}
catch (Exception e) {
System.out.println("Client Error: \n" + e);
b = false;
}
}
}
Server
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer {
public static void main (String[] x){
Boolean b = true;
try {
ServerSocket serversocket = new ServerSocket(9999);
Socket socket = serversocket.accept();
ObjectInputStream in = null;
ObjectOutputStream out = null;
while (b){
System.out.println(" <Server> ");
if (in == null) {
in = new ObjectInputStream(socket.getInputStream() );
}
Test tst = (Test) in.readObject();
tst.say();
if (out == null) {
out = new ObjectOutputStream(socket.getOutputStream());
}
out.writeObject(new Test(22,"Server") );
out.flush();
}
}
catch (Exception e) {
System.out.println("Server Error \n" + e);
b = false;
}
}
}