socket——recv??按行读取

Posted bailuoxi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了socket——recv??按行读取相关的知识,希望对你有一定的参考价值。

1、recv、read

      recv和read的区别??  recv只能用于套接口io,不能用于文件io或者其他io            read可以用于各种io

      recv相较而言多了flag参数,MSG_OOB(接收紧急数据指针)MSG_PEEK(接收缓冲区数据,但是并不急着清除)

2、封装按行读取readline

3、getsockname可以看到本机client针对已连接conn生成的ip和port

4、gethostname可以获取本机主机名/gethostbyname可以获取本机所有ip列表

 

技术图片
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#define ERR_EXIT(m)     do      {               perror(m);              exit(EXIT_FAILURE);      } while(0)

ssize_t readn(int fd, void *buf, size_t count){
    size_t nleft = count;
    ssize_t nread;
    char *bufp = (char*)buf;

    while(nleft > 0){
        if((nread = read(fd, bufp, nleft)) < 0){
            if(errno == EINTR)
                continue;
            return -1;
        } else if (nread == 0){
            return count - nleft;
        }
        bufp += nread;
        nleft -= nread;
    }
    return count;
}

ssize_t writen(int fd, void *buf, size_t count){
    size_t nleft = count;
    ssize_t nwritten;
    char *bufp = (char*)buf;

    while(nleft > 0){
        if((nwritten = write(fd, bufp, nleft)) < 0){
            if(errno == EINTR)
                continue;
            return -1;
        } else if (nwritten == 0){
            continue;
        }
        bufp += nwritten;
        nleft -= nwritten;
    }
    return count;
}

ssize_t recv_peek(int sockfd, void* buf, size_t len){
    while(1){
        int ret = recv(sockfd, buf, len, MSG_PEEK);
        if(ret == -1 && errno == EINTR)
            continue;
        return ret;
    }
}

//按行读取
ssize_t readline(int sockfd, void* buf, size_t maxline){
    int ret;
    int nread;
    char *bufp = buf;
    int nleft = maxline;
    while(1){
        ret = recv_peek(sockfd, bufp, nleft);
        if(ret < 0)
            return ret;
        else if(ret == 0){
            return ret;
        }
        nread = ret;
        int i;

        for(i = 0; i<nread; i++){
            if(bufp[i] == 
){
                ret = readn(sockfd, bufp, i+1);
                if(ret != i+1){
                    exit(EXIT_FAILURE);
                }
                return ret;
            }
        }

        if(nread > nleft)
            exit(EXIT_FAILURE);
        nleft -= nread;
        ret = readn(sockfd, bufp, nread);
        if(ret != nread)
            exit(EXIT_FAILURE);

        bufp += nread;
    }
    return -1;
}

void do_service(int conn){
    char recvbuf[1024];
    while(1){
        memset(recvbuf, 0, sizeof(recvbuf));
        int ret = readline(conn, recvbuf, 1024);
        if (ret == -1){
            ERR_EXIT("readn...4");
        }else if(ret == 0){
            printf("client close
");
            break;
        }

        fputs(recvbuf, stdout);
        writen(conn, recvbuf, strlen(recvbuf));            
    }
}

int main(void){
    int listenfd;
    if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
            ERR_EXIT("socket");

    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(5188);
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    int on = 1;
    if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0){
        ERR_EXIT("setsockopt");
    }

    if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
        ERR_EXIT("bind");
    }
    if (listen(listenfd, SOMAXCONN) < 0)
        ERR_EXIT("listen");

    int conn;
    struct sockaddr_in peeraddr;
    socklen_t peerlen = sizeof(peeraddr);

    pid_t pid;
    while(1){
        if ((conn = accept(listenfd, (struct sockaddr*)&peeraddr, &peerlen)) < 0)
            ERR_EXIT("accept");
        printf("ip=%s  prot=%d
", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));

        pid = fork();
        if(pid == -1)
            ERR_EXIT("fork");
        if(pid == 0){
            close(listenfd);
            do_service(conn);
            exit(EXIT_SUCCESS);
        } else {
            close(conn);
        }
    }
    return 0;
}
echoser.c
技术图片
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#define ERR_EXIT(m)     do      {               perror(m);              exit(EXIT_FAILURE);      } while(0)

ssize_t readn(int fd, void *buf, size_t count){
    size_t nleft = count;
    ssize_t nread;
    char *bufp = (char*)buf;

    while(nleft > 0){
        if((nread = read(fd, bufp, nleft)) < 0){
            if(errno == EINTR)
                continue;
            return -1;
        } else if (nread == 0){
            return count - nleft;
        }
        bufp += nread;
        nleft -= nread;
    }
    return count;
}

