Reputation: 5163
I'm looking at code, and I'm not certain of its functionality. I looked at documentation for the functions however they didn't do much justice. Here is the code:
private void m_InitUdpSock()
{
m_sockBroadcast = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
m_sockBroadcast.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast,1);
m_localhost = new IPEndPoint(IPAddress.Parse(m_localHostIP),BIND_PORT);
m_remotehost = new IPEndPoint(IPAddress.Broadcast,SCAN_PORT);
m_sockBroadcast.Bind(m_localhost);
m_sockBroadcast.BeginReceiveFrom(m_rxBuffer,0,m_rxBuffer.Length,SocketFlags.None,ref m_HostEp,new AsyncCallback(ReceiveBroadcastData),m_sockBroadcast);
}
From what I understand: this function is setting up a socket to broadcast a UDP message. It is using the m_localhost
to send out broadcast. The m_localhost
used to be:
m_localhost = new IPEndPoint(IPAddress.Any,BIND_PORT);
Then, I wanted to select the Network Adapter I chose to communicate with, so I retrieved IP address of the Network Adapter I wanted to use and changed the m_localhost
to the first example. Then using the Bind
function, it should use the specificed network adapter, and use the port, BIND_PORT
, to listen to respoonse messages.
I'm hoping someone could confirm I'm indeed using the network adapter of the IP I pass localhost.
The m_remotehost
is just a IPEndPoint
, which will hold an IP Address of a packet that responds, and the port, SCAN_PORT
, with be the outgoing broadcast port.
SCAN_PORT
and BIND_PORT
have comments from previous developer; however I don't know that they are accurate. Is m_remotehost specifying the port it should use to communicate back with me? (That sounds like bad practice to me)
SCAN_PORT // Outgoing broadcast port.
BIND_PORT // Incoming response listen port.
Next, in BeginReceiveFrom
, the documentation was clear for the most part, however the parameter the previous developer used "ref HostEp" threw me off, because it was declared as:
m_HostEp = new IPEndPoint(IPAddress.Any,0);
so when I call BeginReceiveFrom
, is it still using any local IP Address to broadcast messages, which is not what I want? m_HostEp
is also used in the call EndReceiveFrom(iar,ref m_HostEp)
.
I'm looking for clarification on:
1. BIND_PORT
and SCAN_PORT
2. What is m_HostEp
usage, how is it affecting my communication?
If you need any clarifications, ask!
Thanks in advance.
Upvotes: 0
Views: 105
Reputation: 26446
Looking at the code snippet, m_remotehost
is not used so you cannot determine the meaning of SCAN_PORT
from it, but apparently the broadcast messages can be sent on a different port than the one incoming messages are received on.
BIND_PORT
is the port you will receive messages on. m_HostEp
will contain the EndPoint
of the sender of the message you receive. When learning about sockets I read http://tangentsoft.net/wskfaq/ which turned out to be a great reference if you want to know more about the things "under the hood" as broadcast networking can be tricky. It isn't targetted specifically to c# though.
Upvotes: 1