Results 1 to 3 of 3
- 02-08-2011, 12:49 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
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 01:04 AM. Reason: Moderator Edit: code tags added
-
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 [code] above your pasted code and the tag [/code] below your pasted code like so:
I have added tags to your code above.Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
Last edited by Fubarable; 02-08-2011 at 01:07 AM.
- 02-08-2011, 01:11 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Vista + eclipse + JUnit => "No socket available"
By elannik in forum EclipseReplies: 5Last Post: 04-02-2010, 04:01 PM -
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
[SOLVED] pls help :S . "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerE
By ara in forum New To JavaReplies: 10Last Post: 01-29-2009, 08:00 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks