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

      
    }

    public class UDPTest
    {

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


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

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

  
    }
}

 

posted on 2015-05-09 14:43  Q&A  阅读(5150)  评论(1编辑  收藏  举报