views:

2091

answers:

5

I am building a simple client/server application using java sockets and experimenting with the ObjectOutputStream etc.

I have been following the tutorial at this url http://java.sun.com/developer/technicalArticles/ALT/sockets starting half way down when it talks about transporting objects over sockets.

See my code for the client http://pastebin.com/m37e4c577 However this doesn't seem to work and i cannot figure out what's not working. The code commented out at the bottom is directly copied out of the tutorial - and this works when i just use that instead of creating the client object.

Can anyone see anything i am doing wrong?

Thanks in advanced.

+4  A: 

You are not writing the object anywhere.

See that link again, somewhere you have to write:

 oos.writeObject( new Date() );

In your code you only have

ois.readObject();

That's why

OscarRyz
The code posted only includes the client, so it doesn't need to write an object.
Mike Houston
A: 

Probably you'll like to learn the most basic first.

Here's a sample I have just coded.

It start a server, that attends only ONE client, and it sends an object and die.

When the user ( you ) press enter, a new client is created, it connects to the previously created server and read the object that server will send.

No exception is handled here. Just to make things simpler, but this is NOT the way exception should be handled.

When you understand all the concepts here, it will be easier to understand those in the tutorial.

import java.io.*;
import java.net.*;
import java.util.*;

public class SimpleServer implements Runnable { 

     // Creates the server, send a "date" and die.
    public void run() { 
        try {
            ServerSocket server = new ServerSocket( 8090 );
            Date creationDate = new Date();
            System.out.println("(Server) Server is ready " 
                                 + "and running at " + creationDate );

            Socket uniqueClient = server.accept();

            // We will write using this "chained" object.
            ObjectOutputStream out = new ObjectOutputStream( 
                                                 uniqueClient.getOutputStream());

            out.writeObject( creationDate );


            // close all, at this point forget about the exceptions.
            // this is lesson #1      
            out.close();

            uniqueClient.close();

            server.close();        

            System.out.println("(Server) The server is down");
        }catch( IOException ioe ) {}

     }

    public static void main ( String [] args ) throws IOException ,
                                                 ClassNotFoundException { 

         Thread serverThread = new Thread( new SimpleServer() );

         serverThread.start(); // start the server thread ... doh..

         System.out.println("(Client) Press enter when you want "+ 
                               " to connect to the server...");

         Scanner scanner = new Scanner( System.in );

         scanner.nextLine();

         Socket client = new Socket("localhost", 8090 );

         // Read from this object.
         ObjectInputStream in = new ObjectInputStream( client.getInputStream() );

         Date date = ( Date ) in.readObject();

         System.out.println("(Client) Current time is:          " + new Date() );
         System.out.println("(Client) Object read from server : " + date );

         in.close();
         client.close();

    }
}

I hope this helps.

OscarRyz
+1  A: 

If you tried a debugger it would have told you where the problem was.(Perhaps not why)

The issue you have is that ObjectOutputStream writes a header and ObjectInputStream reads that header. You have created the ObjectInputStream first which means it is trying to read a header which will never be written.

Solution: Always create the ObjectOutputStream first and flush() it before creating the ObjectInputStream.

Peter Lawrey
+1  A: 

Just a reminder.

When you use ObjectOutputStream keep in mind that it keeps a reference cache. If you write an object, change the object contents, and then send the same object again, you will get duplicate data. For example:

List list = new ArrayList();
list.add("value1");
out.writeObject(list);
list.clear();
list.add("value2");
out.writeObject(list);

Will produce in the client side two lists with the string "value1".

To avoid this, the reset method must be invoked to reset the stream cache when writing the same object reference multiple times:

List list = new ArrayList();
list.add("value1");
out.writeObject(list);
out.reset();
list.clear();
list.add("value2");
out.writeObject(list);
jassuncao
Cheers - this will be a useful reminder!
Malachi
+4  A: 

The problem is the order you are creating the streams:

In the server from the article (which I assume is what you are using), when a new connection is opened, the server opens first an input stream, and then an output stream:

public Connect(Socket clientSocket) {
 client = clientSocket;
 try {
  ois = new ObjectInputStream(client.getInputStream());
  oos = new ObjectOutputStream(client.getOutputStream());
 } catch(Exception e1) {
     // ...
 }
 this.start();
}

The commented example code uses the reverse order, first establishing the output stream, then the input stream:

// open a socket connection
socket = new Socket("localhost", 2000);
// open I/O streams for objects
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());

But your code does it the other way around:

server = new Socket(host, port);
in = new ObjectInputStream(server.getInputStream());
out = new ObjectOutputStream(server.getOutputStream());

Establishing an output stream/input stream pair will stall until they have exchanged their handshaking information, so you must match the order of creation. You can do this just by swapping lines 34 and 35 in your example code.

Mike Houston