Java client reading char array from C++ server

Discussion in 'Java' started by Greyham, Feb 8, 2005.

  1. Greyham

    Greyham Guest

    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!
     
    Greyham, Feb 8, 2005
    #1
    1. Advertising

  2. Greyham

    dar7yl Guest

    "Greyham" <> wrote in message
    news:...
    > 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.
     
    dar7yl, Feb 9, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    576
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,097
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    744
  4. davidb
    Replies:
    0
    Views:
    773
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,563
    Default User
    Sep 1, 2006
Loading...

Share This Page