之前写了篇博客:AF_UNIX和127.0.0.1(AF_INET)回环地址写数据速度对比
然后利用的是发送端读取大文件,接收方接收并保存为文件的方式进行测试,结果发现,AF_UNIX并未比127.0.0.1(AF_INET)回环地址优秀,若单次发送的字节数少时,回环地址反而更快。
由于测试时发送的是1.15G大小的文件,比较快就发送结束了,而且读文件,写文件是个比较费时的操作,本人考虑到读写文件费时的影响,决定发送端自己构造字符串,接收方只统计接收到的字符个数,并不写文件。然后发送端发送100秒,对比下100秒之内,AF_UNIX和回还地址接收到的字节个数。
AF_UNIX服务端代码(unixsocketserver2.c)
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#define MAXLINE 80
char *socket_path = "/tmp/server.socket";
#define RECV_LEN 1000000
int main(void)
{
fd_set readmask, exceptmask;
struct timeval tv;
int maxfd = FD_SETSIZE;
int nready = 0;
char buf[RECV_LEN + 1];
int readbyte, writebyte;
struct sockaddr_un serun, cliun;
socklen_t cliun_len;
int listenfd, connfd, size;
if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket error");
exit(1);
}
long long allrecvbyte = 0;
memset(&serun, 0, sizeof(serun));
serun.sun_family = AF_UNIX;
strcpy(serun.sun_path, socket_path);
size = offsetof(struct sockaddr_un, sun_path) + strlen(serun.sun_path);
unlink(socket_path);
if (bind(listenfd, (struct sockaddr *)&serun, size) < 0) {
perror("bind error");
exit(1);
}
printf("UNIX domain socket bound\n");
if (listen(listenfd, 20) < 0) {
perror("listen error");
exit(1);
}
printf("Accepting connections ...\n");
cliun_len = sizeof(cliun);
if ((connfd = accept(listenfd, (struct sockaddr *)&cliun, &cliun_len)) < 0){
perror("accept error");
goto end;
}
time_t now, endtime;
now = time(NULL);
while(1)
{
FD_ZERO(&readmask

文章比较了AF_UNIX和127.0.0.1回环地址在发送大文件时的性能,发现当发送字节数增加时,回环地址表现更优,AF_UNIX并未显示出明显优势。
最低0.47元/天 解锁文章
1322

被折叠的 条评论
为什么被折叠?



