Results 1 to 3 of 3
  1. #1
    superzipzop is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Why do I get "java.io.NotSerializableException: java.net.Socket"?

    I was testing out the ObjectOutputStream class; my code was practically copied word for word from the tutorial, but when I run it, I get

    "java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.net.Socket"

    These are my classes. I made my TestObject class serializable, so I don't understand why I'm getting the error. I wanted to make a class that would serve as a link between the server and the client: the server would add the clients to TestObject, and the TestObject would give itself to the client. Then the TestObject would handle all the communication. I'm just starting the networking tutorials, so I apologize if I'm just being stupid with this one.

    Java Code:
    public class GameServer
    {
    	public static void main(String args[]) throws IOException
    	{
    		ServerSocket ss = new ServerSocket(4117);
    	        TestObject obj = new TestObject();
    		Thread t = new Thread(obj);
    		t.start();
    		System.out.println("Waiting for clients...");
    		while(true)
    		{
    			Socket s = ss.accept();
    			System.out.println("Client Connected");
    			obj.addClient(s);
    		}
    	}
    }
    
    
    public class TestObject implements Runnable, Serializable
    {
    	private ArrayList<Socket> clients;
    	public TestObject()
    	{
    		clients = new ArrayList<Socket>();
    	}
    	public void addClient(Socket s) throws IOException 
    	{
    		clients.add(s);
    		OutputStream out = s.getOutputStream();
    		ObjectOutputStream outos = new ObjectOutputStream(out);
    		outos.writeObject(this);
    	}
    }
    
    public class GameClient implements Serializable
    {
    	public static void main(String args[]) throws ClassNotFoundException 
    	{
    		try 
    		{
    			Socket s = new Socket("localhost", 4117);
    			InputStream is = s.getInputStream();
    			ObjectInputStream os = new ObjectInputStream(is);
    			TestObject obj = (TestObject)os.readObject();
    		} 
    		catch (UnknownHostException e) 
    		{
    			e.printStackTrace();
    		} 
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		}
    	}
    }
    Last edited by Fubarable; 02-08-2011 at 02:04 AM. Reason: Moderator Edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What does it mean to serialize a Socket? I'm not sure why you'd want to do this or what you'd do with it if successful. What are you trying to do with your serialization?


    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    I have added tags to your code above.
    Last edited by Fubarable; 02-08-2011 at 02:07 AM.

  3. #3
    superzipzop is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Oh that makes sense. I misunderstood how sockets work, I'm sorry. Thanks, and thanks for the code tip.
    Last edited by superzipzop; 02-08-2011 at 02:21 AM.

Similar Threads

  1. Replies: 5
    Last Post: 04-02-2010, 05:01 PM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  3. Replies: 1
    Last Post: 11-20-2009, 08:58 PM
  4. Replies: 10
    Last Post: 01-29-2009, 09:00 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •