当前位置:网站首页>套接字socket编程——UDP
套接字socket编程——UDP
2022-06-30 07:17:00 【编程小段】
UDP通信的socket编程
由于UDP是面向无连接的、不可靠的,所以实现起来比较简单,只需要在发送数据的时候指明往哪里发就可以了,而且UDP本身就支持多客户端的连接。
API函数介绍
接收消息(类似于accept+recv)
ssize_t recvfrom(int sockfd, void* buf, size_t len, int flags,
struct sockaddr* src_addr, socklen_t* addrlen);
param:
sockfd: 套接字
buf: 要接受的缓冲区
len: 缓冲区的长度
flags: 标志位,一般填0
src_addr: 原地址,传出参数
addrlen: 发送方地址长度
return:
成功:返回读到的字节数
失败:返回-1
ssize_t sendto(int sockfd, const void* buf, size_t len, int flags,
const struct sockaddr* dest_addr, socklen_t addrlen);
param:
sockfd: 套接字
buf: 要发送的缓冲区
len: 缓冲区的长度
flags: 标志位,一般填0
dest_addr: 目的地址
addrlen: 目的地址的长度
return:
成功:返回写入字节数
失败:返回-1
用nc命令测试UDP时,nc -u 127.1 8888
服务端代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<ctype.h>
int main()
{
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0)
{
perror("socket error\n");
return -1;
}
struct sockaddr_in serv;
struct sockaddr_in client;
bzero(&serv, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(8888);
serv.sin_addr.s_addr = htonl(INADDR_ANY);
bind(fd, (struct sockaddr*)&serv, sizeof(serv));
int n;
socklen_t len;
char buf[1024] = {
0};
while(1)
{
memset(buf, 0, sizeof(buf));
len = sizeof(client);
n = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*)&client, &len);
printf("port==[%d], n==[%d], buf==[%s]\n", ntohs(client.sin_port), n, buf);
for(int j=0; j<n; j++)
{
buf[j] = toupper(buf[j]);
}
sendto(fd, buf, n, 0, (struct sockaddr*)&client, len);
}
close(fd);
return 0;
}
客户端代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<ctype.h>
int main()
{
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0)
{
perror("socket error\n");
return -1;
}
int n;
char buf[1024] = {
0};
struct sockaddr_in serv;
serv.sin_family = AF_INET;
serv.sin_port = htons(8888);
inet_pton(AF_INET, "127.0.0.1", &serv.sin_addr.s_addr);
while(1)
{
memset(buf, 0, sizeof(buf));
n = read(STDIN_FILENO, buf, sizeof(buf));
sendto(fd, buf, n, 0, (struct sockaddr*)&serv, sizeof(serv));
memset(buf, 0, sizeof(buf));
n = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);//可以写NULL
printf("n==[%d], buf==[%s]\n", n, buf);
}
close(fd);
return 0;
}
边栏推荐
- JS null judgment operators | and? Usage of
- Determine whether the picture is in JPG picture format
- Promise async/await
- Linux server installation redis
- Class template case - encapsulation of array classes
- What does the real name free domain name mean?
- Realization of dissolve effect in unity and its principle analysis
- Goland常用快捷键设置
- Network security - detailed explanation of VLAN and tunk methods
- [daily question] 535 Encryption and decryption of tinyurl
猜你喜欢

What if I don't know what to do after graduating from university?

Network security - routing principle

MySQL encounters the problem of expression 1 of select list is not in group by claim and contains nonaggre

Xshell transfer file

FreeRTOS timer group

1285_把AUTOSAR函数以及变量等定义的宏用脚本展开以提高可读性

Traverse map

安装Go语言开发工具

视频播放器(一):流程

1285_ Expand macros defined by AUTOSAR functions and variables with scripts to improve readability
随机推荐
嵌入式测试流程
MySQL encounters the problem of expression 1 of select list is not in group by claim and contains nonaggre
我今年毕业,但我不知道我要做什么
已解决:initialize specified but the data directory has files in it. Aborting
Detailed methods for copying local computer files to virtual machine system
Develop common dependency Libraries
Can introduction
Stm32g0 and FreeRTOS learning summary
[solved] failed! Error: Unknown error 1130
网络安全-VLAN和Tunk方法详解
[hot100] palindrome substring and longest palindrome substring
Installation du serveur linux redis
Swiftui creates a beautiful custom press feedback button
Determine whether the picture is in JPG picture format
Qstring to const char*
Detailed analysis of message signals occupying multiple bytes and bits
1.someip introduction
系统软件开发基础知识
【Hot100】11. Container with the most water
【最全】linux服务器上安装Mysql