Hi all,

Ok, so I know how to write a simple client/server app where the send a message from the client and the message displays on the server's textarea, and I send a message from a server and it displays on the client's textarea.

To do this I normally use ObjectOutputStream and ObjectInputStream.

For example, this is a method in the client class used to send messages to the server:

private void sendData( String message )
   {
      try // send object to server
      {
    	  
         output.writeObject( "CLIENT>>> " + message );
         output.flush(); // flush data to output
         
         displayMessage( "\nCLIENT>>> " + message );
      }
      catch ( IOException ioException )
      {
         displayArea.append( "\nError writing object" );
      }
   }

However, I want to go further than that. I want to be able to declare variables, pick them, put them into the output stream and send them to the server, and I want the server to be able to take these variables one by one and store them into identical variables.

For example, if I had these in the client class:

Private String name;
Private int phoneNumber;
Private String address;

I want to be able to take them and pass them on to the server, and have the server store them in similar variables, like:

Private String ClientName;
Private int ClientPhoneNumber;
Private String ClientAddress;

How do I this?

Also, can some please explain why I have to use the ".flush" method after using an InputStream object?

Cheers

Have you tried the other write methods in the ObjectOututStream class? There are methods for different data types.

why I have to use the ".flush" method after using an InputStream object

I don't know why you'd use flush with an InputStream.

Hi NormR1,

I meant, using flush in the outputstream. Every example I've seen, the stream has to be flushed, but I don't know why lol

And as for the the variable questions, I meant, how do I deliver the the variables to the server one at a time? Is it ok to do something like this?

output.writeObject(name)
output.writeObject(Integer.toString(phoneNumber))
output.writeObject(address)

And after that, is there a way for me to use the inputStream on the server side to know which data is the name, phoneNumber and address, so I can store them in the corresponding variables?

For int you could use writeInt()

is there a way for me to use the inputStream on the server side to know which data is the name, phoneNumber and address

Either your sender and receiver agree on the order of what is being sent so they can keep in sync,
Or you need to send identifying info to tell the receiver what is next. The output stream could look like:
<TYPE><NAME><data>
Where <TYPE> and <NAME> are Strings and <data> is the data of the type:<TYPE>
for example:
"long""clientPhoneNumber"2344567789
Two strings followed by an long
In code:
output.writeObject("long");
output.writeObject("clientPhoneNumber");
output.writeLong(2344567789);

Or for Strings you could use writeUTF and readUTF if your strings are ASCII chars.

Another way would be to have a class that encloses those values and write it as an Object.

Cheers, mate. Now I understand what I need to do.

Thanks!

Every example I've seen, the stream has to be flushed, but I don't know why

Some implementations of the OutputStream/Writer (e.g. BufferedWriter) buffer the data written to them and write it to the underlying actual stream only when the buffer is full. This is so as to avoid doing IO for each and every `write` call made and improve performance (IO is typically magnitudes slower than CPU bound operations). `flush` forces the implementation to write the data which it has buffered to the underlying stream.

Read the section 4.1.1 of this document.
http://java.sun.com/docs/books/performance/1st_edition/html/JPIOPerformance.fm.html

Also, as far as your question is concerned, if you are anyways writing objects, instead of owning the headache of mapping values to their respective variables, write out a single object. For e.g. if you want to send across a long value named phoneNumber, a string value named 'name', instead of doing something like:

oos.write("name"); oos.write("string"); oos.write(yourName);
oos.write("phone"); oos.write("long"); oos.write(longValue);

prefer something like this:

Person p = new Person(yourName, longValue);
oos.write(p);

sos, thanks. Really helpful

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.