I am calling (System.Net.Sockets) Socket.Receive. It returns that it read 13 bytes, which is the expected data length. However, the receive buffer I pass into the function does not become populated with the expected bytes. All of the bytes in the buffer are zero.
I simply don't understand how this could be happening - I thought Receive is supposed to block until data becomes available, and that it should put the data it read into the buffer.
Simple as it is, here is my code:
bytesRead = Socket.Receive(RecvBuffer.Buffer, offset, RecvBuffer.Buffer.Length - offset, SocketFlags.None, out SocketError error);
bytesRead = 13, and RecvBuffer.Buffer is all zeros.
On the sending side, it's not writing all zeros, as far as I can tell. I haven't found any tool to allow me to check what's on the wire.
I'm at a loss. Any help is appreciated.
CodePudding user response:
You have to understand all possible scenarios. We will not consider chat application here where asynchronously both side can send simultaneously. Best way of understanding serial communications is an application using a scale to send weights. There are two different operating modes. One which is continuous where client sends a message to start weighing and the server constantly sends weights. Again this is probably not what you are doing. The second mode with a scale is a master/slave mode where a client request one weight and server sends the weight. The weight may not come in one chunk so client has to loop until the entire weight is received which is usually and end character or a byte count at beginning of the weight. There is no blocking. Windows uses timers and periodically sends the data in the UART is to the application (Socket.Receive). The number of bytes is random and less or equal to the entire data being received. You should be disabling hardware and software handshaking so you do not get null data. You also need to send a message to receive a message and not send until you receive a full block of data which may take multiple Socket.Receive() commands until you reach the end of data (a terminating character or byte count).
CodePudding user response:
The problem here was the sending side. I got fooled by other messages working correctly, but in this particular case, the send buffer was never populated with the data. So none was received :).
Thanks for the comments and suggestions.