当前位置:网站首页>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;
}
边栏推荐
- JS null judgment operators | and? Usage of
- Write about your feelings about love and express your emotions
- The first up Master of station B paid to watch the video still came! Price "Persuading" netizens
- 网络安全-路由原理
- Ad usage notes
- 网络安全-VLAN和Tunk方法详解
- 记录开发过程中无法使用管理员身份修改系统文件问题
- Introduction to go language pointer
- 社招两年半10个公司28轮面试面经
- Resolved: initialize specified but the data directory has files in it Aborting
猜你喜欢

安装Go语言开发工具

Xshell transfer file

Vs2019 and SQL

RT thread kernel application development message queue experiment

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

QT elementary notes

Linux服務器安裝Redis

踩坑记录:supervisor 日志返回信息:redis扩展未安装

Class template case - encapsulation of array classes

Skillfully use 5 keys to improve office efficiency
随机推荐
网络安全-单臂路由、DHCP中继和ICMP协议
QT msvc2015 compiler reports an error: error: lnk1158: unable to run "rc.exe"
Introduction to go project directory structure
2022年6月29日--使用C#迈出第一步--使用 C# 中的“if”、“else”和“else if”语句向代码添加决策逻辑
Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)
01 - embedded learning route and career planning: embedded basic knowledge and development process
QT common macro definitions
grep命令用法
2021-07-02
Traverse map
Xshell transfer file
Determine whether the picture is in JPG picture format
Network security - detailed explanation of VLAN and tunk methods
已解决:initialize specified but the data directory has files in it. Aborting
Resolved: initialize specified but the data directory has files in it Aborting
嵌入式测试流程
将本地电脑文件复制到虚拟机系统中详细方法
JS create PDF file
Binary tree traversal
JS widget wave JS implementation of wave progress bar animation style