#include <sys/types.h>
#include <sys/socket >
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,const struct sockaddr *dest_addr, socklen_t addrlen);
- 参数:
- sockfd : 通信的fd
- buf : 要发送的数据
- len : 发送数据的长度
- flags : 0
- dest_addr : 通信的另外一端的地址信息
- addrlen : 地址的内存大小
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,struct sockaddr *src_addr, socklen_t *addrlen);
- 参数:
- sockfd : 通信的fd
- buf : 接收数据的数组
- len : 数组的大小
- flags : 0
- src_addr : 用来保存另外一端的地址信息,不需要可以指定为NULL
- addrlen : 地址的内存大小
udp_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
// 1.创建一个通信的socket
int fd = socket(PF_INET,SOCK_DGRAM,0);
if(fd == -1) {
perror("socket");
exit(-1);
}
// 2.绑定
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(9999);
saddr.sin_addr.s_addr = INADDR_ANY;
int ret = bind(fd,(struct sockaddr*)&saddr,sizeof(saddr));
if(ret == -1) {
perror("bind");
exit(-1);
}
// 3.通信
while (1) {
char recvbuf[128] = {0};
char ipbuf[16] = {0};
struct sockaddr_in caddr;
int len = sizeof(caddr);
// 接收数据
int num = recvfrom(fd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&caddr,&len);
if(num == -1) {
perror("recvfrom");
exit(-1);
}
inet_ntop(AF_INET,(struct sockaddr*)&caddr.sin_addr.s_addr,ipbuf,sizeof(ipbuf));
printf("Client IP : %s,Port : %d\n",ipbuf,ntohs(caddr.sin_port));
printf("client say : %s\n",recvbuf);
// 发送数据
sendto(fd,recvbuf,strlen(recvbuf) + 1,0,(struct sockaddr*)&caddr,sizeof(caddr));
}
close(fd);
return 0;
}
udp_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
// 1.创建一个通信的socket
int fd = socket(PF_INET,SOCK_DGRAM,0);
if(fd == -1) {
perror("socket");
exit(-1);
}
// 服务器的地址信息
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(9999);
inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr.s_addr);
int num = 0;
// 3.通信
while (1) {
// 发送数据
char sendBuf[128] = {0};
sprintf(sendBuf,"hello,i am client %d \n",num++);
sendto(fd,sendBuf,strlen(sendBuf) + 1,0,(struct sockaddr*)&saddr,sizeof(saddr));
// 接收数据
int num = recvfrom(fd,sendBuf,sizeof(sendBuf),0,NULL,NULL);
printf("server say : %s\n",sendBuf);
sleep(1);
}
close(fd);
return 0;
}
heheda@heheda:~/Linux/lesson37$ gcc udp_server.c -o server
heheda@heheda:~/Linux/lesson37$ ./server
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 0
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 1
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 2
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 3
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 4
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 0
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 5
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 1
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 6
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 2
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 7
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 3
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 8
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 4
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 9
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 5
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 10
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 6
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 11
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 7
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 12
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 8
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 13
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 9
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 14
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 10
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 15
Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 11