Linux中使用C语言实现基于UDP协议的Socket通信示例
linux下udp服务器端源码示例:
#include <errno.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> #include <string.h> #include <netinet/in.h> #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> int main(int argc, char **argv) { printf("server ip: %s/n", argv[1]); printf("server port: %s/n", argv[2]); printf("/n service starting.../n/n"); int socketFd; struct sockaddr_in svrAddr; struct sockaddr_in localAddr; socketFd = socket (AF_INET, SOCK_DGRAM, 0); if ( -1 == socketFd ) { perror( "socket:" ); exit(-1); } // 设置地址可复用 int option = 1; setsockopt( socketFd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option) ); // 客户端IP memset(&localAddr, 0, sizeof(localAddr)); localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = inet_addr( argv[1]); localAddr.sin_port = htons (atoi(argv[2])); int bindResult = bind(socketFd, (struct sockaddr *) &localAddr, sizeof(localAddr)); if ( -1 == bindResult ) { perror( "bind:" ); close(socketFd); exit(-1); } // 服务器IP memset(&svrAddr, 0, sizeof(svrAddr)); svrAddr.sin_family = AF_INET; svrAddr.sin_addr.s_addr = inet_addr( argv[1]); svrAddr.sin_port = htons (atoi(argv[2])); char tempBuff[2048] = {0}; for ( ; ; ) { // 接收数据 struct sockaddr_in fromAddr; memset((char *)&fromAddr, 0, (int)sizeof(fromAddr)); socklen_t fromLen = sizeof(fromAddr); ssize_t result = recvfrom( socketFd, tempBuff, sizeof(tempBuff), 0, (struct sockaddr *)&fromAddr, &fromLen); if ( -1 == result ) { perror("recvfrom:"); continue; } else { printf( "recv data %s successed. data len: %d/n", inet_ntoa(fromAddr.sin_addr), result ); printf( "data:/n"); for ( int i = 0; i < result; i++ ) { printf( "%02x ", tempBuff[i] ); if ( (i+1)%16 == 0 ) { printf( "/n" ); } } printf( "/n" ); } // 发送数据 result = sendto( socketFd, tempBuff, result, 0, (struct sockaddr*)&fromAddr, sizeof(fromAddr)); if ( -1 == result ) { perror("sendto:"); } else { printf("send data success. data len:%d/n", result ); } sleep( 60 ); } close(socketFd); }
linux下udp客户端源码示例:
#include <errno.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> #include <string.h> #include <netinet/in.h> #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> typedef struct _NSS_HEADER { unsigned short ProtocolVersion; /* 协议版本信息 */ unsigned short MsgType; /* 消息类型 */ unsigned short TransactionNo; /* 传输编号 */ unsigned short PacketLength; /* 数据包长度,包括Header */ }NSS_HEADER; int main(int argc, char **argv) { printf("client ip: %s/n", argv[1]); printf("client port: %s/n", argv[2]); printf("server ip: %s/n", argv[3]); printf("server port: %s/n", argv[4]); printf("/n service starting.../n/n"); int socketFd; struct sockaddr_in svrAddr; struct sockaddr_in localAddr; socketFd = socket (AF_INET, SOCK_DGRAM, 0); if ( -1 == socketFd ) { perror( "socket:" ); exit(-1); } // 设置地址可复用 int option = 1; setsockopt( socketFd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option) ); // 客户端IP memset(&localAddr, 0, sizeof(localAddr)); localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = inet_addr( argv[1]); localAddr.sin_port = htons (atoi(argv[2])); int bindResult = bind(socketFd, (struct sockaddr *) &localAddr, sizeof(localAddr)); if ( -1 == bindResult ) { perror( "bind:" ); sleep(10); close(socketFd); exit(-1); } // 服务器IP memset(&svrAddr, 0, sizeof(svrAddr)); svrAddr.sin_family = AF_INET; svrAddr.sin_addr.s_addr = inet_addr( argv[3]); svrAddr.sin_port = htons (atoi(argv[4])); char tempBuff[2048] = {0}; // 每分钟发送一次数据 NSS_HEADER hdr; memset( &hdr, 0, sizeof(NSS_HEADER)); for ( ; ; ) { // 发送数据 hdr.TransactionNo++; ssize_t result = sendto( socketFd, &hdr, sizeof(NSS_HEADER), 0, (struct sockaddr*)&svrAddr, sizeof(svrAddr)); if ( -1 == result ) { perror("sendto:"); } else { printf("send data success. data len:%d/n", result ); } // 接收数据 struct sockaddr_in fromAddr; memset((char *)&fromAddr, 0, (int)sizeof(fromAddr)); socklen_t fromLen = sizeof(fromAddr); result = recvfrom( socketFd, tempBuff, sizeof(tempBuff), 0, (struct sockaddr *)&fromAddr, &fromLen); if ( -1 == result ) { perror("recvfrom:"); } else { printf( "recv data %s successed. data len: %d/n", inet_ntoa(fromAddr.sin_addr), result ); printf( "data:/n"); for ( int i = 0; i < result; i++ ) { printf( "%02x ", tempBuff[i] ); if ( (i+1)%16 == 0 ) { printf( "/n" ); } } printf("/n"); } sleep( 60 ); } close(socketFd); }
上一篇:希尔排序算法的C语言实现示例
栏 目:C语言
下一篇:Win10中VC2013安装Unit test组件出现问题解决方案
本文标题:Linux中使用C语言实现基于UDP协议的Socket通信示例
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/2404.html
您可能感兴趣的文章
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言中对数函数的表达式 c语言中对数怎么表达
- 04-02c语言没有round函数 round c语言
- 04-02C语言中怎么打出三角函数 c语言中怎么打出三角函数的值
- 01-10使用OpenGL实现3D立体显示的程序代码
- 01-10深入理解C++中常见的关键字含义
- 01-10使用C++实现全排列算法的方法详解
- 01-10深入Main函数中的参数argc,argv的使用详解
- 01-10APUE笔记之:进程环境详解
- 01-10c++中inline的用法分析
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10delphi制作wav文件的方法
- 04-02jquery与jsp,用jquery
- 01-10C#中split用法实例总结
- 01-11ajax实现页面的局部加载
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10SublimeText编译C开发环境设置
- 08-05DEDE织梦data目录下的sessions文件夹有什