Multiple Ethernet ports access through Sockets.

  • Thread starter Thread starter christian
  • Start date Start date
C

christian

Hi.

I use the System.Net.Socket and c# in order to implement UDP
communication. Everything has gone alright until now, that I want to
use the multiple Ethernet Ports on my pc.

The exception "Only one usage of each socket address (protocol/network
address/port) is normally permitted" is thrown when I try to bind the
second IPEndPoint. I do not think (although I'm not sure) that this
error would occur if the framework tried to access different Ethernet
ports.

Does any one recognize this problem? What shall I do in order to
utilize both my Ports?

If I posted this in the wrong group I'm sorry. If that is the case I
would be most grateful for a nudge in the direction of a better place
for my question.

Best regards,
Christian Sternell
 
Hi,
can you post any code? It seems like the socket is already binded to
the second endpoint.
You can use IPAddress.Any to bind to all addresses at once.
 
Hello Juan - Thanks for the answer.

Here is some code.

My instantiation looks like this:

ReceiveEndPointAddress = new
IPEndPoint((uint)IPAddress.Parse(this.RemoteAddress).Address,
this.TransmitPort);
TransmitEndPointAddress = new
IPEndPoint((uint)IPAddress.Parse(this.LocalAddress).Address,
this.TransmitPort);
try
{
UDPTransmitSocket = new
Socket(TransmitEndPointAddress.Address.AddressFamily,
SocketType.Dgram, ProtocolType.Udp);
}


And the receive-thread looks like this:

ReceiveEndPointAddress = new
IPEndPoint((uint)IPAddress.Parse(LocalAddress).Address, ReceivePort);
UDPReceiveSocket = new
Socket(ReceiveEndPointAddress.Address.AddressFamily, SocketType.Dgram,
ProtocolType.Udp);

UDPReceiveSocket.Bind(new IPEndPoint(IPAddress.Any, ReceivePort));

while(true)
{
receivedBytes = UDPReceiveSocket.Receive(ReceiveBuffer,0,ReceiveBuffer.Length,SocketFlags.None);
resultBuffer = new byte[receivedBytes];
Array.Copy(ReceiveBuffer,resultBuffer,receivedBytes);

try
{
EV_THREADGOTDATA(receivedBytes, resultBuffer);
}
catch (Exception e)
{
}
}

And I send like this:

UDPTransmitSocket.BeginSendTo(TransmitBuffer,0,TransmitBuffer.Length,System.Net.Sockets.SocketFlags.None,ReceiveEndPointAddress,
new AsyncCallback(this.TransmitDataFinish), UDPTransmitSocket);


As I said, the stuff works until I try to make two instances of this
driver (one for each Ethernet port). When I do that, the first
connection still works, but the second driver tries to attach to the
same Ethernet port as the first.

I have attached a sniffer , and can se that on port 1 I send the data
supposed to be put out on port 1, but also a lot of arp requests, that
wants to know who has the ip-adress that i want to send to on port 2.
Port two on the other hand is completely dead – nothing happens there.

How do I bind my second connection to the other Ethernet port?

As for the question about that exception thrown – it is fixed and had
nothing to do with the problem at hand. I had used the same
port-numbers – a bad idea.

If anybody has an idea about how to setup this lan-stuff - please drop
a line.

Best Regards,
Christian Sternell
 
Hi

I don't understand your problem well - do you mean you have two network
adapters and want to attach to each of them? If so, there should be no
problem at all - you should call the Bind() method with the address of the
respective adapter. Using IPAddress.Any will bind the socket to all of your
adapters.

Hope this helps,
Stefan
 
Back
Top