AF_UNIX和127.0.0.1(AF_INET)回环地址写数据速度对比(二)

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

之前写了篇博客: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值