单声道不支持UDP协议?

时间:2015-07-17 17:13:05

标签: c# sockets mono udp

我不明白,因为它给了我错误代码:

Socket socket = new Socket(
    AddressFamily.InterNetwork, 
    SocketType.Stream, 
    ProtocolType.Udp
);

返回错误:

me@machine:~/Demo/bin/Debug$ mono Demo.exe
Marshaling clicked signal
Exception in Gtk# callback delegate
  Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.Sockets.SocketException: Protocol not supported
  at System.Net.Sockets.Socket..ctor (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) [0x00000] in <filename unknown>:0 
  ...
   at Gtk.Application.gtk_main()
   at Gtk.Application.Run()
   at Demo.MainClass.Main(System.String[] args)

但我的系统支持UDP连接:

me@machine:~$ nc -u -v host.com 53
Connection to host.com 53 port [udp/domain] succeeded!

我做错了吗?

1 个答案:

答案 0 :(得分:5)

对UDP使用SocketType.Stream不起作用,因为UDP不是流协议(TCP是)。您几乎肯定希望对UDP使用SocketType.Dgram(数据报)。 UDP代表“用户数据报协议”,因为它在功能上建立在数据报上。