Socket Send and Receive [C#]
Socket Send and Receive [C#]
Socket Send and Receive [C#]
This example shows how to send and receive data viaTCP/IPusingSocketin .NET Framework. There are methodsSocket.SendandSocket.Receive.
Socket.Send method
Send method sends data from your buffer to a connected Socket. When you call the Send method it returns number of bytes which were „sent“. But it doesn‘t mean that the bytes were already received by the other side,it only means that the data were stored in a socket buffer and the socket will be trying to send them. If the socket buffer is full aWouldBlock
Following method sendssizebytes stored in thebufferfrom theoffsetposition. If the operation lasts more thantimeoutmilliseconds it throws an exception.
[C#]
public static void Send(Socket socket,byte[] buffer,int offset,int size,int timeout)
{
int startTickCount = Environment.TickCount;
int sent = 0; // how many bytes is already sent
do {
if (Environment.TickCount > startTickCount + timeout)
throw new Exception("Timeout.");
try {
sent += socket.Send(buffer,offset + sent,size - sent,SocketFlags.None);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.WouldBlock ||
ex.SocketErrorCode == SocketError.IOPending ||
ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
{
// socket buffer is probably full,wait and try again
Thread.Sleep(30);
}
else
throw ex; // any serious error occurr
}
} while (sent < size);
}
Tocall the Send methoduse following code snippet (suppose the static Send method is defined in MyClass class). TCP/IP socket can be obtained usingTcpClientclass. UseTcpClient.Clientproperty to get the underlying Socket (this property is public since .NET Framework2.0).
[C#]
Socket socket = tcpClient.Client;
string str = "Hello world!";
try
{ // sends the text with timeout 10s
MyClass.Send(socket,Encoding.UTF8.GetBytes(str),str.Length,10000);
}
catch (Exception ex) { /* ... */ }
Socket.Receive method
Receive method receives data from a bound Socket to your buffer. The method returns number of received bytes. If the socket buffer is empty aWouldBlockerror occurs. You should try to receive the datalater.
Following method tries to receivesizebytes into thebufferto theoffsetposition. If the operation lasts more thantimeoutmilliseconds it throws an exception.
[C#]
public static void Receive(Socket socket,int timeout)
{
int startTickCount = Environment.TickCount;
int received = 0; // how many bytes is already received
do {
if (Environment.TickCount > startTickCount + timeout)
throw new Exception("Timeout.");
try {
received += socket.Receive(buffer,offset + received,size - received,SocketFlags.None);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.WouldBlock ||
ex.SocketErrorCode == SocketError.IOPending ||
ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
{
// socket buffer is probably empty,wait and try again
Thread.Sleep(30);
}
else
throw ex; // any serious error occurr
}
} while (received < size);
}
Call the Receive methodusing code such this:
[C#]
Socket socket = tcpClient.Client;
byte[] buffer = new byte[12]; // length of the text "Hello world!"
try
{ // receive data with timeout 10s
MyClass.Receive(socket,buffer,buffer.Length,10000);
string str = Encoding.UTF8.GetString(buffer,buffer.Length);
}
catch (Exception ex) { /* ... */ }
See also
- [C#] Download Files from Web– how to download files from url to localdisk
- [C#] Check Local IP Address– check whether an IP islocal
- Socket– MSDN– provides many methods for network communications
- Socket.Send– MSDN– sends data to a socket
- Socket.Receive– MSDN– receives data from asocket
- SocketError– MSDN– enumeration with error codes for the Socket (WouldBlock)
- TcpClient– MSDN– provides client connections forTCP
- TcpClient.Client– MSDN– gets the underlyingSocket