Transmitting Data with a Socket

Once you have a connection, you can send data synchronously with the socket's Send method:
clientSocket.Send(Encoding.UTF8.GetBytes("Hello World!"));

And you can receive data with the Receive method. The maximum number of bytes the Receive method reads is limited by the amount the specified buffer can hold. The method returns the actual number of bytes that were read:

byte[] inBuffer = new byte[100];
int count = clientSocket.Receive(inBuffer);
char[] chars = Encoding.UTF8.GetChars(inBuffer);
string str = new string(chars, 0, count);
Debug.Print(str);

You can poll the socket to test if data has been received. The first polling parameter specifies the amount of time, in microseconds, you want your application to wait for a response. Set it to –1 or System.Threading.Timeout.Infinite to let your application to wait an infinite amount of time:
if (communicationSocket.Poll(-1, //timeout in microseconds (-1 = infinite)
SelectMode.SelectRead))
{
...//read data here
}
Using the Available property of the Socket class, you can determine the number of bytes that are available to read before receiving them, which allows you to allocate a buffer that can hold all available bytes before reading the data.