Socket I/O Question!

K

Ken Adams

Basically all I want to know is that after a client/server is set up using
sockets and say you start by setting the input and output streams to say
PrintWriter(or whatever) can you just go ahead and set the stream to be
another type such as ObjectOuputStream to to send and object? This is of
course assuming both server and client do these steps in the same order.

Basically what I want my client to do is connect to the server, send a
string representing the name of the client then send some Command messages
to the server. Or do you think a better method is to just set up and object
stream and just cast the first object as a string?

Thanks a bunch.
 
G

Gordon Beaton

Basically all I want to know is that after a client/server is set up
using sockets and say you start by setting the input and output
streams to say PrintWriter(or whatever) can you just go ahead and
set the stream to be another type such as ObjectOuputStream to to
send and object? This is of course assuming both server and client
do these steps in the same order.

You can't "set the stream to be another type". You connect streams to
each other to get various types of data conversions.

Don't try to wrap an ObjectOutputStream around any kind of Writer,
which expects *text*. If you want to send Objects, wrap the stream
around an OutputStream, which expects *bytes*.

By the same token, wrap the corresponding ObjectInputStream around an
an InputStream (not a Reader) at the other end.
Basically what I want my client to do is connect to the server, send
a string representing the name of the client then send some Command
messages to the server. Or do you think a better method is to just
set up and object stream and just cast the first object as a string?

If you only want to send Strings, don't use ObjectStreams at all. Wrap
a Reader and Writer around the InputStream and OutputStream. If you
use a BufferedReader for input, you can easily read the data one line
at a time.

Here is an introduction to this topic:

http://java.sun.com/docs/books/tutorial/essential/io/overview.html

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top