linux 接口函数,Linux下的socket函数接口

#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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值