问题
i am not able to detect devices present in the network. There are two application I am using. One is written in C++ and another is windows phone 7 app. From Wp7 i am sending UDP broadcast that I referred from How to broadcast a UDP packet on WP7 Mango? I am able to broadcast message from wp7 and receiving in C++ app.But I am not able to receive UDP packet from C++ code. Here is my code. C# code
private void PageTitle_Tap(object sender, GestureEventArgs e)
{
var ipAddress = IPAddress.Parse("255.255.255.255");
var endpoint = new IPEndPoint(ipAddress, 4998);
byte[] data = Encoding.UTF8.GetBytes("hello|4998");
var args = new SocketAsyncEventArgs();
args.RemoteEndPoint = endpoint;
args.SetBuffer(data, 0, data.Length);
args.Completed += new EventHandler<SocketAsyncEventArgs>(args_Completed);
socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
bool result = socket.ConnectAsync(args);
}
void args_Completed(object sender, SocketAsyncEventArgs e)
{
if (e.SocketError != SocketError.Success)
{
return;
}
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
bool res = e.ConnectSocket.ReceiveFromAsync(e);
break;
case SocketAsyncOperation.Send:
break;
case SocketAsyncOperation.Receive:
break;
}
}
C++ code
ServerAddress.sin_family= AF_INET;
ServerAddress.sin_addr.s_addr= INADDR_ANY;
ServerAddress.sin_port = htons (PORT_DESKTOP);
ServerSocket = socket (PF_INET, SOCK_DGRAM, 0);
sendto (ServerSocket, Buffer, sizeof (Buffer), 0, (LPSOCKADDR) &ClientAddress, Length);
I have also used creating another socket using IPADDRESS.Any to receive the message.But I am not able to receive response from c++ code.
Thanks in advance.
回答1:
Do you miss enabling SO_BROADCAST?
SOL_SOCKET Socket Options:
SO_BROADCAST yes yes DWORD (boolean) Configure a socket for sending broadcast data. This option is only Valid for protocols that support broadcasting (IPX and UDP, for example).
来源:https://stackoverflow.com/questions/8574623/device-discovery-in-windows-phone-7