我在接收客户端上的消息时遇到了一些问题:
基本上,我能够向侦听器发送消息“Hello world”,但是当侦听器尝试发送“Reply”时,客户端没有收到消息?
你认为我在这里失踪了什么?
客户端:
var socket = new DatagramSocket();
socket.MessageReceived += HandleMessageReceive;
await socket.ConnectAsync(new Windows.Networking.HostName("224.192.12.12"), "9991");
socket.JoinMulticastGroup(_multicastGroupIp);
DataWriter writer = new DataWriter(_socket.OutputStream);
writer.WriteString("Hello world");
await writer.StoreAsync();
听众:
var stateObject = new StateObject();
stateObject.LocalEndPoint = new IPEndPoint(IPAddress.Any, 9991);
stateObject.SocketClient = new UdpClient();
stateObject.SocketClient.Client.Bind(stateObject.LocalEndPoint);
stateObject.SocketClient.JoinMulticastGroup(IPAddress.Parse("224.192.12.12"));
stateObject.SocketClient.BeginReceive(new AsyncCallback(HandleDiscoveryServiceReceive), stateObject);
private static void HandleDiscoveryServiceReceive(IAsyncResult ar)
{
var stateObject = (StateObject)ar.AsyncState;
IPEndPoint localEndPoint = stateObject.LocalEndPoint;
byte[] data = stateObject.SocketClient.EndReceive(ar, ref localEndPoint);
string decodedData = Encoding.UTF8.GetString(data);
System.Console.WriteLine("Got something back: {0}", decodedData);
UdpClient client = stateObject.SocketClient;
client.MulticastLoopback = false;
byte[] msg = Encoding.UTF8.GetBytes("REPLY");
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("224.192.12.12"), 9991);
client.Send(msg, msg.Length, remoteEndPoint);
client.BeginReceive(HandleDiscoveryServiceReceive, stateObject);
}
答案 0 :(得分:0)
您必须将回复发送到错误的地址:port。你很难接线:为什么?远程地址:端口由接收提供。使用它。