函数原型:
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
前三个参数同read/write一样;
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
常见flags:
1. 一般设置为0
2. MSG_PEEK:窥视传入的数据。 数据被复制到缓冲区中,但不会从输入队列中删除。
3. MSG_OOB:处理带外(OOB)数据
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
1. 前四个参数同recv/send一样;
2. 后两个参数是通信结构体和结构体的宽度;
server.c
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int fd;
struct sockaddr_in addr;
char buf[BUFSIZ] = {};
if(argc < 3){
fprintf(stderr, "%s\n" , argv[0]);
exit(EXIT_FAILURE);
}
/*创建套接字*/
if( (fd = socket(AF_INET, SOCK_DGRAM, 0) ) < 0){
perror("socket");
exit(EXIT_FAILURE);
}
/*设置通信结构体*/
bzero(&addr, sizeof(addr) );
addr.sin_port = htons( atoi(argv[2]) );
if(inet_aton(argv[1], &addr.sin_addr) == 0) {
fprintf(stderr, "Invalid address\n");
exit(EXIT_FAILURE);
}
/*绑定通信结构体*/
if(bind(fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1){
perror("bind");
exit(EXIT_FAILURE);
}
while(1){
bzero(buf, BUFSIZ);
recvfrom(fd, buf, BUFSIZ, 0, NULL, NULL);
printf("buf=%s\n", buf);
}
close(fd);
return 0;
}
client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int fd;
struct sockaddr_in addr;
char buf[BUFSIZ] = {};
socklen_t addrlen = sizeof(addr);
if(argc < 3){
fprintf(stderr, "%s\n" , argv[0]);
exit(EXIT_FAILURE);
}
/*创建套接字*/
if( (fd = socket(AF_INET, SOCK_DGRAM, 0) ) < 0){
perror("socket");
exit(EXIT_FAILURE);
}
/*设置通信结构体*/
bzero(&addr, sizeof(addr) );
addr.sin_port = htons( atoi(argv[2]) );
if(inet_aton(argv[1], &addr.sin_addr) == 0) {
fprintf(stderr, "Invalid address\n");
exit(EXIT_FAILURE);
}
while(1){
bzero(buf, BUFSIZ);
printf("Input->");
fgets(buf, BUFSIZ, stdin);
sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&addr, addrlen);
}
close(fd);
return 0;
}
Makefile 编译文件
CC = gcc
CFLAGS = -Wall
all: server client
server: server.c
$(CC) $(CFLAGS) -o server server.c
client: client.c
$(CC) $(CFLAGS) -o client client.c
clean:
rm -f server client