ssize_t writen(int fd, void *buf, size_t count){
    size_t nleft = count;
    ssize_t nwritten;
    char *bufp = (char*)buf;

    while(nleft > 0){
        if((nwritten = write(fd, bufp, nleft)) < 0){
            if(errno == EINTR)
                continue;
            return -1;
        } else if (nwritten == 0){
            continue;
        }
        bufp += nwritten;
        nleft -= nwritten;
    }
    return count;
}

ssize_t recv_peek(int sockfd, void* buf, size_t len){
    while(1){
        int ret = recv(sockfd, buf, len, MSG_PEEK);
        if(ret == -1 && errno == EINTR)
            continue;
        return ret;
    }
}

//按行读取
ssize_t readline(int sockfd, void* buf, size_t maxline){
    int ret;
    int nread;
    char *bufp = buf;
    int nleft = maxline;
    while(1){
        ret = recv_peek(sockfd, bufp, nleft);
        if(ret < 0)
            return ret;
        else if(ret == 0){
            return ret;
        }
        nread = ret;
        int i;

        for(i = 0; i<nread; i++){
            if(bufp[i] == 
){
                ret = readn(sockfd, bufp, i+1);
                if(ret != i+1){
                    exit(EXIT_FAILURE);
                }
                return ret;
            }
        }

        if(nread > nleft)
            exit(EXIT_FAILURE);
        nleft -= nread;
        ret = readn(sockfd, bufp, nread);
        if(ret != nread)
            exit(EXIT_FAILURE);

        bufp += nread;
    }
    return -1;
}

int main(void){
    int sock;
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
            ERR_EXIT("socket");

    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(5188);
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if (connect(sock, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
        ERR_EXIT("connect");

    struct sockaddr_in localaddr;
    socklen_t addrlen = sizeof(localaddr);
    if (getsockname(sock, (struct sockaddr*)&localaddr, &addrlen) < 0){
        ERR_EXIT("getsockname");
    }
    printf("ip=%s  prot=%d
", inet_ntoa(localaddr.sin_addr), ntohs(localaddr.sin_port));


    char sendbuf[1024] = {0};
    char recvbuf[1024] = {0};

    while(fgets(sendbuf, sizeof(sendbuf), stdin) != NULL){
        writen(sock, sendbuf, strlen(sendbuf));

        int ret = readline(sock, recvbuf, sizeof(recvbuf));
        if (ret == -1){
            ERR_EXIT("readline");
        }else if(ret == 0){
            printf("client close
");
            break;
        }

        
        fputs(recvbuf, stdout);
        memset(sendbuf, 0, sizeof(sendbuf));
        memset(recvbuf, 0, sizeof(recvbuf));
    }
    close(sock);
    return 0;
}
echocli.c
技术图片
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#define ERR_EXIT(m)     do      {               perror(m);              exit(EXIT_FAILURE);      } while(0)

int getlocalip(char *ip){
    char host[100] = {0};
    if(gethostname(host, sizeof(host)) < 0){
        ERR_EXIT("gethostname");
    }
    struct hostent *hp;
    if((hp = gethostbyname(host)) == NULL)
        ERR_EXIT("gethostbyname");
    strcpy(ip, inet_ntoa(*(struct in_addr*)hp->h_addr_list[0]));
    return 0;
}

int main(void){
    char host[100] = {0};
    if(gethostname(host, sizeof(host)) < 0){
        ERR_EXIT("gethostname");
    }
    printf("host:%s
", host);

    struct hostent *hp;
    if((hp = gethostbyname(host)) == NULL)
        ERR_EXIT("gethostbyname");
    int i=0;
    while(hp->h_addr_list[i] != NULL){
        printf("%s
", inet_ntoa(*(struct in_addr*)hp->h_addr_list[i]));
        i++;
    }

    char ip[16] = {0};
    getlocalip(ip);
    printf("localip=%s
", ip);
    return 0;
}
getiplist.c
技术图片
.PHONY:clean all
CC=gcc
CFLAGS=-Wall -g
BIN=echosrv echocli getiplist
all:$(BIN)
%.o:%.c
    $(CC) $(CFLAGS) -c $C -o $@
clean:
    rm -f *.o $(BIN)
Makefile

 

以上是关于socket——recv??按行读取的主要内容,如果未能解决你的问题,请参考以下文章

关于 recv 和读取缓冲区 - C Berkeley Sockets

windows socket编程阻塞模式下,recv函数返回0?

python3.6:socket.recv() 与 socket.recv_into() 性能对比

在socket编程中怎么判断recv是不是接收完成

TCP:recv() 获取 ECONNRESET

怎样判定socket连接已经关闭