Socket I/O Question!

Discussion in 'Java' started by Ken Adams, Dec 8, 2004.

  1. Ken Adams

    Ken Adams Guest

    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.
     
    Ken Adams, Dec 8, 2004
    #1
    1. Advertising

  2. On Wed, 08 Dec 2004 05:02:21 GMT, Ken Adams wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Dec 8, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    5,084
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,026
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    594
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    711
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    756
Loading...

Share This Page