Java Server/C Client

Hi I have a networking issue between my two programs. I believe the issue is in the Java Server.

At the moment they connect but a message isn't sent between even though both programs seem to be working fine. They both wait for a reply. They both compile and don't crash, they both just hang waiting for each other. I think the problem is in my Java code. Here is the connection method and where it hangs:

Your mySocket variable is declared within your first try block, so it goes out of scope at the end of that try block. Declare it outside the try block and set it to null, and you should be fine.

EDIT: Sorry, I didn't look closely enough -- it's not declared there, so it must be an instance variable in your class. In that case, it needs to be declared static -- is it? (I guess it is, or it wouldn't compile.) So I don't know what's wrong. I've duplicated your code, and it's working for me. (Tested with 'telnet localhost 4000'.)

I can flush the java out with .flush() obviously. But I don't know how to flush in C. I'm not very experienced with C, all I know is it uses a "send" function and not a stream, that I'm aware of. Here's the code to send the message in C:

It was a combination of new line characters, and my client wait for a message infinitely before sending one back.

Good; so it was a \n versus \r\n issue; in case it matters in the future: a socket normally uses Nagle's algorithm (it won't send anything until a certain amount of bytes are ready to be send). You can turn off this feature with the setsockopt( ... ) function with the TCP_NODELAY option switched on. Using Java you normally don't need it.