Simple Socket Server Is Continuing To Crash

Started by
4 comments, last by RLS0812 9 years, 9 months ago

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

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Advertisement

I wasn't able to get the client to fail this way. Everything worked. What are you doing to cause the error?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

After compiling this and running outside of a test environment, the server kicks up the error "ClassNotFound : Client.Test

Re-wrote the code, and received the same error (( both client and server package have a copy of the Test class ))

I than commented out the object, and instead sent a string - and it worked ... Why can't I send serialized objects ?!

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

I found the problem - this has to be the one of the most obscure type of serialization errors that exist in Java !

When sending a serialized object over a socket to a different JVM , the package name has to be the exact same for both the client and the server ( Java 1.7 ) .

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

I found the problem - this has to be the one of the most obscure type of serialization errors that exist in Java !

When sending a serialized object over a socket to a different JVM , the package name has to be the exact same for both the client and the server ( Java 1.7 ) .

How in the world did you figure that out?

And what kind of Java networking stuff are you doing? I've done lots of that stuff if you need any advice.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

I found the problem - this has to be the one of the most obscure type of serialization errors that exist in Java !

When sending a serialized object over a socket to a different JVM , the package name has to be the exact same for both the client and the server ( Java 1.7 ) .

How in the world did you figure that out?

And what kind of Java networking stuff are you doing? I've done lots of that stuff if you need any advice.

I was Goggling many different forum posts about ClassNotFoundException for almost an hour and half - I found the answer deep in a thread about serializing files that are read by several different programs.

As of right now I am attempting to send several different objects across a Client - Server pair ( map updates, player stats, server event messages ). This will be eventually integrated into my pre-existing ASCII rendering engine.

The goals of this will be to have a MUD like virtual world, with up to 200 players at one time.

TODO: multi-threading each user who connects to the server - develop port listeners - develop 2 way communication - multi-thread server/client actions and socket actions

(( Milt-threading will probably be put on the back burner for a long while ))

Here is the current version of the code I have:

Common object:




class Test_Obj implements Serializable {  
int a ;  
String b;  
	public  Test_Obj(int i, String s ){  
			a = i;
			b = s;
	}

}

Client




public class Client {
	public static void main(String args[]) {
	
	try{  
		Socket s = new Socket("127.0.0.1",9999);  
		OutputStream os = s.getOutputStream();  
		ObjectOutputStream oos = new ObjectOutputStream(os);  
		Test_Obj to = new Test_Obj(999,"Hello World !");  
		oos.writeObject(to);  
		oos.flush();
		oos.close();  
		os.close();  
		s.close();  
		}
	catch(Exception e){
		System.out.println("Client: " + e);
		}  
	}  
}

Server



public class Server  {

	public static void main(String args[]) {  
		try {  
		ServerSocket ss = new ServerSocket(9999);  
		Socket s = ss.accept();  
		InputStream is = (InputStream) s.getInputStream(); 
		ObjectInputStream ois = new ObjectInputStream(is);
		Object in = ois.readObject();
		if (in != null){
			if (in instanceof Test_Obj ){
				Test_Obj to = (Test_Obj) in ; 
				System.out.println(to.a + " " + to.b);
			}
			else{
				System.out.println("Error, wrong object passed !");
				}
		}  
		is.close();  
		s.close();  
		ss.close();  
		}
		catch(Exception e){
			System.out.println("Server: " +e);
				}  
			}  
		}

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

This topic is closed to new replies.

Advertisement