UDP发送数据测试

     一个合做伙伴说UDP发送数据,A(IP:192.168.1.100 子网掩码255.255.255.0)网段能发数据到B网段,但B(IP:192.168.2.100 子网掩码255.255.255.0)网段不能发数据到A网段,说法是跨路由的状况下,数据只能从下层住上层发,而不能由上层住下层发。我以为两个网段的地位应该是相等的,即便跨路由的状况下,也应该有路由映射能够让这两个网段相互能够ping通,而只要两个网段能够ping通,就能够用upd发送数据 (固然,咱们说的前提都是在一个公司的局域网内),而发送数据应该没有什么上层网段和下层网段这个玄乎的概念了吧!(哎,网络不懂害死人呀!),因而,这个测试程序就产生了,目的就是看看在任意两个能够ping通的网段里是否能够收发数据。网络

 

源码以下:测试

namespace Clint2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请设置IP地址:");
            string ip = Console.ReadLine();
            UDPTest udpTest = new UDPTest(ip);
        
            Console.WriteLine("请输入要发送的数据");
            string key = Console.ReadLine();
            while (key != "ok")
            {
                udpTest.AddQueue(key);
                Console.WriteLine("请输入要发送的数据");
                key = Console.ReadLine();
            }
            udpTest.StopSend();
        }this

      
    }spa

    public class UDPTest
    {线程

        Queue<string> _queue = new Queue<string>();
        UdpClient clintReceive = new UdpClient(8801); //接收
        UdpClient clintSend = new UdpClient();  //发送
        IPEndPoint romoteIP;
        bool _isStartSend;
        Thread threadSend;ip


        public UDPTest(string ip)
        {
            romoteIP = new IPEndPoint(IPAddress.Parse(ip), 8801);路由

            //启动发送线程
            _isStartSend = true;
            threadSend = new Thread(new ThreadStart(Send));
            threadSend.IsBackground = true;
            threadSend.Start();源码

            //开始接收数据
            Receive();
        }string

        public void StopSend()
        {
            _isStartSend = false;it

            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();
            }
        }

       }}

相关文章
相关标签/搜索