一个合作伙伴说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();
            }
        }

}
}

UDP发送数据测试的更多相关文章

  1. 通过 UDP 发送数据的简单范例

    package j2se.core.net.udp; import java.io.IOException;import java.net.DatagramPacket;import java.net ...

  2. c/c++ 网络编程 UDP 发送端 bind 作用

    网络编程 UDP 发送端 bind 作用 upd 发送端 调用bind函数的效果:把socket特定到一个指定的端口,如果不调用bind,内核会随机分配一个端口. upd 发送端 调用bind函数的目 ...

  3. Linux 下V4l2摄像头采集图片,实现yuyv转RGB,RGB转BMP,RGB伸缩,jpeglib 库实现压缩RGB到内存中,JPEG经UDP发送功(转)

    ./configure CC=arm-linux-gnueabihf-gcc LD=arm-linux-gnueabihf-ld --host=arm-linux --prefix=/usr/loca ...

  4. Java使用UDP发送数据到InfluxDB

    最近在做压测引擎相关的开发,需要将聚合数据发送到InfluxDB保存以便实时分析和控制QPS. 下面介绍对InfluxDB的使用. 什么是InfluxDB InfluxDB是一款用Go语言编写的开源分 ...

  5. UDP发送的数据 以数据包形式发送

    UDP发送的数据 以数据包形式发送

  6. 关于提高UDP发送效率的方法

    UDP的发送效率和什么因素有关呢? 直观觉得,UDP的切包长越大,应该发送效率越高(最长为65536).可是依据实际測试和在网上查到的资料的结果,包长度为1024为发送效率最高. 这样的结果让人感到疑 ...

  7. C# UDP发送和接收

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...

  8. Learn day8 re正则表达式\search函数\反射\tcp发送消息(循环)\udp发送消息

    1.匹配单个字符 # ### 正则表达式 - 单个字符匹配 import re ''' findall 把匹配的结果直接返回到列表中 lst = re.findall("正则表达式" ...

  9. UDP发送和接收

    发送函数 public bool udpSend(string ip, int port, byte[] data) { Socket socket = new Socket(AddressFamil ...

随机推荐

  1. MySQL数据库管理用户权限

    http://blog.itpub.net/7607759/viewspace-675079/ 2.2 授予权限 前面提到了grant命令,grant的语法看起来可是相当复杂的呐: GRANT pri ...

  2. iOS开发拓展篇—CoreLocation定位服务

    iOS开发拓展篇—CoreLocation定位服务 一.简单说明 1.CLLocationManager CLLocationManager的常用操作和属性 开始用户定位- (void)startUp ...

  3. HTTP 超时

    TWinHTTPTimeouts = class(TPersistent) private FConnectTimeout, FReceiveTimeout, FSendTimeout: DWord; ...

  4. ViewHolder简洁写法

    ViewHolder holder = null;         if(convertView == null){                 convertView = mInflater.i ...

  5. HDU2222

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 注意: 1. keyword可以相同,因此计算时要累计:cur->num++. 2. 同一个keyw ...

  6. Nodejs连接mysql

    1.首先需要安装nodejs 的mysql包 npm install mysql 2.编写nodejs与mysql交互的代码 var mysql = require('mysql'); var TES ...

  7. 启动运行下载gradle速度太慢,手动添加

    启动运行下载gradle速度太慢,并且容易卡死(感谢群友ˋ狠ㄨ得意提供支持)---国内网络访问地址 我们经常运行项目的时候会需要进行下载gradle,不过由于网络或者和谐的问题经常下载需要花很长时间或 ...

  8. Linux命令行与图形界面切换方法

    1.实时切换 1.1 命令行->图形 startx 1.2 图形->命令行 Ctrl+Alt+F1--F6 2.启动默认 2.1 启动进入命令行 修改/etc/inittab文件 &quo ...

  9. selinium的ruby版在windows8下安装

    1.安装ruby,当然windows下是最简单的装一个rubyinstaller,官网有.一步到位 2.因为selinium官网被墙,所以改成淘宝的一个资源库 gem sources --remove ...

  10. codeforces mysterious present 最长上升子序列+倒序打印路径

    link:http://codeforces.com/problemset/problem/4/D #include <iostream> #include <cstdio> ...