当前位置:网站首页>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;
}
边栏推荐
- 【已解决】ERROR 1290 (HY000): Unknown error 1290
- Minecraft 1.16.5模组开发(五十) 书籍词典 (Guide Book)
- 【已解决】Failed! Error: Unknown error 1130
- The simulation interface does not declare an exception and throws an exception
- Idea running run and services
- 神经网络计算量及参数量
- 2、 Layout system
- 1.someip introduction
- 1285_ Expand macros defined by AUTOSAR functions and variables with scripts to improve readability
- Qtcreator debug code after configuring CDB debugger view variable value display card
猜你喜欢

Class template case - encapsulation of array classes

Vs2019 and SQL

Go common commands

Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)

记录开发过程中无法使用管理员身份修改系统文件问题

FreeRTOS timer group

The maximum expression in Oracle database message list is 1000 error

Qtcreator debug code after configuring CDB debugger view variable value display card

Idea running run and services

Stm32g0 Tim interrupt use
随机推荐
Skillfully use 5 keys to improve office efficiency
I graduated this year, but I don't know what I want to do
Local unloading traffic of 5g application
halcon:读取摄像头并二值化
El input can only input numbers and has a decimal point. At most two digits can be reserved
网络安全-路由原理
Linu基础-分区规划与使用
Linu foundation - zoning planning and use
Write and run the first go language program
动态内存管理
Egret engine P2 physics engine (2) - Funny physical phenomenon of small balls hitting the ground
视频播放器(一):流程
【已实现】服务器jar包启动脚本、shell脚本
Win10 step pit - power on 0xc0000225
Mysql5.7 compressed version installation tutorial
Variable storage unit and pointer
[introduction to Expo application] v Expert recommendation letter template
If I am in Zhuhai, where can I open an account? In addition, is it safe to open a mobile account?
Pit stepping record: Supervisor log return information: redis extension is not installed
Daemon and user threads