Java client reading char array from C++ server

G

Greyham

I'm trying to get a Java client to recieve data from a C++ server but
the java client crashes as soon as I try the readLine() function of my
bufferedreader. My C++ server is the one from MSDN at:
http://msdn.microsoft.com/library/d...n-us/winsock/winsock/complete_server_code.asp
And here's my Java code:

import java.net.*;
import java.io.*;

public class tcpClientString {
public static void main(String[] args) {
int port = 1500;
String server = "localhost";
Socket socket = null;
BufferedReader input;

// connect to server
/*.....code.....*/

try {
System.out.println("one");
input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
System.out.println("two");
String message = input.readLine();
System.out.println("three");
System.out.println(message);
}
catch (IOException e) {
System.out.println(e);
}

// closing connection
/*....code....*/
}
}//////////////END OF CODE/////////////////////
So my java output tells me I've connected to the server and then I get
the messages "one" and "two" but then "Java.net.SocketException:
Connection Reset" My C++ code seems to work fine... it tells me I've
sent 22 bytes (message should be "Server: Sending Data.\n"). Thanks
for the help!
 
D

dar7yl

Greyham said:
I'm trying to get a Java client to recieve data from a C++ server but
the java client crashes as soon as I try the readLine() function of my
bufferedreader. My C++ server is the one from MSDN at:
http://msdn.microsoft.com/library/d...n-us/winsock/winsock/complete_server_code.asp
And here's my Java code:
...
String message = input.readLine();
...
So my java output tells me I've connected to the server and then I get
the messages "one" and "two" but then "Java.net.SocketException:
Connection Reset" My C++ code seems to work fine... it tells me I've
sent 22 bytes (message should be "Server: Sending Data.\n"). Thanks
for the help!

Look at the code for the .asp:
< code .asp>
char sendbuf[32] = "Server: Sending Data.";
char recvbuf[32] = "";
....
bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
printf( "Bytes Sent: %ld\n", bytesSent );
</code>

You'll see that it doesn't actually have a '\n' at the end, which you
are expecting in the input.readLine() call. It's still waiting for it,
then the socket times out and the connection is lost.

Blame it on Bill Gates ;)

regards,
Dar7yl.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top