已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
服务器端服务器端 ntUdpServer cpp Defines the entry point for the console application include stdafx h include include winsock2 h winsock 2 2 library pragma comment lib ws2 32 lib define PORT 8888 define ADDR 127 0 0 1 int main int argc char argv WSADATA wsock SOCKET sersocket SOCKADDR IN serAddr int serAddrsize sizeof serAddr int nRet 0 char buf 1024 int PORT char ADDR PORT atoi argv 2 ADDR argv 1 初始化 Winsock 2 2 printf nInitialising Winsock n if WSAStartup MAKEWORD 2 2 exit 0 printf Initialised successfully n 创建 socket printf nCreating UDP Socket n if sersocket socket AF INET SOCK DGRAM IPPROTO UDP INVALID SOCKET printf Creation of socket failed d n WSAGetLastError WSACleanup return 0 printf UDP Socket Created successfully n 设置 SOCKADDR IN 地址结构 serAddr sin family AF INET serAddr sin port htons PORT serAddr sin addr s addr INADDR ANY serAddr sin addr s addr inet addr ADDR 绑定套接字 if bind sersocket SOCKADDR closesocket sersocket WSACleanup return 0 printf Waiting to receive data n for int i 0 i if nRet recvfrom sersocket buf sizeof buf 0 SOCKADDR closesocket sersocket WSACleanup return 0 printf The data was received from s d n inet ntoa serAddr sin addr ntohs serAddr sin port 显示接收到的数据 printf buf printf n if nRet sendto sersocket buf strlen buf 0 SOCKADDR closesocket sersocket WSACleanup return 0 若发现 exit 则退出处理循环 if strncmp buf exit sizeof exit 0 printf exit the receiving loop n break 关闭套接字 closesocket sersocket WSACleanup return 0 客户端客户端 ntUdpClient cpp Defines the entry point for the console application include stdafx h include include winsock2 h winsock 2 2 library pragma comment lib ws2 32 lib define PORT 8888 define ADDR 127 0 0 1 int main int argc char argv WSADATA wsock SOCKET clisocket SOCKADDR IN serAddr int serAddrsize sizeof serAddr int nRet 0 char buf 1024 int PORT char ADDR PORT atoi argv 2 ADDR argv 1 初始化 Winsock 2 2 printf nInitialising Winsock n if WSAStartup MAKEWORD 2 2 exit 0 printf Initialised successfully n 创建 socket printf nCreating UDP Socket n if clisocket socket AF INET SOCK DGRAM IPPROTO UDP INVALID SOCKET printf Creation of socket failed d n WSAGetLastError WSACleanup return 0 设置 SOCKADDR IN 地址结构 serAddr sin family AF INET serAddr sin port htons PORT serAddr sin addr s addr INADDR ANY serAddr sin addr s addr inet addr ADDR printf UDP Socket Created successfully n printf Trying to send messages n for int i 0 i memset buf 0 sizeof buf printf nMessage to be sent gets buf if nRet sendto clisocket buf strlen buf 1 0 SOCKADDR closesocket clisocket WSACleanup return 0 printf The data was sent to s d successfully n inet ntoa serAddr sin addr ntohs serAddr sin port if strncmp buf exit sizeof exit 0 break memset buf 0 sizeof buf if nRet recvfrom clisocket buf sizeof buf 0 SOCKADDR closesocket clisocket WSACleanup return 0 显示接
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年创新设计专员招聘面试题库及参考答案
- 中级会计内部题库及答案
- 2025年绿色能源顾问招聘面试参考题库及答案
- 铁路基础知识题库及答案
- 2025年社会学研究员招聘面试参考题库及答案
- 2025年影视制作导演招聘面试题库及参考答案
- 2025年母婴护理师招聘面试参考题库及答案
- 2025年策划经理招聘面试参考题库及答案
- 护士节抢答题题库及答案
- 消防题目考试题库及答案
- 2025山东德德州天衢建设发展集团有限公司招聘工作人员20人笔试考试参考试题及答案解析
- 2025年酉阳土家族苗族自治县辅警招聘考试真题附答案详解(满分必刷)
- 2025-2026学年河南省天一大联考高一上学期9月月考历史试题
- 标准离婚协议书文档模板
- 装修挂靠协议合同范本
- 爱情合同协议电子合同
- 2025年高考生物试题(重庆卷) 含答案
- 拆除工程专项方案
- 2025年全国低压电工证理论考试笔试试题(200题)附答案
- 《政治经济学》全套PPT课件【完整版】
- (完整版)安全评价、预评价验收评价标书模板
评论
0/150
提交评论