Problem with Java TCP client to the C server

W

Wonderer

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
 
D

Danny Woods

Wonderer said:
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.
 
A

Andrew Thompson

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
 
S

Steve Horsley

Wonderer said:
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
 
R

Roedy Green

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.
 
W

Wonderer

Gordon Beaton said:
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.
 
G

Gordon Beaton

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
 
T

Thomas Schodt

Wonderer said:
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.
 
R

Roedy Green

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
 
R

Roedy Green

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).
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top