以下为正文
博客分类:
相对于recv()~send()等IO函数,recvmsg()与sendmsg()功能更为强大,当然用起来也更为复杂。
#include "sys/socket.h"
ssize_t recvmsg(int sockfd, struct msghdr * msg, int flags);
ssize_t sendmsg(int sockfd, struct msghdr * msg, int flags);
成功时候返回读写字节数,出错时候返回-1.
这2个函数只用于套接口,不能用于普通的I/O读写,参数sockfd则是指明要读写的套接口。
flags用于传入控制信息,一般包括以下几个
MSG_DONTROUTE send可用
MSG_DONWAIT send与recv都可用
MSG_PEEK recv可用
MSG_WAITALL recv可用
MSG_OOB send可用
MSG_EOR send recv都可用
返回信息都记录在struct msghdr * msg中。
struct msghdr {
void * msg_name;//协议地址和套接口信息,在非连接的UDP中,发送者要指定对方地址端口,接受方用于的到数据来源,如果不需要的话可以设置为NULL(在TCP或者连接的UDP中,一般设置为NULL)。
socklen_t msg_namelen;//上面的长度
struct iovec * msg_iov;
ssize_t msg_iovlen;//和readv和writev一样
void * msg_control;
socklen_t msg_controllen;
int msg_flags; //用于返回之前flags的控制信息
}
样例1,在TCP中使用 sendmsg与recvmsg
服务器
#define MAXSIZE 100
int main(int argc, char ** argu) {
.......
struct msghdr msg;//初始化struct msghdr
msg.msg_name = NULL; //在tcp中,可以设置为NULL
struct iovec io;//初始化返回数据
io.iov_base = buf; //只用了一个缓冲区
io.iov_len = MAXSIZE; //定义返回数据长度
msg.msg_iov = &io;
msg.msg_iovlen = 1;//只用了一个缓冲区,所以长度为1
...................
ssize_t recv_size = recvmsg(connfd, &msg, 0);
char * temp = msg.msg_iov[0].iov_base;//获取得到的数据
temp[recv_size] = '\0';//为数据末尾添加结束符
printf("get message:%s", temp);
..........................
}
客户端
#define MAXSIZE 100
int main(int argc, char ** argv) {
.................
struct msghdr msg;//初始化发送信息
msg.msg_name = NULL;
struct iovec io;
io.iov_base = send_buff;
io.iov_len = sizeof(send_buff);
msg.msg_iov = &io;
msg.msg_iovlen = 1;
if(argc != 2) {
printf("please input port");
exit(1);
}
............
ssize_t size = sendmsg(sockfd, &msg, 0);
close(sockfd);
exit(0);
}
这里控制信息都设置成0,主要是初始化返回信息struct msghdr结构。关于struct iovec这个数据结构,上一篇readv与writev有介绍。
未连接的UDP套接口
服务器
//#include "/programe/net/head.h"
#include
#include
#include
//仅支持Linux(POSIX)
#include
#include
#include
#include
#define MAXSIZE 100
int main(int argc, char ** argv) {
int sockfd;
struct sockaddr_in serv_socket;
struct sockaddr_in * client_socket = (struct sockaddr_in *) malloc (sizeof(struct sockaddr_in));
char buf[MAXSIZE + 1];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&serv_socket, sizeof(serv_socket));
serv_socket.sin_family = AF_INET;
serv_socket.sin_addr.s_addr = htonl(INADDR_ANY);
serv_socket.sin_port = htons(atoi(argv[1]));
bind(sockfd, (struct sockaddr *)&serv_socket, sizeof(serv_socket));
struct msghdr msg;
msg.msg_name = client_socket;
//如果想得到对方的地址和端口,一定要把初始化完毕的内存头指针放入msg之中
msg.msg_namelen = sizeof(struct sockaddr_in);//长度也要指定
struct iovec io;
io.iov_base = buf;
io.iov_len = MAXSIZE;
msg.msg_iov = &io;
msg.msg_iovlen = 1;
ssize_t len = recvmsg(sockfd, &msg, 0);
client_socket = (struct sockaddr_in *)msg.msg_name;
char ip[16];
inet_ntop(AF_INET, &(client_socket->sin_addr), ip, sizeof(ip));
int port = ntohs(client_socket->sin_port);
char * temp = msg.msg_iov[0].iov_base;
temp[len] = '\0';
printf("get message from %s[%d]: %s\n", ip, port, temp);
close(sockfd);
}
客户端
//#include "/programe/net/head.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "sys/select.h"
#define MAXSIZE 100
int main(int argc, char ** argv) {
int sockfd;
struct sockaddr_in serv_socket;
int maxfdpl;
char send[] = "hello yuna";
if(argc != 2) {
printf("please input port");
exit(1);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&serv_socket, sizeof(serv_socket));
serv_socket.sin_family = AF_INET;
serv_socket.sin_port = htons(atoi(argv[1]));
inet_pton(AF_INET, "192.168.1.235", &serv_socket.sin_addr);
struct msghdr msg;
msg.msg_name = &serv_socket;
msg.msg_namelen = sizeof(struct sockaddr_in);
struct iovec io;
io.iov_base = send;
io.iov_len = sizeof(send);
msg.msg_iov = &io;
msg.msg_iovlen = 1;
ssize_t send_size = sendmsg(sockfd, &msg, 0);
close(sockfd);
exit(0);
}