UDP Send data test

Source: Internet
Author: User

A partner says UDP sends data, a (ip:192.168.1.100 subnet mask 255.255.255.0) segment can send data to a B segment, but B (ip:192.168.2.100 subnet mask 255.255.255.0) network segment cannot send data to a segment , the argument is that in the case of cross-routing, the data can only be sent from the lower layer, but not from the upper layer. I think the status of two network segments should be equal, even if the cross-routing situation, there should be a route map can let the two network segments can ping each other, and as long as two network segments can be ping, you can send data with the UPD (of course, we say the premise is in a company's LAN), and send the data should have no upper network segment and the lower network segment this iffy concept! (Alas, the Internet does not know how to kill people!) ), the test program is created to see if any two can ping the network segment can send and receive data.

The source code is as follows:

Namespace Clint2
{
Class Program
{
static void Main (string[] args)
{
Console.WriteLine ("Please set IP address:");
String ip = Console.ReadLine ();
Udptest udptest = new Udptest (IP);

Console.WriteLine ("Please enter the data to be sent");
String key = Console.ReadLine ();
while (Key! = "OK")
{
Udptest.addqueue (key);
Console.WriteLine ("Please enter the data to be sent");
Key = Console.ReadLine ();
}
Udptest.stopsend ();
}


}

public class Udptest
{

queue<string> _queue = new queue<string> ();
UdpClient clintreceive = new UdpClient (8801); Receive
UdpClient clintsend = new UdpClient (); Send
IPEndPoint Romoteip;
BOOL _isstartsend;
Thread Threadsend;


Public udptest (String IP)
{
Romoteip = new IPEndPoint (Ipaddress.parse (IP), 8801);

Start Send Thread
_isstartsend = true;
Threadsend = new Thread (new ThreadStart (Send));
Threadsend.isbackground = true;
Threadsend.start ();

Start receiving data
Receive ();
}

public void Stopsend ()
{
_isstartsend = false;

            clintreceive.close ();
            clintsend.close ();
       }

        public void Receive ()
        {
            clintreceive.beginreceive ( ReceiveCallback, clintreceive);
       }
        public void ReceiveCallback (IAsyncResult ar)
         {
            ipendpoint TMPROMEIP = null;
            udpclient cr = ar. AsyncState as UdpClient;
            byte[] Receivedata = cr. EndReceive (AR, ref TMPROMEIP);

String str = Encoding.ASCII.GetString (receivedata);
Console.WriteLine (str + "from IP:" + tmpRomeIP.Address.ToString () +
"Port:" + tmpRomeIP.Port.ToString ()
);

Receive ();
}

        public void Send ()
        {
            Clintsend.connect (ROMOTEIP);
            while (_isstartsend)
             {
                 while (_queue. Count > 0 && _isstartsend)
                 {
                     string strvalue = DeQueue ();
                     byte[] SendData = Encoding.ASCII.GetBytes (strvalue);

Clintsend.send (SendData, Senddata.length, ROMOTEIP);

Clintsend.beginsend (SendData, Senddata.length, Sendcallback, clintsend);
}
Thread.Sleep (500);
}
}
public void Sendcallback (IAsyncResult ar)
{
UdpClient cr = Ar. AsyncState as UdpClient;
Cr. EndSend (AR);
}

public void Addqueue (String str)
{
Lock (This)
{
_queue. Enqueue (str);
}
}
public string DeQueue ()
{
Lock (This)
{
Return _queue. Dequeue ();
}
}


}
}

UDP Send data test

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.