Visual C++设计UDP协议通讯示例
2023-09-11 14:16:45 时间
一、绪言
UDP是一种面向非连接,不可靠的通讯协议,相对于 TCP来说,虽然可靠性不及,但传输效率较高。所以在网络上仍有很大的用途。这几日需要了解下udp通讯的过程,上网发现这方面的资料还挺少。于是仔细的翻找了下msdn,基本上搞清楚了udp通讯的过程。做了一个 测试的例子。希望对大家有帮助。
二、UDP的通讯过程
server端: open a socket(socket)---& gt; name the socket(bind)--->send and receive data( sendto recvfrom)--->close socket(closesocket)
client端: open a socket(socket)---------------------------->send and receive data(sendto recvfrom)--->close socket(closesocket)
server端无需listen,client端无需connect,因此udp中server和client的区别相对较模糊。只要调用sendto和recvfrom就可以给指定的地址收发数据,但并不保证收发的数据的完整性和可靠性。
三、示例
udpserver主要代码:
UDP是一种面向非连接,不可靠的通讯协议,相对于 TCP来说,虽然可靠性不及,但传输效率较高。所以在网络上仍有很大的用途。这几日需要了解下udp通讯的过程,上网发现这方面的资料还挺少。于是仔细的翻找了下msdn,基本上搞清楚了udp通讯的过程。做了一个 测试的例子。希望对大家有帮助。
二、UDP的通讯过程
server端: open a socket(socket)---& gt; name the socket(bind)--->send and receive data( sendto recvfrom)--->close socket(closesocket)
client端: open a socket(socket)---------------------------->send and receive data(sendto recvfrom)--->close socket(closesocket)
server端无需listen,client端无需connect,因此udp中server和client的区别相对较模糊。只要调用sendto和recvfrom就可以给指定的地址收发数据,但并不保证收发的数据的完整性和可靠性。
三、示例
udpserver主要代码:
void main() { SOCKET socket1; InitWinsock(); struct sockaddr_in local; struct sockaddr_in from; int fromlen =sizeof(from); local.sin_family=AF_INET; local.sin_port=htons(1000); ///监听端口 local.sin_addr.s_addr=INADDR_ANY; ///本机 socket1=socket(AF_INET,SOCK_DGRAM,0); bind(socket1,(struct sockaddr*)&local,sizeof local); while (1) { char buffer[1024]="\0"; printf("waiting for message from others-------------\n"); if (recvfrom(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,&fromlen)!=SOCKET_ERROR) { printf("Received datagram from %s--%s\n",inet_ntoa(from.sin_addr),buffer); 给cilent发信息 sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,fromlen); } Sleep(500); } closesocket(socket1); } |
udpcilent主要代码:
void main() { SOCKET socket1; InitWinsock(); struct sockaddr_in server; int len =sizeof(server); server.sin_family=AF_INET; server.sin_port=htons(1000); ///server的监听端口 server.sin_addr.s_addr=inet_addr("168.168.0.10"); ///server的地址 socket1=socket(AF_INET,SOCK_DGRAM,0); while (1) { char buffer[1024]="\0"; printf("input message\n"); scanf("%s",buffer); if (strcmp(buffer,"bye")==0) break; if (sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&server,len)!=SOCKET_ERROR) { if (recvfrom(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&server,&len) != SOCKET_ERROR) printf("rece from server:%s\n",buffer); } } closesocket(socket1); } |
四、结束语
UDP通讯比TCP简单了很多,在数据可靠性不是很高的情况下,有使用的价值。
相关文章
- Visual Studio Code is not showing the ouput of Python
- error: Microsoft Visual C++ 14.0 is required问题最佳解决方法pip安装python包报错
- Visual Studio Code 支持TensorFlow配置支持
- Visual C++ 经常使用快捷键
- Win7安装visual c++ 2015 redistributable x64失败
- 【转】Visual Studio——多字节编码与Unicode码
- Visual Studio 2013 更新 NuGet 包管理器
- 《Visual C++ 2012 开发权威指南》导读
- 《数字图像处理与机器视觉——Visual C++与Matlab实现(第2版)》——2.5 图像的显示
- 《C++ AMP:用Visual C++加速大规模并行计算》——1.3 C++ AMP方法
- 《C++ AMP:用Visual C++加速大规模并行计算》——第3章 C++ AMP基础
- 《C++ AMP:用Visual C++加速大规模并行计算》——3.6 parallel_for_each
- 《C++ AMP:用Visual C++加速大规模并行计算》——3.7 使用restrict(amp)标记的函数
- 《数字图像处理与机器视觉——Visual C++与Matlab实现》——1.2 Matlab图像类型及其存储方式
- 《数字图像处理与机器视觉——Visual C++与Matlab实现》——1.3 Matlab的图像转换
- 泡泡一分钟:Semi-Dense Visual-Inertial Odometry and Mapping for Quadrotors with SWAP Constraints
- C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)
- Visual Studio 2017在编译时出现错误E2512:功能测试宏的参数必须是简单标识符
- Udacity project setup in Visual Studio with uWebSockets
- MonoDevelop with Visual Studio to Linux and Mac OSX maintaining a single code base for all platforms.
- visual studio必须建立项目才能编译.cpp文件吗?
- C++ 之 Visual Studio 搭建 EasyX Graphics Library 图形库环境,并使用EasyX(graphics.h)绘制第一个图形
- C++ 之 Windows Visual Studio 开发环境搭建/C++第一个Hello World
- Microsoft Visual C++ Runtime Library Runtime Error的解决的方法
- mysql与EF(visual studio 2015)