• Post Reply Bookmark Topic Watch Topic
  • New Topic

C and Java Programming  RSS feed

 
amit bamby
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want send Data from C client to Java Server Socket,Infact Java client is working well with Java Serversocket, However problem with C client, pls help me to get off from this problem
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

There are really no special problems. If you're sending binary data, you should send it from C in "network byte order" using functions in the hton(), ntoh(), but you should always do that anyway. Then on the Java end, you'd use a DataInputStream/DataOutputStream combination.

What problems are you having?
 
amit bamby
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok C client is sending database URL and Classpath to Java Server , n Java Server uses JDBC to connect to database using url and classpath string provided by C client, infact in java server i m using StringTokenizer to break the string into url and classpath

Now pls tell me how to approach on this issue
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're just exchanging text, then even the network-byte-order rule is irrelevant. So as I said, what problems are you having? What errors are you seeing?
 
Peter Aarestad
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me chime in here, since I am trying to figure out how to do the same thing. On the server (Java) side, here's my simple code:



On the C side, I am simply doing :



However, when I run the C client after having started the Java server, this is what I see:


What should I be doing differently here? Anyone else try to do this before? I tried removed the "+ 1" from the third argument of send() since Java strings are not null-delimited, but I still got the EOFException. The C program exits normally (I'm checking the return code from send()), so I know the client is sending the data. Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,

Well, if you're going to use readUTF(), then the C program has to write UTF data. The definition for the format is here; look for the part about the readUTF() method. It's unfortunately much more complex than just sending the characters!

An alternative, if you're just sending text, is to use your existing C code and do this in Java:

BufferedReader reader = new BufferedReader(s.getInputStream())
String st = reader.readLine();
 
Peter Aarestad
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Hi Peter,

Well, if you're going to use readUTF(), then the C program has to write UTF data. The definition for the format is here; look for the part about the readUTF() method. It's unfortunately much more complex than just sending the characters!


I found that just before I came back to this forum. Basically, it's not so bad for plain old ASCII - I coded up a general-purpose function in C that emulates Java's protocol, and it looks like it works pretty well. Thanks for your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!