博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux网络IO函数以及TCP连接函数包装
阅读量:5245 次
发布时间:2019-06-14

本文共 10675 字,大约阅读时间需要 35 分钟。

740551-20170614213647525-1641585732.png

标准I/O VS 网络IO

  • 标准I/O又称为标准I/O流,从某种意义上讲是全双工的,因为程序能够在同一个流上执行输入和输出。
  • Unix/Linux对网络的抽象是一种称为套接字的文件类型。和任何Unix/Linux文件一样,套接字也是用文件描述符来引用的,在这种情况下称为套接字描述符。引用进程通过读写套接字描述符来与运行在其他计算机上的进程通信。

然而对流的限制和对套接字的限制,有时候会相互冲突。(However, there are restrictions on full-duplex streams that interact badly with restrictions on sockets:):

  • 限制一:跟在输出函数之后的输入函数。 如果中间没有插入对fflush, fseek, fsetpos或者rewind的调用,一个输入函数不能跟随在一个输出函数之后。 fflush 函数清空与流相关的缓冲区。后三个函数使用 Unix I/O lseek 函数来重置当前的文件位置。
  • 限制二:跟在输入函数之后的输出函数。 如果中间没有插入对 fseek, fsetpos或者rewind的调用,一个输出函数不能跟随在一个输入函数之后, 除非该输入函数遇到了一个EOF。

这些限制给网络应用带来了一个问题, 因为对套接字使用 lseek 函数是非法的。 对流 I/O 的第一个限制能够通过采用在每个输入操作前刷新缓冲区这样的规则来满足。然而,要满足第二个限制的唯一办法是,对同一个打开的套接字描述符打开两个流,一个用来读,一个用来写:

FILE *fpin, *fput; fpin = fdopen(sockfd, "r"); fput = fdopen(sockfd, "w");

但是这种方法也有问题,因为它要求应用程序在两个流上都要调用fclose, 这样才能释放与每个流相关联的存储器资源,避免存储器泄漏:

fclose(fpin);fclose(fpout);

这些操作中的每一个都试图关闭同一个底层的套接字描述符,所以第二个close操作会失败。对顺序的程序来说,这并不是问题,但是在一个线程化的程序中关闭一个已经关闭了的描述符是会导致灾难的。

Linux网络IO的实现

头文件: rio.h

#ifndef __RIO_H__#define __RIO_H__#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// Simplifies calls to bind(), connect(), and accept() typedef struct sockaddr SA;// Persistent state for the robust I/O (Rio) package#define RIO_BUFSIZE 8192typedef struct { int rio_fd; // Descriptor for this internal buf int rio_cnt; // Unread bytes in internal buf char *rio_bufptr; // Next unread byte in internal buf char rio_buf[RIO_BUFSIZE]; // Internal buffer} rio_t;// External variablesextern int h_errno; // Defined by BIND for DNS errorsextern char **environ; // Defined by libc // Misc constants#define MAXLINE 8192 // Max text line length#define MAXBUF 8192 // Max I/O buffer size#define LISTENQ 1024 // Second argument to listen()// Our own error-handling functionsvoid unix_error(char *msg);void posix_error(int code, char *msg);void dns_error(char *msg);void app_error(char *msg);// Rio (Robust I/O) package ssize_t rio_readn(int fd, void *usrbuf, size_t n);ssize_t rio_writen(int fd, void *usrbuf, size_t n);void rio_readinitb(rio_t *rp, int fd); ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n);ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen);// Wrappers for Rio packagessize_t Rio_readn(int fd, void *usrbuf, size_t n);void Rio_writen(int fd, void *usrbuf, size_t n);void Rio_readinitb(rio_t *rp, int fd); ssize_t Rio_readnb(rio_t *rp, void *usrbuf, size_t n);ssize_t Rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen);// Client/server helper functionsint open_clientfd(char *hostname, int portno);int open_listenfd(int portno);// Wrappers for client/server helper functionsint Open_clientfd(char *hostname, int port);int Open_listenfd(int port); #endif // __RIO_H__

网络IO函数及其包裹函数:

