码迷,mamicode.com
首页 > 其他好文 > 详细

socket——recv??按行读取

时间:2020-04-19 12:52:53      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:pid   nbsp   nis   src   缓冲   connect   int   sock   ++   

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] == \n){
                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\n");
            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\n", 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] == \n){
                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\n", 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\n");
            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\n", host);

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

    char ip[16] = {0};
    getlocalip(ip);
    printf("localip=%s\n", 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??按行读取

标签:pid   nbsp   nis   src   缓冲   connect   int   sock   ++   

原文地址:https://www.cnblogs.com/bailuoxi/p/12730805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!