C# - How to listen on UPD port for a fixed IP address

  • Thread starter Thread starter zydjohn
  • Start date Start date
Z

zydjohn

Guest
I tried to follow Microsoft article to create my own Asynchronous Server Socket to listen on port 11000 for UPD data.

https://github.com/dotnet/docs/blob/master/docs/framework/network-programming/asynchronous-server-socket-example.md


The following is part of my code:

public static void StartListening()
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 11000);
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Udp);
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
while (true)
{
allDone.Reset();
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}



When it runs, it got run time error on this statement:

Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Udp);


The error message shows ProtocolType.Udp is not configured.

The physical server where I run the server socket program has a fixed IP address, i.e: 200.200.200.123; but it has no DNS name. I just want to listen to IP: 200.200.200.123 on Port 11000 for upd packets.

I don’t quite understand the errors.

Any suggestion on how to modify the code to suit my need?

Thanks,

Continue reading...
 
Back
Top