C# udp can not receive any data
Posted
by StoneHeart
on Stack Overflow
See other posts from Stack Overflow
or by StoneHeart
Published on 2010-04-17T02:53:10Z
Indexed on
2010/04/17
3:03 UTC
Read the original article
Hit count: 328
here is my code
Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.Bind(new IPEndPoint(IPAddress.Any, 0));
// Broadcast to find server
string msg = "Imlookingforaserver:" + udp_listen_port;
byte[] sendBytes4 = Encoding.ASCII.GetBytes(msg);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Parse("255.255.255.255"), server_port);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
sck.SendTo(sendBytes4, groupEP);
//Wait response from server
Socket sck2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck2.Bind(new IPEndPoint(IPAddress.Any, udp_listen_port));
byte[] buffer = new byte[128];
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, udp_listen_port);
sck2.ReceiveFrom(buffer, ref remoteEndPoint); //<<< I never pass this line
I use above code to try find a server. First i broadcast a message and then i wait response from server.
A test i made with the server written in c++ and running in windows vista, client written in C# and run on the same machine with server.
Problem is: The server can receive message which client broadcast. But client can not receive anything from server.
I try to write a client with c++ and it work like a charm, i think my problem is in C# client.
© Stack Overflow or respective owner