Why does my client socket not receive what my server socket sends?

This is documented in the Receive() method, emphasis mine:

The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

When ignoring the return value, you will not know what portion of your buffer actually contains relevant data. Depending on the protocol being used, you may or may not know the content length in advance. Some protocols provide this length, others close the connection when done, yet another can use a message boundary.

You’ll have to hold the received data in another buffer, and return or output the entire message buffer when no more data is available or expected. This can be done like this:

int BufferSize = 1024;

using (var socket = new Socket(SocketType.Stream, ProtocolType.IP))
{
    socket.Connect(IPAddress.Parse("127.0.0.1"), 5000);

    byte[] buffer = new byte[BufferSize];
    string message = "";

    int bytesReceived;
    do
    {
        bytesReceived = socket.Receive(buffer);
        message += Encoding.ASCII.GetString(buffer, 0, bytesReceived);

    } while (bytesReceived > 0);

    Console.WriteLine(message);
}

The received bytes are ASCII characters (as defined in the made-up protocol), so each byte received indicates one character (you can’t convert partially received multibyte unicode characters). The bytes are converted to a string and appended to the message variable. The code loops until the server closes the connection.

When the message size is known on beforehand, again, depending on the protocol being used, you can create a message buffer and copy the data there on each Receive():

// Received using another Receive() call
int messageSize = 1234;
int totalBytesReceived = 0;
byte[] messageBuffer = new byte[messageSize];

byte[] buffer = new byte[BufferSize];

int bytesReceived;
do
{
    bytesReceived = socket.Receive(buffer);

    // Copy the receive buffer into the message buffer, appending after 
    // previously received data (totalBytesReceived).
    Buffer.BlockCopy(buffer, 0, messageBuffer, totalBytesReceived, bytesReceived);
    totalBytesReceived += bytesReceived;

} while (bytesReceived > 0);

// This assumes the connection wasn't closed prematurely.
Console.WriteLine(Encoding.ASCII.GetString(messageBuffer));

This can in turn be put in a resuable method:

public byte[] ReceiveMessage(Socket socket, int messageSize)
{
    byte[] messageBuffer = new byte[messageSize];

    int bytesReceived = 0;
    int totalBytesReceived = 0;
    do
    {
        byte[] buffer = new byte[BufferSize];

        // Receive at most the requested number of bytes, or the amount the 
        // buffer can hold, whichever is smaller.
        int toReceive = Math.Min(messageSize - totalBytesReceived, BufferSize);
        bytesReceived = socket.Receive(buffer, toReceive, SocketFlags.None);

        // Copy the receive buffer into the message buffer, appending after 
        // previously received data (totalBytesReceived).
        Buffer.BlockCopy(buffer, 0, messageBuffer, totalBytesReceived, bytesReceived);

        totalBytesReceived += bytesReceived;

    } while (bytesReceived > 0);

    if (totalBytesReceived < messageSize)
    {
        throw new Exception("Server closed connection prematurely");
    }

    return messageBuffer;
}

There’s the NetworkStream which wraps a socket, but it has the same reading issues as the socket itself. You will have to monitor the return value and keep calling Read() until you’ve received all bytes. Same goes for the TcpClient that has a GetStream() method, on whose return value you’ll also have to continue reading until you’ve read all data.

Leave a Comment