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();
}
}
}
}
UDP发送数据测试的更多相关文章
- 通过 UDP 发送数据的简单范例
package j2se.core.net.udp; import java.io.IOException;import java.net.DatagramPacket;import java.net ...
- c/c++ 网络编程 UDP 发送端 bind 作用
网络编程 UDP 发送端 bind 作用 upd 发送端 调用bind函数的效果:把socket特定到一个指定的端口,如果不调用bind,内核会随机分配一个端口. upd 发送端 调用bind函数的目 ...
- 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 ...
- Java使用UDP发送数据到InfluxDB
最近在做压测引擎相关的开发,需要将聚合数据发送到InfluxDB保存以便实时分析和控制QPS. 下面介绍对InfluxDB的使用. 什么是InfluxDB InfluxDB是一款用Go语言编写的开源分 ...
- UDP发送的数据 以数据包形式发送
UDP发送的数据 以数据包形式发送
- 关于提高UDP发送效率的方法
UDP的发送效率和什么因素有关呢? 直观觉得,UDP的切包长越大,应该发送效率越高(最长为65536).可是依据实际測试和在网上查到的资料的结果,包长度为1024为发送效率最高. 这样的结果让人感到疑 ...
- C# UDP发送和接收
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...
- Learn day8 re正则表达式\search函数\反射\tcp发送消息(循环)\udp发送消息
1.匹配单个字符 # ### 正则表达式 - 单个字符匹配 import re ''' findall 把匹配的结果直接返回到列表中 lst = re.findall("正则表达式" ...
- UDP发送和接收
发送函数 public bool udpSend(string ip, int port, byte[] data) { Socket socket = new Socket(AddressFamil ...
随机推荐
- String类和StringBuffer类的方法
一.String类的方法 public char charAt(int index) 返回字符串index个字符 public int length() 返回字符串长 ...
- 坑爹的属性,android:descendantFocusability用法简析
开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承 BaseAdapter,在adapter中按照需求进行编写,问题就出现 ...
- 读javascript高级程序设计01-基本概念、数据类型、函数
一. javascript构成 1.javascript实现由三部分组成: ECMAScript:核心语言功能 DOM:文档对象模型,提供访问和操作网页内容的方法和接口 BOM:浏览器对象模型,提供与 ...
- Web开发学习笔记
EnumUtils.isValidEnum(JnRouteProViewStateEnum.class, status) : 判断 status是否在 枚举集合中. MapUtils.isNotEmp ...
- SSH框架构建微信公众帐号服务器小技巧
SSH框架构建微信公众帐号服务器小技巧 熟悉struts2和servlet的同学应该清楚,struts2的方法多样性弥补了servlet单一的doGet 和doPost方法.如果自己的公众账号服务器是 ...
- yarn 0.9.0 build spark
1. 下载scala并安装.版本为2.10.3.设置SCALA_HOME和PATH环境变量 2. 下载SPARK 0.9.0源代码并解压到/root/Downloads/spark-0.9.0-inc ...
- realestate.cei.gov.cn
using AnfleCrawler.Common; using System; using System.Collections.Concurrent; using System.Collectio ...
- YII2.0 验证表单
控制器代码 <?php namespace app\modules\pub\controllers; use Yii; use backend\base\BaseController; use ...
- java.net.BindException: address already in use <null>:xxxx
linux下,tomcat突然关闭了,执行关闭(shutdown.sh)和启动(startup.sh)成功后,tomcat仍未运行,查看tomcat的catalina日志发现这样一个报错:java.n ...
- 关于 Unix 用户权限及进程权限及 Saved set-user-id
最近在看APUE,看到3.14节,fcntl的时候#include <fcntl.h>int fcntl(int fd, int cmd, .../* int arg */);出错返回-1 ...