#include "rio.h"/********************************************************************* * The Rio package - robust I/O functions **********************************************************************//* * rio_readn - robustly read n bytes (unbuffered) */// rio_readnssize_t rio_readn(int fd, void *usrbuf, size_t n) {    size_t nleft = n;    ssize_t nread;    char *bufp = usrbuf;    while (nleft > 0) {    if ((nread = read(fd, bufp, nleft)) < 0) {        if (errno == EINTR) // Interrupted by sig handler return        nread = 0;      // and call read() again        else        return -1;      // errno set by read()    }     else if (nread == 0)        break;              // EOF    nleft -= nread;    bufp += nread;    }    return (n - nleft);         // return >= 0}/* * rio_writen - robustly write n bytes (unbuffered) */// rio_writenssize_t rio_writen(int fd, void *usrbuf, size_t n) {    size_t nleft = n;    ssize_t nwritten;    char *bufp = usrbuf;    while (nleft > 0) {    if ((nwritten = write(fd, bufp, nleft)) <= 0) {        if (errno == EINTR)  // Interrupted by sig handler return        nwritten = 0;    // and call write() again        else        return -1;       // errno set by write()    }    nleft -= nwritten;    bufp += nwritten;    }    return n;}/*  * rio_read - This is a wrapper for the Unix read() function that *    transfers min(n, rio_cnt) bytes from an internal buffer to a user *    buffer, where n is the number of bytes requested by the user and *    rio_cnt is the number of unread bytes in the internal buffer. On *    entry, rio_read() refills the internal buffer via a call to *    read() if the internal buffer is empty. *//* $begin rio_read */static ssize_t rio_read(rio_t *rp, char *usrbuf, size_t n){    int cnt;    while (rp->rio_cnt <= 0) {  /* Refill if buf is empty */    rp->rio_cnt = read(rp->rio_fd, rp->rio_buf,                sizeof(rp->rio_buf));    if (rp->rio_cnt < 0) {        if (errno != EINTR) /* Interrupted by sig handler return */        return -1;    }    else if (rp->rio_cnt == 0)  /* EOF */        return 0;    else         rp->rio_bufptr = rp->rio_buf; /* Reset buffer ptr */    }    /* Copy min(n, rp->rio_cnt) bytes from internal buf to user buf */    cnt = n;              if (rp->rio_cnt < n)       cnt = rp->rio_cnt;    memcpy(usrbuf, rp->rio_bufptr, cnt);    rp->rio_bufptr += cnt;    rp->rio_cnt -= cnt;    return cnt;}/* * rio_readinitb - Associate a descriptor with a read buffer and reset buffer *//* $begin rio_readinitb */void rio_readinitb(rio_t *rp, int fd) {    rp->rio_fd = fd;      rp->rio_cnt = 0;      rp->rio_bufptr = rp->rio_buf;}/* * rio_readnb - Robustly read n bytes (buffered) *//* $begin rio_readnb */ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n) {    size_t nleft = n;    ssize_t nread;    char *bufp = usrbuf;        while (nleft > 0) {    if ((nread = rio_read(rp, bufp, nleft)) < 0)             return -1;          /* errno set by read() */     else if (nread == 0)        break;              /* EOF */    nleft -= nread;    bufp += nread;    }    return (n - nleft);         /* return >= 0 */}/*  * rio_readlineb - robustly read a text line (buffered) *//* $begin rio_readlineb */ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen) {    int n, rc;    char c, *bufp = usrbuf;    for (n = 1; n < maxlen; n++) {         if ((rc = rio_read(rp, &c, 1)) == 1) {        *bufp++ = c;        if (c == '\n') {                n++;            break;            }    } else if (rc == 0) {        if (n == 1)        return 0; /* EOF, no data read */        else        break;    /* EOF, some data was read */    } else        return -1;    /* Error */    }    *bufp = 0;    return n-1;}/********************************** * Wrappers for robust I/O routines **********************************/ssize_t Rio_readn(int fd, void *ptr, size_t nbytes) {    ssize_t n;      if ((n = rio_readn(fd, ptr, nbytes)) < 0)    unix_error("Rio_readn error");    return n;}void Rio_writen(int fd, void *usrbuf, size_t n) {    if (rio_writen(fd, usrbuf, n) != n)    unix_error("Rio_writen error");}void Rio_readinitb(rio_t *rp, int fd){    rio_readinitb(rp, fd);} ssize_t Rio_readnb(rio_t *rp, void *usrbuf, size_t n) {    ssize_t rc;    if ((rc = rio_readnb(rp, usrbuf, n)) < 0)    unix_error("Rio_readnb error");    return rc;}ssize_t Rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen) {    ssize_t rc;    if ((rc = rio_readlineb(rp, usrbuf, maxlen)) < 0)    unix_error("Rio_readlineb error");    return rc;}

