最近学习了Linux下面的tcp编程,Linux系统下提供了基本的socket函数,但是缺少了一些错误处理机制。下面是自己总结的一些接口程序,希望大家一起完善。
/*wrap.h文件*/
/************************************************
* The package about the tcp communication
* in Linux OS, including the error handling.
*************************************************/
#ifndef WRAP_H
#define WRAP_H
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
void perr_exit(const char *s);
int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr);
void Bind(int fd, const struct sockaddr *sa, socklen_t salen);
void Connect(int fd, const struct sockaddr *sa, socklen_t salen);
void Listen(int fd, int backlog);
int Socket(int family, int type, int protocol);
void Close(int fd);
ssize_t Read(int fd, void *ptr, size_t nbytes);
ssize_t Write(int fd, const void *ptr, size_t nbytes);
ssize_t Readn(int fd, void *vptr, size_t n);
ssize_t Writen(int fd, const void *vptr, size_t n);
#endif
* The package about the tcp communication
* in Linux OS, including the error handling.
*************************************************/
#ifndef WRAP_H
#define WRAP_H
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
void perr_exit(const char *s);
int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr);
void Bind(int fd, const struct sockaddr *sa, socklen_t salen);
void Connect(int fd, const struct sockaddr *sa, socklen_t salen);
void Listen(int fd, int backlog);
int Socket(int family, int type, int protocol);
void Close(int fd);
ssize_t Read(int fd, void *ptr, size_t nbytes);
ssize_t Write(int fd, const void *ptr, size_t nbytes);
ssize_t Readn(int fd, void *vptr, size_t n);
ssize_t Writen(int fd, const void *vptr, size_t n);
#endif
/*wrap.c文件*/
#include "wrap.h"
/*********************************************************************
* Name : perr_exit
* Description : exit the function
* Input : the error string
* Output :
* Return :
* Others : by jzk 2009.12.02
**********************************************************************/
void perr_exit(const char *s)
{
perror(s);
exit(1);
}
/*********************************************************************
* Name : Accept
* Description : accept a connection on a socket
* Input : fd---a socket that has been created
* sa---a pointer to a sockaddr structure
* salenptr---actual size of the peer address
* Output :
* Return : the descriptor for the accepted socket
* Others : by jzk 2009.12.02
**********************************************************************/
int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
int n;
again:
if((n = accept(fd, sa, salenptr)) < 0) {
if((ECONNABORTED == errno) || (EINTR == errno))
goto again;
else
perr_exit("accept error");
}
return n;
}
/*********************************************************************
* Name : Bind
* Description : bind a name to a socket
* Input : fd---a socket that has been created
* sa---a pointer to a sockaddr structure
* salen---the size of the address structure
* Output :
* Return :
* Others : by jzk 2009.12.02
**********************************************************************/
void Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
if(bind(fd, sa, salen) < 0)
perr_exit("bind error");
}
/*********************************************************************
* Name : Connect
* Description : initiate a connection on a socket
* Input : fd---a socket that has been created
* sa---a pointer to a sockaddr structure
* salen---the size of the address structure
* Output :
* Return :
* Others : by jzk 2009.12.02
**********************************************************************/
void Connect(int fd, const struct sockaddr *sa, socklen_t salen)
{
if(connect(fd, sa, salen) < 0)
perr_exit("connect error");
}
/*********************************************************************
* Name : Listen
* Description : listen for connections on a socket
* Input : fd---a socket that has been created
* backlog---the maximum length to the queue of
* pending connections
* Output :
* Return :
* Others : by jzk 2009.12.02
**********************************************************************/
void Listen(int fd, int backlog)
{
if(listen(fd, backlog) < 0)
perr_exit("listen error");
}
/*********************************************************************
* Name : Socket
* Description : create an endpoint for communication
* Input : family---a communication domain
* type---the communication semantics
* protocol---a particular protocol for the socket
* Output :
* Return : return a descriptor of the socket
* Others : by jzk 2009.12.02
**********************************************************************/
int Socket(int family, int type, int protocol)
{
int n;
if((n = socket(family, type, protocol)) < 0)
perr_exit("socket error");
return n;
}
/*********************************************************************
* Name : Read
* Description : read from a file descriptor
* Input : fd---a socket that has been created
* ptr---the buffer which storage the bytes
* nbytes---the number of bytes read
* Output :
* Return : return the number of bytes read
* Others : by jzk 2009.12.02
**********************************************************************/
ssize_t Read(int fd, void *ptr, size_t nbytes)
{
ssize_t n;
again:
if((n = read(fd, ptr, nbytes)) == -1) {
if(EINTR == errno)
goto again;
else
return -1;
}
return n;
}
/*********************************************************************
* Name : Write
* Description : write to a file descriptor
* Input : fd---a socket that has been created
* ptr---buffer of the bytes
* nbytes---the number of bytes written
* Output :
* Return : return the number of bytes written
* Others : by jzk 2009.12.02
**********************************************************************/
ssize_t Write(int fd, const void *ptr, size_t nbytes)
{
ssize_t n;
again:
if((n = write(fd, ptr, nbytes)) == -1) {
if(EINTR == errno)
goto again;
else
return -1;
}
return n;
}
/*********************************************************************
* Name : Close
* Description : close a file descriptor
* Input : fd---a socket that has been created
* Output :
* Return :
* Others : by jzk 2009.12.02
**********************************************************************/
void Close(int fd)
{
if(close(fd) == -1)
perr_exit("close error");
}
/*********************************************************************
* Name : Readn
* Description : read from a file descriptor,
* make sure read the enough bytes
* Input : fd---a socket that has been created
* ptr---the buffer which storage the bytes
* nbytes---the number of bytes read
* Output :
* Return : return the number of bytes read
* Others : by jzk 2009.12.02
**********************************************************************/
ssize_t Readn(int fd, void *vptr, size_t nbytes)
{
size_t nleft;
size_t nread;
char *ptr;
ptr = vptr;
nleft = nbytes;
while(nleft > 0) {
if((nread = read(fd, ptr, nleft)) < 0) {
if(EINTR == errno)
nread = 0;
else
return -1;
} else if(nread == 0)
break;
nleft -= nread;
ptr += nread;
}
return (nbytes-nleft);
}
/*********************************************************************
* Name : Writen
* Description : write to a file descriptor,
* make sure write the enough bytes
* Input : fd---a socket that has been created
* ptr---the buffer which storage the bytes
* nbytes---the number of bytes read
* Output :
* Return : return the number of bytes read
* Others : by jzk 2009.12.02
**********************************************************************/
ssize_t Writen(int fd, const void *vptr, size_t nbytes)
{
size_t nleft;
size_t nwritten;
const char *ptr;
ptr = vptr;
nleft = nbytes;
while(nleft > 0) {
if((nwritten = write(fd, ptr, nleft)) <= 0) {
if(nwritten < 0 && EINTR == errno)
nwritten = 0;
else
return -1;
}
nleft -= nwritten;
ptr += nwritten;
}
return nbytes;
}
/*********************************************************************
* Name : perr_exit
* Description : exit the function
* Input : the error string
* Output :
* Return :
* Others : by jzk 2009.12.02
**********************************************************************/
void perr_exit(const char *s)
{
perror(s);
exit(1);
}
/*********************************************************************
* Name : Accept
* Description : accept a connection on a socket
* Input : fd---a socket that has been created
* sa---a pointer to a sockaddr structure
* salenptr---actual size of the peer address
* Output :
* Return : the descriptor for the accepted socket
* Others : by jzk 2009.12.02
**********************************************************************/
int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
int n;
again:
if((n = accept(fd, sa, salenptr)) < 0) {
if((ECONNABORTED == errno) || (EINTR == errno))
goto again;
else
perr_exit("accept error");
}
return n;
}
/*********************************************************************
* Name : Bind
* Description : bind a name to a socket
* Input : fd---a socket that has been created
* sa---a pointer to a sockaddr structure
* salen---the size of the address structure
* Output :
* Return :
* Others : by jzk 2009.12.02
**********************************************************************/
void Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
if(bind(fd, sa, salen) < 0)
perr_exit("bind error");
}
/*********************************************************************
* Name : Connect
* Description : initiate a connection on a socket
* Input : fd---a socket that has been created
* sa---a pointer to a sockaddr structure
* salen---the size of the address structure
* Output :
* Return :
* Others : by jzk 2009.12.02
**********************************************************************/
void Connect(int fd, const struct sockaddr *sa, socklen_t salen)
{
if(connect(fd, sa, salen) < 0)
perr_exit("connect error");
}
/*********************************************************************
* Name : Listen
* Description : listen for connections on a socket
* Input : fd---a socket that has been created
* backlog---the maximum length to the queue of
* pending connections
* Output :
* Return :
* Others : by jzk 2009.12.02
**********************************************************************/
void Listen(int fd, int backlog)
{
if(listen(fd, backlog) < 0)
perr_exit("listen error");
}
/*********************************************************************
* Name : Socket
* Description : create an endpoint for communication
* Input : family---a communication domain
* type---the communication semantics
* protocol---a particular protocol for the socket
* Output :
* Return : return a descriptor of the socket
* Others : by jzk 2009.12.02
**********************************************************************/
int Socket(int family, int type, int protocol)
{
int n;
if((n = socket(family, type, protocol)) < 0)
perr_exit("socket error");
return n;
}
/*********************************************************************
* Name : Read
* Description : read from a file descriptor
* Input : fd---a socket that has been created
* ptr---the buffer which storage the bytes
* nbytes---the number of bytes read
* Output :
* Return : return the number of bytes read
* Others : by jzk 2009.12.02
**********************************************************************/
ssize_t Read(int fd, void *ptr, size_t nbytes)
{
ssize_t n;
again:
if((n = read(fd, ptr, nbytes)) == -1) {
if(EINTR == errno)
goto again;
else
return -1;
}
return n;
}
/*********************************************************************
* Name : Write
* Description : write to a file descriptor
* Input : fd---a socket that has been created
* ptr---buffer of the bytes
* nbytes---the number of bytes written
* Output :
* Return : return the number of bytes written
* Others : by jzk 2009.12.02
**********************************************************************/
ssize_t Write(int fd, const void *ptr, size_t nbytes)
{
ssize_t n;
again:
if((n = write(fd, ptr, nbytes)) == -1) {
if(EINTR == errno)
goto again;
else
return -1;
}
return n;
}
/*********************************************************************
* Name : Close
* Description : close a file descriptor
* Input : fd---a socket that has been created
* Output :
* Return :
* Others : by jzk 2009.12.02
**********************************************************************/
void Close(int fd)
{
if(close(fd) == -1)
perr_exit("close error");
}
/*********************************************************************
* Name : Readn
* Description : read from a file descriptor,
* make sure read the enough bytes
* Input : fd---a socket that has been created
* ptr---the buffer which storage the bytes
* nbytes---the number of bytes read
* Output :
* Return : return the number of bytes read
* Others : by jzk 2009.12.02
**********************************************************************/
ssize_t Readn(int fd, void *vptr, size_t nbytes)
{
size_t nleft;
size_t nread;
char *ptr;
ptr = vptr;
nleft = nbytes;
while(nleft > 0) {
if((nread = read(fd, ptr, nleft)) < 0) {
if(EINTR == errno)
nread = 0;
else
return -1;
} else if(nread == 0)
break;
nleft -= nread;
ptr += nread;
}
return (nbytes-nleft);
}
/*********************************************************************
* Name : Writen
* Description : write to a file descriptor,
* make sure write the enough bytes
* Input : fd---a socket that has been created
* ptr---the buffer which storage the bytes
* nbytes---the number of bytes read
* Output :
* Return : return the number of bytes read
* Others : by jzk 2009.12.02
**********************************************************************/
ssize_t Writen(int fd, const void *vptr, size_t nbytes)
{
size_t nleft;
size_t nwritten;
const char *ptr;
ptr = vptr;
nleft = nbytes;
while(nleft > 0) {
if((nwritten = write(fd, ptr, nleft)) <= 0) {
if(nwritten < 0 && EINTR == errno)
nwritten = 0;
else
return -1;
}
nleft -= nwritten;
ptr += nwritten;
}
return nbytes;
}
本文转自jazka 51CTO博客,原文链接:http://blog.51cto.com/jazka/238247,如需转载请自行联系原作者