【C语言】【unix c】编写代码实现基于UDP的网络通信
来源:互联网 发布:三体电影知乎 编辑:程序博客网 时间:2024/06/06 11:02
编写代码实现基于UDP的网络通信:(userv.c uclie.c) userv.c: #include <stdio.h> #include <p_net.h> #include <ctype.h> int main(void) { SA4 serv, cli; int rcv; char buf[128]; int cli_len = sizeof(cli); //创建socket int s_fd = socket(AF_INET, SOCK_DGRAM, 0); if(s_fd == -1) { perror("socket"); return -1; } //初始化服务器信息 serv.sin_family = AF_INET; serv.sin_port = htons(7777); serv.sin_addr.s_addr = htonl(INADDR_ANY); int b = bind(s_fd, (SA*)&serv, sizeof(SA4)); if(b == -1) { perror("bind"); return -1; } while(1) { rcv = recvfrom(s_fd, buf, 128, 0, (SA*)&cli, &cli_len); if(rcv > 0) { int i; for(i = 0; i < rcv; i++) { buf[i] = toupper(buf[i]); } sendto(s_fd, buf, rcv, 0, (SA*)&cli, sizeof(SA4)); } } return 0; } uclie.c: #include <stdio.h> #include <p_net.h> #include <string.h> int main(int argc, char *argv[]) { SA4 serv; char *msg = "wang \n"; char *buf[128]; //创建socket int s_fd = socket(AF_INET, SOCK_DGRAM, 0); if(s_fd == -1) { perror("socket"); return -1; } //初始化服务器的地址 serv.sin_family = AF_INET; serv.sin_port = htons(7777); inet_pton(AF_INET, argv[1], &serv.sin_addr); //向服务器发消息 int s = sendto(s_fd, msg, strlen(msg), 0, (SA*)&serv,sizeof(SA4)); if(s == -1) { perror("sendto"); return -1; } //等待从服务器段返回消息 int rcv = recvfrom(s_fd, buf, 128, 0, NULL, NULL); write(1, buf, rcv); close(s_fd); return 0; }
阅读全文
0 0
- 【C语言】【unix c】编写代码实现基于UDP的网络通信
- 【C语言】【unix c】基于UDP的网络编程
- 【C语言】【unix c】编写代码实现cat的功能
- C语言编写基于TCP和UDP协议的Socket通信程序示例
- C编程之基于UDP的网络通信
- C语言--利用UDP实现socket通信
- Linux下C语言实现简单的udp通信
- C/C++实现基于UDP的网络编程
- Unix c 网络编程 UDP
- 【C语言】【unix c】网络的基础知识
- UNIX 网络编程测试代码 -- UDP通信
- C语言实现UDP连接的参考代码
- 基于C的UDP,TCP网络编程
- 【C语言】【unix c】编写代码测试自己电脑的字节序
- udp通信(C 语言)
- 基于linux简单的UDP网络通信代码
- C语言Socket实现网络通信
- c语言中如何实现网络通信
- QCustomPlot的legend图例背景透明的方法
- 快排
- 学习Linux C编程之数据类型 问题-extern及static修饰全局变量的区别
- nagios插件之发起http的purchase购买请求
- java 课堂笔记 类和对象
- 【C语言】【unix c】编写代码实现基于UDP的网络通信
- centos下xxx is not in the sudoers file.this incident will be reported
- 从问题角度来思考设计模式(1)
- Javaweb面试题
- kubernetes之Scheduler分析
- Oracle笔记十一: 建表、更新、查询综合练习
- redhat7创建完整克隆之后出现Network is unreachable解决办法
- 项目报错java.lang.NoClassDefFoundError: HttpServletRequest
- python3[爬虫实战] 使用selenium,xpath爬取京东手机(下)