TCP 连接函数包装

#include "rio.h"/********************************  * Client/server helper functions ********************************//* * open_clientfd - open connection to server at 
* and return a socket descriptor ready for reading and writing. * Returns -1 and sets errno on Unix error. * Returns -2 and sets h_errno on DNS (gethostbyname) error. *//* $begin open_clientfd */int open_clientfd(char *hostname, int port) { int clientfd; struct hostent *hp; struct sockaddr_in serveraddr; if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* Check errno for cause of error */ /* Fill in the server's IP address and port */ if ((hp = gethostbyname(hostname)) == NULL) return -2; /* Check h_errno for cause of error */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char *)hp->h_addr_list[0], (char *)&serveraddr.sin_addr.s_addr, hp->h_length); serveraddr.sin_port = htons(port); /* Establish a connection with the server */ if (connect(clientfd, (SA *) &serveraddr, sizeof(serveraddr)) < 0) return -1; return clientfd;}/* * open_listenfd - open and return a listening socket on port * Returns -1 and sets errno on Unix error. *//* $begin open_listenfd */int open_listenfd(int port) { int listenfd, optval=1; struct sockaddr_in serveraddr; /* Create a socket descriptor */ if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* Eliminates "Address already in use" error from bind */ if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)) < 0) return -1; /* Listenfd will be an endpoint for all requests to port on any IP address for this host */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short)port); if (bind(listenfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0) return -1; /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, LISTENQ) < 0) return -1; return listenfd;}/****************************************** * Wrappers for the client/server helper routines ******************************************/int Open_clientfd(char *hostname, int port) { int rc; if ((rc = open_clientfd(hostname, port)) < 0) { if (rc == -1) unix_error("Open_clientfd Unix error"); else dns_error("Open_clientfd DNS error"); } return rc;}int Open_listenfd(int port) { int rc; if ((rc = open_listenfd(port)) < 0) unix_error("Open_listenfd error"); return rc;}

错误处理函数 -- 包装

#include "rio.h"/**************************  * Error-handling functions **************************/// Unix-style errorvoid unix_error(char *msg) {    fprintf(stderr, "%s: %s\n", msg, strerror(errno));    exit(0);}// Posix-style errorvoid posix_error(int code, char *msg){    fprintf(stderr, "%s: %s\n", msg, strerror(code));    exit(0);}// DNS-style errorvoid dns_error(char *msg){    fprintf(stderr, "%s: DNS error %d\n", msg, h_errno);    exit(0);}// Application errorvoid app_error(char *msg){    fprintf(stderr, "%s\n", msg);    exit(0);}

参考资料

  • 《深入理解计算机系统 原书2th》(CSAPP)

转载于:https://www.cnblogs.com/acm1314/p/7010058.html

你可能感兴趣的文章
考题纠错2
查看>>
ps6-工具的基础使用
查看>>
关于CefSharp.WinForms的学习
查看>>
灵活运用 SQL SERVER FOR XML PATH
查看>>
es 加磁盘扩容
查看>>
linux 参数内核
查看>>
使用Azcopy在Azure上进行HBase的冷热备份还原
查看>>
计组_定点数一位乘_布斯公式
查看>>
linux下使用过的命令总结(未整理完)
查看>>
ES6的一些文章
查看>>
eclipse luna 无法安装veloeclipse问题
查看>>
LeetCode 198, 213 House Robber
查看>>
New Year Permutation(Floyd+并查集)
查看>>
Qt编写输入法V2018超级终结版
查看>>
<context:component-scan>详解
查看>>
DS博客作业07--查找
查看>>
[JOI2017] サッカー (Soccer)
查看>>
Git 方法
查看>>
[Python] numpy.nonzero
查看>>
2016-11-29
查看>>