0
我在播放和接收UDP消息。 我有一台客户端和一台服务器,可以在我的机器上正常工作,但不能跨机器连接。使用UdpClient广播和接收消息
我的服务器发送消息,我的客户端收到它们。 我在两台机器上都转过了防火墙,这不成问题。
服务器看起来像:
var udpclient = new UdpClient();
IPAddress multicastAddress = IPAddress.Parse("239.0.0.222");
udpclient.JoinMulticastGroup(multicastAddress);
var endPoint = new IPEndPoint(multicastAddress, 2222);
while(true)
{
Byte[] buffer = Encoding.Unicode.GetBytes(Dns.GetHostName());
udpclient.Send(buffer, buffer.Length, endPoint);
Console.WriteLine("Broadcasting server hostname: {0}", Dns.GetHostName());
Thread.Sleep(3000);
}
而客户端的样子:
var client = new UdpClient { ExclusiveAddressUse = false };
var ipEndPoint = new IPEndPoint(IPAddress.Any, 2222);
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.ExclusiveAddressUse = false;
client.Client.Bind(ipEndPoint);
IPAddress multicastaddress = IPAddress.Parse("239.0.0.222");
client.JoinMulticastGroup(multicastaddress);
Byte[] data = client.Receive(ref ipEndPoint);
string strData = Encoding.Unicode.GetString(data);
Console.WriteLine("Received hostname {0} from the server", strData);
Console.WriteLine("I'm done. Press any key to close me.");
Console.ReadLine();
我觉得这个问题是不是在代码中,而是与网络相关的。 有关如何检查问题的任何想法?预先感谢您
尝试先做一个TCP连接,看看你是否可以让他们互相交谈。 UDP是无连接协议,不保证消息到达目的地。 –
我的想法是在不知道对方的情况下连接服务器和客户端。服务器通过Udp广播它的IP,客户端选择它并建立与服务器的tcp连接。但我可以尝试TCP,只是为了测试。谢谢回复。 – Dante