- 1、本文档共8页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
具有缓冲区功能的UDP数据传输类(UDP data transport class with buffer function)
具有缓冲区功能的UDP数据传输类(UDP data transport class with buffer function)
使用系统;
使用系统文本;
使用系统集合;
使用system.windows.forms;
使用系统网;
使用系统线程;
使用系统集合;
使用系统。网。插座;// UdpClient的命名空间
命名空间的UDP
{
摘要
/ / / ----名称:UDP通讯类
//
// 备注
/ / / ----使用说明与定义:
// //备注
公共课udpconnect /具有缓冲区功能的UDP数据传输类
{
摘要
/ / /缓冲区
//
私人只读队列字符串 字符串 sendqueue =新的队列(1000);//发送队列
私人只读队列字符串 字符串 recqueue =新的队列(1000);//接收队列
摘要
/ / / UDP对象
//
私人UdpClient m_client;
摘要
/ / /对方IP
//
m_remoteip字符串;
摘要
/ / /远程通讯端口
//
私有m_remoteport;
公共udpconnect(字符串remoteip,int端口传送过来的远端,int LocalPort)
{
m_client =新UdpClient(LocalPort);/ /绑定本地端口
m_remoteip = remoteip;
m_remoteport =端口传送过来的远端;
}
公共无效startsenddata() /开启发送数据
{
m_client。连接(m_remoteip,m_remoteport);/ /连接远程IP
/ /开一线程
螺纹=新线程(新”(listensenddata));
/ /设置为后台
th.isbackground =真;
start() TH;
}
private void listensenddata() /发送信息,一直从队列中取出信息发送到指定远程IP端口
{
虽然(真实)
{
尝试
{
如果(sendqueue.count!= 0)
{
字节[] sendbytes =编码。UTF8。GetBytes(sendqueue。dequeue());/ /从发送缓冲区中取出数据化为二进制
m_client。发送(sendbytes,sendbytes。长度);/ /将二进制数据发送到指定IP的指定端口
}
}
catch(异常错误)
{
MessageBox显示(犯错。tostring());
}
}
}
public void senddata (string data) / / 发送数据到缓冲区
{
sendqueue.enqueue (date).
}
/ / ------------------------------------------------------------------------------------------------------------------- / /
public void startrecdata () / / 开启接收数据
{
m _ client.connect (m _ remoteip, m _ remoteport); / / 连接远程ip
/ / 开一线程
thread thread _ rec = new thread (new threadstart (listenrecdata));
/ / 设置为后台
thread _ rec.isbackground = true;
thread _ rec.start ();
}
private void listenrecdata () / / 监听远程ip发送来的信息并压入队列
{
/ / 声明终结点
ipendpoint iep = new ipendpoint (ipaddress.parse (m _ remoteip), m _ remoteport);
while (true)
{
/ / 获得对方发送过来的数据包
byte [] getbytes = m _ client.receive (ref iep);
string text = system.text.encoding.utf8.getstring (getbytes); / / 从二进制转换成string
recqueue.enqueue (text); / / 压入队列中
}
}
public string getdata () / / 获取接收缓冲区中的内容
{
if (recqueue.count! = 0)
{
string str = recqueue.dequeue ();
return str;
}
return null;
}
public
您可能关注的文档
- 从心理辅导观点看现代禅((Modern Zen from the perspective of psychological counseling).doc
- 从杯装奶茶里获得的启发(Inspiration from a cup of milk tea).doc
- 从治理机制等谈家族企业发展(Discussion on family enterprise development from governance mechanism).doc
- 从理解到自由(From understanding to freedom).doc
- 从中航油(新管理加坡)公司的再次起飞看公司治理(A review of corporate governance of China Aviation Oil Corporation (new management, Singapore)).doc
- 从认知主义到建构主义(From cognitive to Constructivist).doc
- 从超弦理论到魔法(From superstring theory to magic).doc
- 从西南地区少数民族民间传说看铜鼓的厌胜功能(Viewing the function of the bronze drums in the minority nationalities in Southwest China).doc
- 从面相和手相看身体状况(Look at your body from face and palm).doc
- 仓储式物流中心的消防设计与探讨(Fire protection design and discussion of warehouse logistics center).doc
文档评论(0)