本篇文章主要介绍了c#的socket实现udp协议通信示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
今天稍花化了一点时间,利用c#的socket验证了udp的通信,为接下来特地利用udp做个分布式的通信仿真系统打下基础。众所周知,udp 就是用户数据报协议,在互联网参考模型的第四层——传输层。与tcp协议同层,都是提供信息的传输服务,但与tcp最大区别就是,它是一种无连接不可靠的信息传输。
什么是无连接不可靠?直白点讲就是,发送数据的时候,直接把udp包往网络一扔就完事了,接不接受收到发送的就不理了;接受数据的时候,有发给本地的udp包就照单全收,收完再看是谁发的。相比tcp,少了握手建立连接,维护连接,连接释放等一系列过程,因此具有很小的资源消耗和处理速度快的优点。
好了说了一堆废话,开始讲怎么利用c#中socket进行udp通信。tcp、udp应用程序可以通过tcpclient、tcplistener 和 udpclient 类进行编程,而这些协议类也建立在system.net.sockets.socket 类的基础上,并无需理会数据传送的细节。但为了更好地理解socket编程,这里还是利用了socket类进行udp通信编程。
udp应用上已经无严格意义上的真正的服务器和客户端之分了,端点之间都是平等的关系,因此进行通信只需编写一个程序即可。
下面给出关键部分代码与说明:
关键的全局变量
private ipendpoint iplocalpoint;
private endpoint remotepoint;
private socket mysocket;
private bool runningflag = false;
获取本地ip的方法
private string getipaddress()
{
// 获得本机局域网ip地址
ipaddress[] addresslist = dns.gethostbyname(dns.gethostname()).addresslist;
if (addresslist.length < 1)
{
return "";
}
return addresslist[0].tostring();
}
ip与端口号有效验证
private int getvalidport(string port)
{
int lport;
//测试端口号是否有效
try
{
//是否为空
if (port == "")
{
throw new argumentexception(
"端口号无效,不能启动dup");
}
lport = system.convert.toint32(port);
}
catch (exception e)
{
//argumentexception,
//formatexception,
//overflowexception
console.writeline("无效的端口号:" + e.tostring());
this.tbmsg.appendtext("无效的端口号:" + e.tostring() + "\n");
return -1;
}
return lport;
}
private ipaddress getvalidip(string ip)
{
ipaddress lip = null;
//测试ip是否有效
try
{
//是否为空
if (!ipaddress.tryparse(ip, out lip))
{
throw new argumentexception(
"ip无效,不能启动dup");
}
}
catch (exception e)
{
//argumentexception,
//formatexception,
//overflowexception
console.writeline("无效的ip:" + e.tostring());
this.tbmsg.appendtext("无效的ip:" + e.tostring() + "\n");
return null;
}
return lip;
}
socket的配置
//得到本机ip,设置udp端口号
ip = getvalidip(tblocalip.text);
port = getvalidport(tblocalport.text);
iplocalpoint = new ipendpoint(ip, port);
//定义网络类型,数据连接类型和网络协议udp
mysocket = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp);
//绑定网络地址
mysocket.bind(iplocalpoint);
//得到客户机ip
ip = getvalidip(tbremoteip.text);
port = getvalidport(tbremoteport.text);
ipendpoint ipep = new ipendpoint(ip, port);
remotepoint = (endpoint)(ipep);
//启动一个新的线程,执行方法this.receivehandle,
//以便在一个独立的进程中执行数据接收的操作
runningflag = true;
thread thread = new thread(new threadstart(this.receivehandle));
thread.start();
接收线程
//定义一个委托
public delegate void myinvoke(string strrecv);
private void receivehandle()
{
//接收数据处理线程
string msg;
byte[] data=new byte[1024];
myinvoke myi = new myinvoke(updatemsgtextbox);
while (runningflag)
{
if (mysocket == null || mysocket.available < 1)
{
thread.sleep(200);
continue;
}
//跨线程调用控件
//接收udp数据报,引用参数remotepoint获得源地址
int rlen = mysocket.receivefrom(data, ref remotepoint);
msg = encoding.default.getstring(data, 0, rlen);
tbmsg.begininvoke(myi, new object[]{remotepoint.tostring() + " : " + msg});
}
}
private void btsend_click(object sender, eventargs e)
{
string msg;
msg = tbsendmsg.text;
//发送udp数据包
byte[] data = encoding.default.getbytes(msg);
mysocket.sendto(data, data.length, socketflags.none, remotepoint);
}
private void updatemsgtextbox(string msg)
{
//接收数据显示
this.tbmsg.appendtext( msg + "\n");
}
以上只需设置好本地和远程的ip和端口号,很容易就实现了udp的双向通信。虽说udp数据包不能保证可靠传输,网络繁忙、拥塞等因素,都有可能阻止数据包到达指定的目的地。但经过测试,其通信还蛮可靠的,别忘了qq也是利用udp进行即时通信的。
以上就是c#的socket实现udp协议通信的示例代码详解的详细内容。