Mega Code Archive

 
Categories / C# Tutorial / Network
 

Udp time out option

using System; using System.Net; using System.Net.Sockets; using System.Text; class MainClass {    public static void Main()    {       byte[] data = new byte[1024];       string input, stringData;       int receivedDataLength;       IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);       Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);       int sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);       Console.WriteLine("Default timeout: {0}", sockopt);       server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);       sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);       Console.WriteLine("New timeout: {0}", sockopt);       server.Close();    } }