using System; using System.Text; using System.Net; using System.Net.Sockets; namespace socket { /// /// Summary description for Class1. /// class socket { [STAThread] static void Main(string[] args) { try { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); s.Blocking = true; s.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 50000)); Console.WriteLine("s.Blocking = " + s.Blocking); Console.WriteLine("s.ReceiveTimeout = " + s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout)); byte[] buffer = new byte[1024]; int count = s.Receive(buffer); if (count > 0) { Console.Write("Received: " + Encoding.ASCII.GetString(buffer, 0, count)); } else { Console.WriteLine("Received nothing."); } s.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } } }