-1

[SOLVED]

I have similar problem to the post here, have followed all the answers, but got empty string as response. data.
Here's my code :

try
{
   string ENDOFLINE = "\r\n";
   TcpClient client = new TcpClient(txtIP.Text, Int32.Parse(txtPort.Text));
   NetworkStream stream = client.GetStream();

   byte[] data = Encoding.ASCII.GetBytes(txtMessage.Text + ENDOFLINE);
   stream.Write(data, 0, data.Length);

   String responseData = String.Empty;
   Int32 bytes = stream.Read(data, 0, data.Length);
   responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
   Console.WriteLine("Received: {0}", responseData);
   stream.Close();
   client.Close();
}
catch(Exception ex)
{
   Console.WriteLine(ex.message);
}

Solution:

Disconnecting the connection right after sending the message and before receiving response. I followed this article

7
  • 2
    Please always post code in the question itself, not through external links, so the question does not become useless if the link dies. Commented Dec 29, 2016 at 8:21
  • @PeterB, thank you for your advice, I have edited the post :) Commented Dec 29, 2016 at 8:34
  • you cant send bytes array from C# to Java and VV because Java uses sbyte not byte ... you should change byte[] data = Encoding.ASCII.GetBytes(txtMessage.Text + ENDOFLINE); Commented Dec 29, 2016 at 8:41
  • @Alrehamy ok, let me try your solution. Commented Dec 29, 2016 at 8:51
  • @alrehamy, I could not get it working, do you have a sample or perhaps an article I can read? Commented Dec 29, 2016 at 9:13

1 Answer 1

3

Your code works fine but only if the server-side is cooperative.

I tested it by having the simple java server implementation from Christian Tucker with a slightly altered run implementation:

public void run()
{
    while(isRunning)
    {
        try 
        {
            clientSocket = serverSocket.accept();
            System.out.println("Client Connected from " + clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort());
            recv = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            System.out.println("Data Recieved: " + recv.readLine());

            // make sure to send something back ...
            OutputStreamWriter wr = new OutputStreamWriter(clientSocket.getOutputStream());
            wr.write("FuBar!\r\n");
            // and flush!
            wr.flush();

            clientSocket.close();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

Without calling flush on that OutputStreamWriter I got the same results as you: Data got send successfully but received an empty response.

It does help in these cases to have both ends running in a debugger.

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

2 Comments

Interesting, I'm gonna check the one who developed the server side, hopefully this will solve my problem. I will accept your solution once it's confirmed. Thanks for your help.
Hi, thank you for your help, I finally found the correct solution :)

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.