
03-20-2010, 12:38 AM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 10
Rep Power: 0
|
|
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:
|
Code:
|
public void connect() throws IOException
{
try
{
// Create server socket on specified port
// to wait for client requests
mySocket = new ServerSocket(4000);
}
catch (IOException e)
{
System.err.println("IO exception on port");
System.exit(1);
}
System.out.println("Server Socket set up, waiting for call");
try
{
// Wait here for the client request
// Returns a normal socket
clientSocket = mySocket.accept();
}
catch (IOException e)
{
System.err.println("Could not accept incoming call");
System.exit(1);
}
System.out.println("Incoming Call Accepted");
PrintWriter out = new PrintWriter(
clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
out.println("Welcome");
System.out.println(in.readLine());
} |
The Server waits on the last "System.out.println(in.readLine());", and the client is still waiting for the message from the Server.
Can anyone see any mistakes?
|
|

03-20-2010, 02:59 AM
|
 |
Member
|
|
Join Date: Jan 2009
Posts: 26
Rep Power: 0
|
|
|
I have found that there are issues when an input stream and an output stream are open at the same time.
It always freezes on me when I do it.
__________________
If you don't try you can't succeed.
|
|

03-20-2010, 11:07 AM
|
|
Senior Member
|
|
Join Date: Mar 2010
Posts: 605
Rep Power: 1
|
|
|
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'.)
-Gary-
Last edited by gcalvin; 03-20-2010 at 11:17 AM.
|
|

03-20-2010, 01:11 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
|
|
|
Can it be an end of line issue? i.e. one party writes a single \n for an end of line while the other party waits for a \r\n\ pair to mark the end of a line.
kind regards,
Jos
|
|

03-20-2010, 02:34 PM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 10
Rep Power: 0
|
|
Well I've changed the code to this:
|
Code:
|
public String connect() throws IOException
{
String message = null;
try
{
// Create server socket on specified port
// to wait for client requests
mySocket = new ServerSocket(4000);
}
catch (IOException e)
{
System.err.println("IO exception on port");
System.exit(1);
}
System.out.println("Have Server Socket set up, waiting for call");
try
{
// Wait here for the client request
// Returns a normal socket
clientSocket = mySocket.accept();
}
catch (IOException e)
{
System.err.println("Could not accept incoming call");
System.exit(1);
}
System.out.println("Incoming Call Accepted");
PrintWriter out = new PrintWriter(
clientSocket.getOutputStream(), true);
String inputLine, outputLine;
out.println("Welcome\r\n");
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
System.out.println(in.readLine());
return message;
} |
Because I used "out.close()" it crashes the Java server, saying that the socket is closed. If I take "out.close()" out, it doesn't crash but it still just hangs waiting for the message.
However the C client now gets the message of "Welcome" and sends it's response.
|
|

03-20-2010, 02:44 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
|
|
|
What happens when you flush the output streams (both on the client side and the server side).
kind regards,
Jos
|
|

03-20-2010, 02:49 PM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 10
Rep Power: 0
|
|
|
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:
send(socket_fd, "1/523/101/Sam/nighttime\r\n", 23, 0);
|
|

03-20-2010, 03:58 PM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 10
Rep Power: 0
|
|
|
FIXED!
It was a combination of new line characters, and my client wait for a message infinitely before sending one back.
|
|

03-20-2010, 04:14 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
|
|
Originally Posted by FallenBlade
|
FIXED!
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.
kind regards,
Jos
|
|

03-20-2010, 04:50 PM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 10
Rep Power: 0
|
|
|
Cheers, glad this is all sorted. Now to get the messages to have meaning and my programs working!
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 09:16 PM.
|
|