 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Wonderer Guest
|
Posted: Sun Apr 25, 2004 12:19 pm Post subject: Problem with Java TCP client to the C server |
|
|
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
|
|
| Back to top |
|
 |
Danny Woods Guest
|
Posted: Sun Apr 25, 2004 12:35 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
"Wonderer" <truth (AT) here (DOT) com> writes:
| Quote: | 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.
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Sun Apr 25, 2004 12:40 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
On Sun, 25 Apr 2004 12:19:24 GMT, Wonderer wrote:
| Quote: | 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?
| Quote: | 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
|
|
| Back to top |
|
 |
Gordon Beaton Guest
|
Posted: Sun Apr 25, 2004 3:16 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
On Sun, 25 Apr 2004 12:40:10 GMT, Andrew Thompson wrote:
| Quote: | 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
--
[ 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
|
|
| Back to top |
|
 |
Steve Horsley Guest
|
Posted: Sun Apr 25, 2004 3:31 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
Wonderer wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Sun Apr 25, 2004 3:34 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
On 25 Apr 2004 17:16:49 +0200, Gordon Beaton wrote:
| Quote: | 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.
|
Ooops! Glad I added the caveat..
| Quote: | Whatever the OP's problem is, he hasn't provided nearly enough
information to help him (unless guessing counts).
|
;-)
I'd go the example route..
<http://www.physci.org/codes/sscce.jsp>
--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sun Apr 25, 2004 7:23 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
On Sun, 25 Apr 2004 12:19:24 GMT, "Wonderer" <truth (AT) here (DOT) com> wrote or
quoted :
| Quote: | 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.
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sun Apr 25, 2004 7:25 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
On 25 Apr 2004 17:16:49 +0200, Gordon Beaton <not (AT) for (DOT) email> wrote or
quoted :
| Quote: | Whatever the OP's problem is, he hasn't provided nearly enough
information to help him (unless guessing counts).
|
It is amazing how often you can solve a problem with a guess. Ask
yourself, what are the 5 errors I have made in past working in that
general area?
Chances are one of them is the magic solution.
I try to put them in http://mindprod.com/jgloss/gotchas.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Wonderer Guest
|
Posted: Mon Apr 26, 2004 3:40 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
"Gordon Beaton" <not (AT) for (DOT) email> wrote
| Quote: | 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.
| Quote: | --
[ 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
|
|
|
| Back to top |
|
 |
Gordon Beaton Guest
|
Posted: Mon Apr 26, 2004 3:57 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
On Mon, 26 Apr 2004 15:40:36 GMT, Wonderer wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Thomas Schodt Guest
|
Posted: Mon Apr 26, 2004 4:04 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
Wonderer wrote:
| Quote: | 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() ?
| Quote: | 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 " or "GET " ought to work.
And the server will know it has to read 4 bytes first to find out what
the client wants.
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Mon Apr 26, 2004 7:36 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
On Mon, 26 Apr 2004 17:04:56 +0100, Thomas Schodt
<"news04jan"@"xenoc.demon.co.uk"> wrote or quoted :
| Quote: |
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.
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Mon Apr 26, 2004 7:39 pm Post subject: Re: Problem with Java TCP client to the C server |
|
|
On Mon, 26 Apr 2004 17:04:56 +0100, Thomas Schodt
<"news04jan"@"xenoc.demon.co.uk"> wrote or quoted :
| Quote: | Normally you'd have your client send a packet identifying what it wants
immediately after connecting to the server.
Something simple like "PUT " or "GET " 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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|