0

I'm writing a tcp server in c# and corresponding client in java. I'm testing the connection on localhost, and the client is able to connect to the server. However, when I'm sending messages, the client never receives them. Using the debugger I've verified that stream.Write(...) is executed. Any idea what the problem could be?

This is the c# server:

        TcpClient client = (TcpClient)cl;
        NetworkStream stream = client.GetStream();

        byte[] msg = new byte[512];
        int bytesRead; 

        while (running)
        {
            while (messages.getCount() > 0)
            {
                String msg = messages.Take();

                if (cmd != null)
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(msg.ToCharArray());

                    try
                    {
                        stream.Write(bytes, 0, bytes.Length);
                        stream.Flush();
                    }
                    catch (Exception e)
                    {

                    }
                }
            }

            Thread.Sleep(1000); 
        }

And the Java client:

public void run() 
{
    try 
    {
        socket = new Socket(address, port);
        in = new BufferedReader( new InputStreamReader( socket.getInputStream() ));
        out = new PrintWriter(socket.getOutputStream());
        running = true;
    } 
    catch (Exception e){
        e.printStackTrace();
        running = false; 
    } 

    String data;
    while(running)
    {
        try 
        {
            data = in.readLine();

            if(data != null)
            {
                processData(data);
            }
        } 
        catch (IOException e) 
        {
            e.printStackTrace();

            running = false; 
            break;
        }
    }

    try 
    {
        socket.close();
        socket = null;
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    running = false; 
}

1 Answer 1

5

You're using BufferedReader.readLine(). Are your message strings terminated by a CR, LF, or CR/LF?

readLine blocks until a line-terminating character is read.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.