当前位置: 首页 > news >正文

UDP通信、本地套接字

#include <sys/types.h>
#include <sys/socket >
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,const struct sockaddr *dest_addr, socklen_t addrlen);- 参数:- sockfd : 通信的fd- buf : 要发送的数据- len : 发送数据的长度- flags : 0- dest_addr : 通信的另外一端的地址信息- addrlen : 地址的内存大小ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,struct sockaddr *src_addr, socklen_t *addrlen);- 参数:- sockfd : 通信的fd- buf : 接收数据的数组- len : 数组的大小- flags : 0- src_addr : 用来保存另外一端的地址信息,不需要可以指定为NULL- addrlen : 地址的内存大小

udp_server.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>int main() {// 1.创建一个通信的socketint fd = socket(PF_INET,SOCK_DGRAM,0);if(fd == -1) {perror("socket");exit(-1);}// 2.绑定struct sockaddr_in saddr;saddr.sin_family = AF_INET;saddr.sin_port = htons(9999);saddr.sin_addr.s_addr = INADDR_ANY;int ret = bind(fd,(struct sockaddr*)&saddr,sizeof(saddr));if(ret == -1) {perror("bind");exit(-1);}// 3.通信 while (1) {char recvbuf[128] = {0};char ipbuf[16] = {0};struct sockaddr_in caddr;int len = sizeof(caddr);// 接收数据int num = recvfrom(fd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&caddr,&len);if(num == -1) {perror("recvfrom");exit(-1);}inet_ntop(AF_INET,(struct sockaddr*)&caddr.sin_addr.s_addr,ipbuf,sizeof(ipbuf));printf("Client IP : %s,Port : %d\n",ipbuf,ntohs(caddr.sin_port));printf("client say : %s\n",recvbuf);// 发送数据sendto(fd,recvbuf,strlen(recvbuf) + 1,0,(struct sockaddr*)&caddr,sizeof(caddr));}close(fd);return 0;
}

udp_client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>int main() {// 1.创建一个通信的socketint fd = socket(PF_INET,SOCK_DGRAM,0);if(fd == -1) {perror("socket");exit(-1);}// 服务器的地址信息struct sockaddr_in saddr;saddr.sin_family = AF_INET;saddr.sin_port = htons(9999);inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr.s_addr);int num = 0;// 3.通信 while (1) {// 发送数据char sendBuf[128] = {0};sprintf(sendBuf,"hello,i am client %d \n",num++);sendto(fd,sendBuf,strlen(sendBuf) + 1,0,(struct sockaddr*)&saddr,sizeof(saddr));// 接收数据int num = recvfrom(fd,sendBuf,sizeof(sendBuf),0,NULL,NULL);printf("server say : %s\n",sendBuf);sleep(1);}close(fd);return 0;
}
heheda@heheda:~/Linux/lesson37$ gcc udp_server.c -o server
heheda@heheda:~/Linux/lesson37$ ./server
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 0 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 1 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 2 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 3 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 4 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 0 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 5 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 1 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 6 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 2 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 7 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 3 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 8 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 4 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 9 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 5 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 10 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 6 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 11 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 7 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 12 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 8 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 13 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 9 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 14 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 10 Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 15 Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 11 

http://www.lryc.cn/news/141859.html

相关文章:

  • ChatGPT提示与技巧分享:如何作出更好的提示2023年8月
  • 网络安全(自学黑客)一文全解
  • Vue中ElementUI结合transform使用时,发现弹框定位不准确问题
  • (一)连续随机量的生成-基于分布函数
  • 【springboot】Spring Cache缓存:
  • 数学建模-建模算法(4)
  • python之函数返回数据框
  • 电子商务安全体系架构技术方面
  • 新安装IDEA 常用插件、设置
  • ChromeOS 的 Linux 操作系统和 Chrome 浏览器分离
  • 哔哩哔哩 B站 bilibili 视频倍速设置 视频倍速可自定义
  • Lazada商品详情接口 获取Lazada商品详情数据 Lazada商品价格接
  • 路由攻击(ospf attack)及C/C++代码实现
  • nginx配置站点强制开启https
  • Jacoco XML 解析
  • 【面试题】JDK(工具包)、JRE(运行环境和基础库)、JVM(java虚拟机)之间的关系?
  • 软件设计师学习笔记7-输入输出技术+总线+可靠性+性能指标
  • Windows下MATLAB调用Python函数操作说明
  • 【android12-linux-5.1】【ST芯片】驱动与HAL移植后数据方向异常
  • JavaScript Es6_3笔记
  • Qt产生随机数
  • postgresql常用函数-数学函数
  • 【排序】快速排序(前后指针法)—— 考的最少的一种算法
  • 软考:中级软件设计师:关系代数:中级软件设计师:关系代数,规范化理论函数依赖,它的价值和用途,键,范式,模式分解
  • openCV实战-系列教程2:阈值与平滑处理(图像阈值/图像平滑处理/高斯/中值滤波)、源码解读
  • C语言第五章-循环结构练习
  • Echarts面积图2.0(范围绘制)
  • flink checkpoint时exact-one模式和atleastone模式的区别
  • QEMU 仿真RISC-V freeRTOS 程序
  • 用大白话来讲讲多线程的知识架构