This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Sockets between Java and C Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sockets between Java and C" Watch "Sockets between Java and C" New topic
Author

Sockets between Java and C

Gus Spain
Greenhorn

Joined: Sep 12, 2004
Posts: 17
Hello,
first of all, i�m not pretty sure if i should post it here or in a C forum, so my apologies if i do wrong.
I�m trying to communicate a java client and a C server, and it works....almost. While there�s no problem in Java sending messages and C receiveng them properly, the other way doesn�t seem to work.
This piece of code shows two ways how i tried to send the information from C back to java:
1) char *cadena;
cadena = malloc(sizeof(char)*1024);
cadena = Getdata(Buffer,Buffer2, &db);
retval = send(msgsock,cadena,sizeof(Buffer2),0);
free(cadena);
2) retval = send(msgsock,Getdata(Buffer,Buffer2, &db),sizeof(Buffer2),0);

I don�t get any error message while sending. And here it is how i do receive in java:

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str;
StringBuffer text=new StringBuffer();
System.out.println("BEFORE THE IF CLAUSE");
if ((str = in.readLine())!=null)
{
reference.printResult(str);
text.append(str);
}

and here�s the problem, it prints the message "BEFORE THE IF CLAUSE" but then it seems it never gets to do the readLine, like if it got stuck in that line. The strange thing is that if the C server sends a message like "FROM C TO JAVA", Java receives it fine, but then why it does not the result of the function i need?
Would someone point me in the right direction?
THANK YOU SO MUCH!!
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Does your "From C to java" contain an eol?
And the output of the function not?

readLine might be waiting for the lineterminator...


http://home.arcor.de/hirnstrom/bewerbung
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
I sure would stick to sending bytes in both directions, and don't depend on the Java character conversions that using a Reader and readLine implies.
After you have safely received the bytes, you can try to convert to Strings.
Also - be sure you are catching and printing any IOException thrown.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sockets between Java and C
 
Similar Threads
problem reading String
Problems with read and readLine
Buffering using Thread Synchronization
Problems with read and readLine
JNA handling char** java mapping