zoukankan      html  css  js  c++  java
  • 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();
                }
            }

      
        }
    }

  • 相关阅读:
    VC6.0 error LNK2001: unresolved external symbol _main解决办法
    C++中数字与字符串之间的转换(使用CString.Format或者sprintf)
    C++内存管理(超长)
    Flash, Flex, Air, Flashplayer之间的相互关系是什么?
    《KVM虚拟化技术实战和原理解析》读书笔记(十几篇)
    HNCU1323:算法2-1:集合union (线性表)
    Delphi XE7下如何创建一个Android模拟器调试
    DelphiXE Android的所有权限按照分类总结说明
    DelphiXE8怎么使用调试模式(朱建强)
    C++静态库中使用_declspec(dllexport) 不能导出函数的问题
  • 原文地址:https://www.cnblogs.com/movemoon/p/4489909.html
Copyright © 2011-2022 走看看