shutdown关闭读这一半后为什么还能收到对端的数据。

之前看unix网络编程,shutdown函数读半关闭会丢弃数据的,但是我写了个测试程序,在同一个主机上运行,关闭读这一半后还是可以读到数据,这是为什么?

client

#include <stdio.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>


int main(int argc,char ** argv)
{
    int fd;
    struct sockaddr_in addr;
    int ret;
    int i;
    char buf[256] = {0};

    fd = socket(AF_INET,SOCK_STREAM,0);
    if(fd < 0){
        fprintf(stderr,"socket error\n");
        return -1;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(8888);
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    ret = connect(fd,(struct sockaddr*)&addr,sizeof(addr));
    if(ret < 0){
        fprintf(stderr,"connect error:%s\n",strerror(errno));
        close(fd);
        return -1;
    }

    i = 0;
    while(1){
#if 1
        if(i == 5){
            printf("shutdown read\n");
            shutdown(fd,SHUT_RD);
        }
#endif

        if(i == 10){
            printf("client break\n");
            shutdown(fd,SHUT_WR);
            break;
        }
        write(fd,"1234567890",10);
        i++;
        memset(buf,0,sizeof(buf));
        if((ret = read(fd,buf,sizeof(buf))) >0){
            printf("ret=%d,buf:%s\n",ret,buf);
        }
        sleep(1);

    }
    sleep(2);
    close(fd);
    return 0;
}

server

void recive_data(void *p)
{
    int fd =  *((int *)p);
    char buf[256] = {0};
    char out_data[256] = {0};
    int status = 0;
    int ret;
    int i = 0;
    while(1){
        memset(buf,0,sizeof(buf));
        if((ret = read(fd,buf,sizeof(buf)))>0){
            printf("server buf:%s\n");
        }
        else{
            printf("ret=%d,recv fin\n",ret);
            break;
        }
        i++;
        sprintf(out_data,"the %d tims,0987654321\n",i);
        ret = write(fd,out_data,strlen(out_data));
        if(ret > 0){
            printf("server write %d bytes data to client\n",ret);
        }

    }
    close(fd);
    //pthread_exit((void *)&status);
    return 0;
}


int main(int argc,char **argv)
{
    int socketfd  =  -1;
    struct sockaddr_in addr;
    struct sockaddr addr_client;
    int addr_len;
    int ret;
    int listenfd = -1;
    fd_set rdset;
    pthread_t tid;
    int status;


    socketfd = socket(AF_INET,SOCK_STREAM,0);
    if(socketfd < 0){
        fprintf(stderr,"socket error:%s \n",strerror(errno));
        return -1;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(8888);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    ret =bind(socketfd,(struct sockaddr*)&addr,sizeof(addr));
    if(ret < 0){
        fprintf(stderr,"bind error:%s\n",strerror(errno));
        return -1;
    }

    listen(socketfd,0);

    while(1){
        listenfd = accept(socketfd,&addr_client,&addr_len);
        if(listenfd < 0){
            fprintf(stderr,"accept error:%s\n",strerror(errno));
            return -1;
        }

        printf("listenfd=%d\n",listenfd);
        if(pthread_create(&tid,NULL,recive_data,(void *)&listenfd)<0){
            fprintf(stderr,"pthread_create error:%s\n",strerror(errno));
            return -1;
        }

        //pthread_join(tid,&status);
    }


    close(socketfd);
}

运行结果如下
shishaowei@T330A:~/test/IPC/socket/tcp$ ./tcp_server &
[1] 8567
shishaowei@T330A:~/test/IPC/socket/tcp$ ./tcp_client
listenfd=4
server buf:1234567890
server write 22 bytes data to client
ret=22,buf:the 1 tims,0987654321

server buf:1234567890
server write 22 bytes data to client
ret=22,buf:the 2 tims,0987654321

server buf:1234567890
server write 22 bytes data to client
ret=22,buf:the 3 tims,0987654321

server buf:1234567890
server write 22 bytes data to client
ret=22,buf:the 4 tims,0987654321

server buf:1234567890
server write 22 bytes data to client
ret=22,buf:the 5 tims,0987654321

shutdown read
server buf:1234567890
server write 22 bytes data to client
ret=22,buf:the 6 tims,0987654321

server buf:1234567890
server write 22 bytes data to client
ret=22,buf:the 7 tims,0987654321

server buf:1234567890
server write 22 bytes data to client
ret=22,buf:the 8 tims,0987654321

server buf:1234567890
server write 22 bytes data to client
ret=22,buf:the 9 tims,0987654321

server buf:1234567890
server write 23 bytes data to client
client break
ret=0,recv fin

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问