简体   繁体   中英

Sending data to server with NetworkStream

My assignment is to make a client that sends a ToString-override method to the server. My teacher sent me a Centralcomputer.exe which is the serverprogram, this program can recieve single strings and uses my local IP.

I connect without a problem, the problem comes when trying to execute this line Int32 bytesRead = stream.Read(bytesToRead, 0, klient.ReceiveBufferSize); my client freezes and the value of bytesRead is 0.

EDIT: Forgot to mention that the serverprogram responds with some "oriental" letters before client freezes.

 TcpClient client = new TcpClient();

 public async Task Connecting()
    {
        try
        {

            await client.ConnectAsync("127.0.0.1", 12345);

        }
        catch (ArgumentNullException e)
        {
            MessageBox.Show("ArgumentNullException: {0}" + e);
        }
        catch (SocketException e)
        {
            MessageBox.Show("SocketException: {0}" + e);
        }


  public void Sending()
    {
        string sendToServer = "Testing testing";
        byte[] data = System.Text.Encoding.ASCII.GetBytes(sendToServer);

        NetworkStream stream = client.GetStream();

        stream.Write(data, 0, data.Length);

        byte[] bytesToRead = new byte[client.ReceiveBufferSize];

        Int32 bytesRead = stream.Read(bytesToRead, 0, client.ReceiveBufferSize);    
        //This is where the client freezes

        MessageBox.Show("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
    }

I've never done this type of coding before so I really would appreciate some help just to get started!

There are only two scenarios when you will receive zero here:

  • a zero-length read with a zero-length buffer can be used to detect when data becomes available
  • the inbound socket has been closed

Assuming you didn't request a zero-length read, we can therefore assume the second option, and the socket closed. So: why did it close? Most likely, because the server gave up waiting for a message to reply to. Which suggests that it doesn't think you sent a complete message.

That in turn means one of:

  • whatever you sent was not complete by the protocol's definition as expected by the server
  • the data is still buffered and didn't send

To rule out the second, make sure you call Flush() after the Write . If that still doesn't fix it: check the protocol very carefully to see what you were meant to send. Maybe you missed a trailing linefeed or similar.

As a side note: any time you're doing a single Read on a socket, you've probably made a mistake. TCP isn't message oriented - it is just a stream. This means you always need a read-loop that handles whatever framing protocol is in use.

It is possible that your server expects a carriage return or/and a line feed before writing something back to the client.

Try to change the following line

string sendToServer = "Testing testing";

into

string sendToServer = "Testing testing \r\n";

Since we do not have access to the server's code, there is pretty hard to give you an exact solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM