我有一些网络代码连接到多播地址,但几秒钟后断开连接。谁能弄清楚这段代码有什么问题?
String Target_IP = "224.1.2.3";
int Target_Port = 31337;
IPEndPoint LocalEP = new IPEndPoint(IPAddress.Any, Target_Port);
IPEndPoint RemoteEP = new IPEndPoint(IPAddress.Parse(Target_IP), Target_Port);
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
//s.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoDelay, 1);
//s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
s.Bind(LocalEP);
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 0);
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(Target_IP)));
s.Connect(RemoteEP);
// TODO
}
调用 Connect() 函数后,它报告为已连接,但等待一两秒钟,它就会断开连接。我绑定到错误的端口还是什么?每个在线教程似乎都以不同的方式进行。