Problem with Java TCP client to the C server

Discussion in 'Java' started by Wonderer, Apr 25, 2004.

  1. Wonderer

    Wonderer Guest

    Hi there

    I am trying to use a Java client to write to a server written in C, but for
    some reason, the C server is just unable to read from the Java client,
    ALTHOUGH it can write perfectly to the Java client.

    The Java client reads from the C server using an InputStream object, but te
    inverse,OutputStream, fails to work when writing back to the C server.

    Why is that? Both my InputStream and OutputStream are derived from the same
    socket that I used to initialize a connection the C server. Is that why it's
    not working?

    Thanks
    Wonderer, Apr 25, 2004
    #1
    1. Advertising

  2. Wonderer

    Danny Woods Guest

    "Wonderer" <> writes:

    > The Java client reads from the C server using an InputStream object, but te
    > inverse,OutputStream, fails to work when writing back to the C server.
    >
    > Why is that? Both my InputStream and OutputStream are derived from the same
    > socket that I used to initialize a connection the C server. Is that why it's
    > not working?


    Perhaps there's some buffering going on. Have you tried calling flush()
    against the OutputStream after you write to it?

    Danny.
    Danny Woods, Apr 25, 2004
    #2
    1. Advertising

  3. On Sun, 25 Apr 2004 12:19:24 GMT, Wonderer wrote:

    > I am trying to use a Java client to write to a server written in C, but for
    > some reason, the C server is just unable to read from the Java client,
    > ALTHOUGH it can write perfectly to the Java client.
    >
    > The Java client reads from the C server using an InputStream object, but te
    > inverse,OutputStream, fails to work when writing back to the C server.


    Fails to work? How?

    > Why is that? Both my InputStream and OutputStream are derived from the same
    > socket that I used to initialize a connection the C server. Is that why it's
    > not working?


    I am pretty sure that is it.
    Are you getting exceptions?
    <http://www.physci.org/codes/javafaq.jsp#exact>

    From my vague memory of a networking
    D'n'D list I made, was that it required
    two sockets, one for reading, one for writing
    [ My example _actually_ had four, so it was
    easy to feed either serialized objects or
    Strings, backwards and forwards. ]

    Try and make a pure Java exmaple that
    can communicate both ways, remove the
    'inter-language' question until you get
    the pure Java side solved, then attack
    I/O with the C server as the second part.

    You might also have a look over the
    Networking FAQ, you can find the link here..
    <http://www.physci.org/codes/javafaq.jsp#faq>

    Please note that three groups is probably
    too wide a cross-post.
    <http://www.physci.org/codes/javafaq.jsp#xpost>

    One of the groups is for Germany (de.?)
    but you seem comfortable with English, so
    I am setting the follow-ups to c.l.j.help

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Apr 25, 2004
    #3
  4. Wonderer wrote:
    > Hi there
    >
    > I am trying to use a Java client to write to a server written in C, but for
    > some reason, the C server is just unable to read from the Java client,
    > ALTHOUGH it can write perfectly to the Java client.
    >
    > The Java client reads from the C server using an InputStream object, but te
    > inverse,OutputStream, fails to work when writing back to the C server.
    >
    > Why is that? Both my InputStream and OutputStream are derived from the same
    > socket that I used to initialize a connection the C server. Is that why it's
    > not working?
    >
    > Thanks
    >
    >

    Without seeing code it's difficult to guess, but I have two comments:

    1: Don't forget to call flush() at the end of sending your data.

    2: C programmers often don't know exactly what they're sending over
    the wire. They often send images of structures as they are stored in
    memory without knowing what alignment padding has been used. A C
    source-code structure definition simply isn't adequate to define
    the network protocol.

    The best way to be sure what is expected is to use a protocol
    analyser (ethereal is good and free) to trace the C client working
    succesfully, and then wirte your java program to reproduce exactly
    what the C client sends.

    Steve
    Steve Horsley, Apr 25, 2004
    #4
  5. Wonderer

    Roedy Green Guest

    On Sun, 25 Apr 2004 12:19:24 GMT, "Wonderer" <> wrote or
    quoted :

    >Why is that? Both my InputStream and OutputStream are derived from the same
    >socket that I used to initialize a connection the C server. Is that why it's
    >not working?


    Try a packet sniffer to see if the fault lies with the receiver or
    sender.

    See http://mindprod.com/jgloss/sniffer.html

    Try a flush after each record or batch. Your stuff may be stuck in a
    BufferedOutputStream. The underlying socket layer may not have even
    seen it you.

    See http://mindprod.com/fileio.html
    for how to do raw socket i/o.



    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 25, 2004
    #5
  6. Wonderer

    Wonderer Guest

    "Gordon Beaton" <> wrote in message
    news:408bd661$...
    > On Sun, 25 Apr 2004 12:40:10 GMT, Andrew Thompson wrote:
    > > From my vague memory of a networking
    > > D'n'D list I made, was that it required
    > > two sockets, one for reading, one for writing

    >
    > That shouldn't be necessary at all - A TCP socket is two independent
    > streams, one in each direction.
    >
    > You will get slightly better network performance by using a single
    > socket in both directions, instead of two sockets, one for each
    > direction (piggy-back ACKs, for example).
    >
    > Whatever the OP's problem is, he hasn't provided nearly enough
    > information to help him (unless guessing counts).
    >
    > /gordon
    >

    Hey there, my basic problem right now is that on the C server side, I need
    to read and write to a Java client(really a web applet) when it connects and
    the C server accepts the connnection. The thing I realized is that if I read
    from this incoming connection, I cannot write to it anymore, and vice versa
    if I write to it first. Thus, I don't know how to get the server to identify
    whether the incoming connection will require data sent to OR from the Java
    client.

    > --
    > [ 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
    >
    Wonderer, Apr 26, 2004
    #6
  7. On Mon, 26 Apr 2004 15:40:36 GMT, Wonderer wrote:
    > Hey there, my basic problem right now is that on the C server side,
    > I need to read and write to a Java client(really a web applet) when
    > it connects and the C server accepts the connnection. The thing I
    > realized is that if I read from this incoming connection, I cannot
    > write to it anymore, and vice versa if I write to it first. Thus, I
    > don't know how to get the server to identify whether the incoming
    > connection will require data sent to OR from the Java client.


    You should be able to do both without any kind of restrictions. That
    is until you close either the InputStream or the OutputStream, at
    which point the Socket itself is closed, preventing you using the
    other stream anymore.

    Exactly what happens when you try to write to it? Do you get an
    exception or what? Post it!

    Also post the relevent code - both client and server, both reading and
    writing.

    /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, Apr 26, 2004
    #7
  8. Wonderer wrote:

    > my basic problem right now is that on the C server side, I need
    > to read and write to a Java client(really a web applet) when it connects and
    > the C server accepts the connnection. The thing I realized is that if I read
    > from this incoming connection, I cannot write to it anymore, and vice versa
    > if I write to it first.



    You mean after you post a blocking read() you don't get control back
    meaning you can't write?

    I guess not, as you claim
    if you write() first you will not be able to read() ?


    > Thus, I don't know how to get the server to identify
    > whether the incoming connection will require data sent to OR from the Java
    > client.


    Normally you'd have your client send a packet identifying what it wants
    immediately after connecting to the server.
    Something simple like "PUT\0" or "GET\0" ought to work.
    And the server will know it has to read 4 bytes first to find out what
    the client wants.
    Thomas Schodt, Apr 26, 2004
    #8
  9. Wonderer

    Roedy Green Guest

    On Mon, 26 Apr 2004 17:04:56 +0100, Thomas Schodt
    <"news04jan"@\"xenoc.demon.co.uk\"> wrote or quoted :

    >
    >You mean after you post a blocking read() you don't get control back
    >meaning you can't write?
    >
    >I guess not, as you claim
    >if you write() first you will not be able to read() ?


    You have two streams with a socket, a read and a write. Both can work
    simultaneously, but obviously you need at least two threads, one for
    reading and one for writing.

    http://mindprod.com/jgloss/thread.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 26, 2004
    #9
  10. Wonderer

    Roedy Green Guest

    On Mon, 26 Apr 2004 17:04:56 +0100, Thomas Schodt
    <"news04jan"@\"xenoc.demon.co.uk\"> wrote or quoted :

    >Normally you'd have your client send a packet identifying what it wants
    >immediately after connecting to the server.
    >Something simple like "PUT\0" or "GET\0" ought to work.
    >And the server will know it has to read 4 bytes first to find out what
    >the client wants.


    In HTTP the server reads one message. It knows when to stop reading
    from the Content-Length field in the header. Then it digests the
    message, and sends back a response.

    If you have continuous streams of messages coming in and messages
    going out with no direct connection to each other, your server needs
    two threads per socket too (or a way of faking it).

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 26, 2004
    #10
    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. Mariya
    Replies:
    13
    Views:
    802
    Steve Horsley
    Aug 5, 2004
  2. qqq111
    Replies:
    19
    Views:
    1,140
    Roedy Green
    Mar 1, 2006
  3. Tiger
    Replies:
    5
    Views:
    956
    Dave Thompson
    May 1, 2006
  4. TsanChung
    Replies:
    5
    Views:
    2,734
    Antoninus Twink
    Oct 18, 2008
  5. TsanChung
    Replies:
    3
    Views:
    1,812
    Antoninus Twink
    Oct 18, 2008
Loading...

Share This Page