当前位置:网站首页>Socket socket programming -- UDP
Socket socket programming -- UDP
2022-06-30 07:19:00 【Programming segment】
UDP communication socket Programming
because UDP It's for connectionless 、 unreliable , So it's easy to implement , Just indicate where to send the data when sending it , and UDP It supports multi client connection .
API Function introduction
receive messages ( Be similar to accept+recv)
ssize_t recvfrom(int sockfd, void* buf, size_t len, int flags,
struct sockaddr* src_addr, socklen_t* addrlen);
param:
sockfd: Socket
buf: Buffer to accept
len: Length of buffer
flags: Sign a , General filling 0
src_addr: The original address , Out parameter
addrlen: Sender address length
return:
success : Returns the number of bytes read
Failure : 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: Socket
buf: Buffer to send
len: Length of buffer
flags: Sign a , General filling 0
dest_addr: Destination address
addrlen: Length of destination address
return:
success : Returns the number of bytes written
Failure : return -1
use nc Command test UDP when ,nc -u 127.1 8888
Server code
#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;
}
Client code
#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);// Can write NULL
printf("n==[%d], buf==[%s]\n", n, buf);
}
close(fd);
return 0;
}
边栏推荐
- QT common macro definitions
- Linux server installation redis
- Win10踩坑-开机0xc0000225
- Merge: extension click the El table table data to expand
- oracle数据库报列表中最大表达式为1000错误
- Introduction to ecostruxure (1) IEC61499 new scheme
- How crazy are young people in sideline industry: 3000 monthly salary and 3W sideline income
- Qdebug small details
- Detailed analysis of message signals occupying multiple bytes and bits
- 套接字socket编程——UDP
猜你喜欢
随机推荐
Embedded test process
Stm32g0 and FreeRTOS learning summary
网络安全-单臂路由、DHCP中继和ICMP协议
SQL Server2005中SUM函数内嵌套IF语句
28 rounds of interviews with 10 companies in two and a half years
MySQL encounters the problem of expression 1 of select list is not in group by claim and contains nonaggre
网络安全-抓包和IP包头分析
Class templates and friends
The first up Master of station B paid to watch the video still came! Price "Persuading" netizens
Detailed analysis of message signals occupying multiple bytes and bits
嵌入式测试流程
FreeRTOS timer group
Resolution: div failed to get keyboard event
[semidrive source code analysis] [x9 chip startup process] 34 - RTOS side display module SDM_ display_ Init display initialization source code analysis
解决:div获取不到键盘事件
TC397 QSPI(CPU)
03 - programming framework: Division of application layer, middle layer and driver layer in bare metal programming
The maximum expression in Oracle database message list is 1000 error
如果我在珠海,到哪里开户比较好?另外,手机开户安全么?
Xshell transfer file







