1. 程式人生 > >linux網路程式設計基礎-常用函式錯誤處理封裝

linux網路程式設計基礎-常用函式錯誤處理封裝

my.c

#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
 #include <sys/types.h>



void perr_exit(const char *s)
{
	perror(s);
	exit(1);
}



//***************************網路*********************************
int Accept(int fd, struct sockaddr*  addr, socklen_t* addrlen)
{ int n; again: if ( (n = accept(fd, addr, addrlen)) < 0) { if ((errno == ECONNABORTED) || (errno == EINTR)) goto again; else perr_exit("accept error"); } return n; } int Bind(int fd, const struct sockaddr *addr, socklen_t addrlen) { int n; if ((n = bind(fd, addr, addrlen)
) < 0) perr_exit("bind error"); return n; } int Connect(int fd, const struct sockaddr *addr, socklen_t addrlen) { int n; if ((n = connect(fd, addr, addrlen)) < 0) perr_exit("connect error"); return n; } int Listen(int fd, int backlog) { int n; if ((n = listen(fd, backlog)) <
0) perr_exit("listen error"); return n; } int Socket(int family, int type, int protocol) { int n; if ( (n = socket(family, type, protocol)) < 0) perr_exit("socket error"); return n; } //***************************讀寫操作********************************* ssize_t Read(int fd, void *ptr, size_t nbytes) { ssize_t n; again: if ( (n = read(fd, ptr, nbytes)) == -1) { if (errno == EINTR) goto again; else return -1; } return n; } ssize_t Write(int fd, const void *buf, size_t nbytes) { ssize_t n; again: if ( (n = write(fd, buf, nbytes)) == -1) { if (errno == EINTR) goto again; else return -1; } return n; } //讀指定位元組數 ssize_t Readn(int fd, void *vptr, size_t n) { size_t nleft; ssize_t nread; char *ptr; ptr = vptr; nleft = n; while (nleft > 0) { if ( (nread = read(fd, ptr, nleft)) < 0) { if (errno == EINTR) nread = 0; else return -1; } else if (nread == 0) break; nleft -= nread; ptr += nread; } return n - nleft; } //寫指定的位元組數 ssize_t Writen(int fd, const void *vptr, size_t n) { size_t nleft; ssize_t nwritten; const char *ptr; ptr = vptr; nleft = n; while (nleft > 0) { if ( (nwritten = write(fd, ptr, nleft)) <= 0) { if (nwritten < 0 && errno == EINTR) nwritten = 0; else return -1; } nleft -= nwritten; ptr += nwritten; } return n; } //讀100位元組,通過ptr返回1位元組,然後每次呼叫都返回 第一次讀到的資料的 一個位元組,直到100位元組全部返回 static ssize_t my_read(int fd, char *ptr) { static int read_cnt; static char *read_ptr; static char read_buf[100]; if (read_cnt <= 0) { again: if ((read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) { if (errno == EINTR) goto again; return -1; } else if (read_cnt == 0) return 0; read_ptr = read_buf; } read_cnt--; *ptr = *read_ptr++; return 1; } //讀一行 ssize_t Readline(int fd, void *vptr, size_t maxlen) { ssize_t n, rc; char c, *ptr; ptr = vptr; for (n = 1; n < maxlen; n++) { if ( (rc = my_read(fd, &c)) == 1) { *ptr++ = c; if (c == '\n') break; } else if (rc == 0) { *ptr = 0; return n - 1; } else return -1; } *ptr = 0; return n; }

my.h

#ifndef __WRAP_H_
#define __WRAP_H_

//socket相關
void perr_exit(const char *s);
int Accept(int fd, struct sockaddr*  addr, socklen_t* addrlen);
int Bind(int fd, const struct sockaddr *addr, socklen_t addrlen);
int Connect(int fd, const struct sockaddr *addr, socklen_t addrlen);
int Listen(int fd, int backlog);
int Socket(int family, int type, int protocol);

//讀寫相關
ssize_t Read(int fd, void *ptr, size_t nbytes);
ssize_t Write(int fd, const void *buf, size_t nbytes);
ssize_t Readn(int fd, void *vptr, size_t n);
ssize_t Writen(int fd, const void *vptr, size_t n);
static ssize_t my_read(int fd, char *ptr);
ssize_t Readline(int fd, void *vptr, size_t maxlen);
#endif