UDP Broadcast recieve

D

Dave

I can't figure out why my code does not recieve the message. The only
message that it receives is the one going out from my machine. I am
suppose to send out a message to a server. Upon receiving that message
on the server side the server is suppose to send a message back. I
can't get my app to receive that message. It only receives the one
being sent out by my machine. I know that the server is sending out a
message because there is another app in vb that receives the message.
Here is my code:

private constructor()
{
iepSend = new IPEndPoint(IPAddress.Broadcast, UDP_PORT);
epSend = (EndPoint)iepSend;

iepRx = new IPEndPoint(IPAddress.Any, UDP_PORT);
epRx = (EndPoint)iepRx;

m_SocketUDP = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
m_SocketUDP.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
m_SocketUDP.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
m_SocketUDP.Connect(epSend);

udpRxSocket = new Socket(iepRx.AddressFamily, SocketType.Dgram,
ProtocolType.Udp);
udpRxSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
udpRxSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 5000);
udpRxSocket.Bind(iepRx);

SetupReceiveCallbackUDP()
}

private void SetupReceiveCallbackUDP()
{
AsyncCallback receiveData = new AsyncCallback(OnReceiveData);
udpRxSocket.BeginReceiveFrom(buffer, 0, buffer.Length,
SocketFlags.None, ref epRx, receiveData, udpRxSocket);
}

private void OnReceiveData(IAsyncResult ar)
{
Socket socket = (Socket)ar.AsyncState;

try
{
int nBytesRec = socket.EndReceiveFrom(ar, ref epRx);
if(nBytesRec > 0)
{
string Received = Encoding.ASCII.GetString(buffer, 0, nBytesRec);
//Received += socket.RemoteEndPoint.ToString();
WriteOutput("Received Data: " + Received);
SetupReceiveCallbackUDP();
}
}
catch(Exception e)
{
WriteOutput("Error. "+ e.Message);
}
}

Anyone know whats wrong?
 
J

Jani Järvinen [MVP]

Dave,
I can't figure out why my code does not recieve the message.

Unfortunately I don't have the time to review your code, but two things come
into mind architecture-wise: firewall settings and network errors.

Years ago I wrote a simple system to broadcast UDP messages to a local
subnet. This solution worked well when the 10 MB Ethernet network wasn't
busy, but when there were lots of traffic, those little UDP messages simply
were "missing in action". The same thing might happen to you.

Also, if it only happens that the server sees your client's messages but not
the other way around, check the (desktop)firewall settings on the server
(for outbound UDP blocking) and on your client (for incoming blocks).

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top