Results 1 to 4 of 4
Thread: ObjectInput/Output Streams
- 07-29-2008, 10:27 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
ObjectInput/Output Streams
I am trying to write a multi-client server that can pass objects back and forth between the server and it's client(s) through a socket using ObjectInputStream and ObjectOutputStream. I can setup the connection and even initiate the ObjectInput/output streams, but I must have the format wrong. I mimicked another program I wrote using Scanner and PrintWriter (which works perfectly) like this:
This is the run method in my server code. It is in a subclass of the server called ClientThread which gets initiated and started from the main method:
Java Code:public void run(){ //System.out.println(clientList); System.out.println(client.ID); try { objIn = new ObjectInputStream(connection.getInputStream()); objOut = new ObjectOutputStream(connection.getOutputStream()); courier.playerID = client.ID; courier.cmd = "client.ID"; objOut.writeObject(courier); while(true){ courier = (Info)objIn.readObject(); filterCmd(courier); }
This is from my client code. It is simply in the run method of my client class.
For a long time I was getting Connection reset errors, but now I don't (and I don't remember what I did because I was just messing with the code).Java Code:public void run(){ try { OutputStream os = connection.getOutputStream(); objOut = new ObjectOutputStream(os); objOut.flush(); InputStream is = connection.getInputStream(); objIn = new ObjectInputStream(is); }catch(Exception e){ } try{ while (true){ Info courierIn = (Info) objIn.readObject(); filterCmd(courierIn); } }catch (Exception e){ }
So what happens now is that the server sends the first "courier" object to the client and the client filters it correctly. But then I try to send another object back to the server from the client by invoking the following method from a swing component:
When I invoke this method, the ObjectInput/outputs on both sides are severed. How am I supposed to keep my server and clients listening for incoming objects while also being able to write objects out to the stream. Any help on this would be greatly appreciated. I am learning java on my own and have been struggling with this issue for a week or more. Thanks.Java Code:public void send(String cmd){ try { courierOut.cmd = cmd; objOut.writeObject(courierOut); } catch (IOException ex) { } }
Matt
- 07-29-2008, 10:59 PM #2
Looks like a complicated program for a beginner. Have you tried a simpler one that only sends a String initially until you get the technique?
How are you doing this?keep my server and clients listening
Do you get any error messages? Copy and post them here.
Can you write a single program that demos the problem and post it here. A main() that starts a server and some clients and tracks the flow with println().
- 07-29-2008, 11:38 PM #3
What Norm said. Get strings to work first. To send Objects, they must be serializable and have the same version.
I never send objects. I always send strings. I invent simple string formats and send them back and forth.
In addition to the advantage of being easy to understand and debug, it also has a major advantage of working through firewalls and being directly convertable to HTTP/HTTPS
- 07-30-2008, 01:26 AM #4
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Runtime.exec(), handling input and output streams
By crookshank in forum New To JavaReplies: 0Last Post: 06-05-2008, 02:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks