Results 1 to 4 of 4
  1. #1
    Rhesus21 is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default 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.

    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){
                    
    
                }
    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).

    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:

    Java Code:
     public void send(String cmd){
            try {
                courierOut.cmd = cmd;
                objOut.writeObject(courierOut);
            } catch (IOException ex) {
                
            }
        }
    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.


    Matt

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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?
    keep my server and clients listening
    How are you doing this?

    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().

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    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

  4. #4
    Rhesus21 is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    I got it working perfectly now (for now). Thanks for your replies!

    Matt

Similar Threads

  1. Runtime.exec(), handling input and output streams
    By crookshank in forum New To Java
    Replies: 0
    Last Post: 06-05-2008, 02:41 PM

Posting Permissions

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