Jump to content
  • Advertisement
Sign in to follow this  
RLS0812

Simple Socket Server Is Continuing To Crash

This topic is 1498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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 ?!

Edited by Shippou

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 

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);
				}  
			}  
		}
Edited by Shippou

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!