e-i*_*128 1 c# sockets mono udp
我不明白,因为它给了我错误代码:
Socket socket = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Udp
);
Run Code Online (Sandbox Code Playgroud)
返回错误:
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)
Run Code Online (Sandbox Code Playgroud)
但我的系统支持UDP连接:
me@machine:~$ nc -u -v host.com 53
Connection to host.com 53 port [udp/domain] succeeded!
Run Code Online (Sandbox Code Playgroud)
我做错了吗?
使用SocketType.Streamfor UDP不起作用,因为UDP不是流协议(TCP是).你几乎肯定想使用SocketType.Dgram(数据报)UDP.UDP代表"用户数据报协议",因为它在功能上建立在数据报上.