Dual-NIC transceiver for sockets (C #)

Source: Internet
Author: User

A recent project requires the simultaneous use of two NICs to send and receive UDP multicast packets, and a socket is required for receiving and sending network packets (I will not tell you that I used to use Sharppcap directly). In C # Socket contact is relatively early, but not much, especially in the implementation of the network card transmission and delivery process has encountered a lot of trouble. One of the most headache is not at the same time receive two net card data, although this problem is not fatal (big deal with Sharppcap Bai!!) ), but the most ambitious young people of the 21st century, how to do this dabbler thing! So, these two days I fell into this problem can not extricate themselves, of course, finally solved! Ha ~ ha ~ hahaha ~ ~

In the moment of solving the problem, there suddenly appeared a figure-"Ah ~ ~ Industrious (yard) Farmer Ah ~ ~, I ask you to lose the gold axe, or this ...", "Gold axe!" Gold Axe! "(Hey, you can fry the stock if you sell it!) Make a lot of money! Served as ceo! Marry Formica beauty! To the pinnacle of life! )

Ah ~ ~ ~ Yuck! Bah! Bah! In fact, I would like to say, conscience found I still think if there is a group of people still struggling with this problem, then look here, may be helpful. (Really!) Definitely not for X.)

Problem overview

In my application, I mainly want to use C # socket to receive multicast UDP packets. Of course, send also need, but not my most concern of the problem. So, the first thing I think about is the UdpClient class. This class has a very good encapsulation of sockets and is easier to use. So the problem comes, I always can only receive a network card on the data, which is very headache. I checked the network and checked the packet discovery, but only received data from one of the NICs.

Here is the code for my init socket, which is a paragraph in the Capdevice class:

//define IPEndPointPrivateIPEndPoint localEP =NULL;PrivateIPEndPoint remoteEP =NULL;//define UDP send and receive sockets//private Socket udpreceive = null;PrivateUdpClient udpreceive =NULL;PrivateUdpClient udpsend =NULL;//Native NodelocalEP=NewIPEndPoint (device, local_port);//Remote NoderemoteEP=NewIPEndPoint (Ipaddress.parse (MASTER_IP), des_port);//instantiation ofudpreceive=NewUdpClient (addressfamily.internetwork); UdpReceive.Client.ReceiveBufferSize=320000; UdpReceive.Client.Bind (LOCALEP); UdpReceive.Client.SetSocketOption (Socketoptionlevel.socket, Socketoptionname.reuseaddress,true); Udpsend=Newudpclient ();//send and receive join a multicast groupUdpreceive.joinmulticastgroup (Ipaddress.parse (Master_tx_group));
Udpsend.joinmulticastgroup (Ipaddress.parse (Master_rx_group)); // open send flag bit  true; // open a Receive thread  New Thread (new=true;

During use, I instantiated two Capdevice objects separately and passed in the required IP and port information through the parameters.

Device is the IP address of the native network card;

Local_port is the native listening port;

MASTER_IP is the IP address of the remote device to be received;

Des_port is the port of the remote device to be received;

Master_tx_group is the group that receives data binding;

Master_rx_group is a group that sends data bindings;

I use desktop development, the motherboard comes with a gigabit network card, and then add a PCI interface Gigabit network card. In the process of debugging and looking for problems, I encountered this situation:

1. Two network card IP is 192.168.30.33 and 192.168.30.34,33 the main board of the card, 34 is the external network card. After the configuration as described above, I found that I can only receive 33 of the data on the network card, unless I unplug the network cable on the 33 NIC, and re-open the receive, at this time, 34 of the network card will have data.

2. I tried to use synchronous, asynchronous methods, and tried to use the socket class instead of the UdpClient class, but none succeeded.

Therefore, I suspect that even though my socket is listening on the IP is 192.168.30.34, the actual Windows still does not listen to 34 of the data of this net card.

Problem solving

I spent a lot of time looking for answers, but the results were not right. Eventually I found a website that told me the same question:

Http://stackoverflow.com/questions/15265620/udp-read-data-from-all-network-interfaces

The person who raised the question also encountered the same problem and made a lot of attempts. In the end he got the conclusion: "The synchronous reception is changed to receive asynchronously!" ”

I tried it and found the problem was not there, or not. But I found a place where the Joinmulticastgroup function they used when they joined the group had two parameters, and then I changed:

Udpreceive.joinmulticastgroup (Ipaddress.parse (Master_tx_group), localep.address);

Well, actually see so much, I want to say this, really embarrassed, wasting everybody so much time to see nonsense, Hee-ha!!

The second parameter is the local address, really can let my second NIC can also receive data is also this parameter, it seems that the second parameter is the notification system to connect IP with the local network card. Tested, asynchronous receive and synchronous receive are no problem.

Summarize

In a nutshell, joining a multicast group requires using an overloaded function with a local IP address. Of course, to write so much is to describe the problem clearly, the same problem can be solved as described here. If the socket communication fails for other reasons, it is not necessarily possible to resolve it as described in this article.

Here is the initialization code (the difference is in the highlighted section of the code):

//define IPEndPointPrivateIPEndPoint localEP =NULL;PrivateIPEndPoint remoteEP =NULL;//define UDP send and receive sockets//private Socket udpreceive = null;PrivateUdpClient udpreceive =NULL;PrivateUdpClient udpsend =NULL;//Native NodelocalEP=NewIPEndPoint (device, local_port);//Remote NoderemoteEP=NewIPEndPoint (Ipaddress.parse (MASTER_IP), des_port);//instantiation ofudpreceive=NewUdpClient (addressfamily.internetwork); UdpReceive.Client.ReceiveBufferSize=320000; UdpReceive.Client.Bind (LOCALEP); UdpReceive.Client.SetSocketOption (Socketoptionlevel.socket, Socketoptionname.reuseaddress,true); Udpsend=Newudpclient ();//send and receive join a multicast groupUdpreceive.joinmulticastgroup (Ipaddress.parse (Master_tx_group), localep.address);
Udpsend.joinmulticastgroup (Ipaddress.parse (Master_rx_group)); // open send flag bit  true; // open a Receive thread  New Thread (new=true;

Here, by the way, I'll get my sync. The receive thread function is also posted:

Private void Receiveloop () {    byte[] rcvdata;      while (IsOpen)    {        = udpreceive.receive (ref  remoteep);         //  parse data          uploadendecoder.capturepkgm (rcvdata);    }    Udpreceive.close ();    Udpsend.close ();}

Dual-NIC transceiver for sockets (C #)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.