Linux常用开发服务器的代码[Linux zhoulifa ]

简介:
经常用到的几个自定义函数:
1、开启监听的函数

http://linux.chinaunix.net/bbs/viewthread.php?tid=786283&extra=
ExpandedBlockStart.gif /*************************关于本函数************************************  
InBlock.gif*function_name: OpenSCPServer  
InBlock.gif*参数说明:port整数型监听端口号,total整数型监听个数,sendbuflen整数型发送缓冲区大小  
InBlock.gif*          recvbuflen整数型接收缓冲区大小,blockORnot整数型是否阻塞,reuseORnot整数型是否端口重用  
InBlock.gif*purpose: 用来建立一个tcp服务端socket  
InBlock.gif*tidied by: zhoulifa(zhoulifa@163.com) 周立发(
http://zhoulifa.9999mb.com)  
InBlock.gifLinux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言  
InBlock.gif*date time:2006-07-05 20:00:00  
InBlock.gif*Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途  
InBlock.gif* 但请遵循GPL  
InBlock.gif*Thanks to: Paul Sheer 感谢Paul Sheer在select_tut的man手册里提供了这份源代码  
InBlock.gif*Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力  
InBlock.gif*Note:要使用此函数需要自定义一个全局变量char errorMessage[1024];并包含GetCurrentTime.h头文件  
ExpandedBlockEnd.gif********************************************************************
*/
  
None.gif int  
ExpandedBlockStart.gifOpenSCPServer( int port,  int total,  int sendbuflen,  int recvbuflen,  int blockORnot,  int reuseORnot)     {   
InBlock.gif    int    sockfd = 0, ret = 0, opt = 0, flags=1;   
InBlock.gif    struct sockaddr_in    laddr;   
InBlock.gif  
InBlock.gif    ret = sockfd = socket(PF_INET, SOCK_STREAM, 0);   
ExpandedSubBlockStart.gif    if(ret < 0)    {   
InBlock.gif        sprintf(errorMessage, "OpenTCPServer socket() error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif        return -1;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
InBlock.gif    ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseORnot, sizeof(int));   
ExpandedSubBlockStart.gif    if(ret < 0)    {   
InBlock.gif        sprintf(errorMessage, "OpenTCPServer setsockopt() reuse error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif        return -2;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
InBlock.gif    ret = setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &recvbuflen, sizeof(int));   
ExpandedSubBlockStart.gif    if ( ret < 0)    {   
InBlock.gif        sprintf(errorMessage, "OpenTCPServer setsockopt() recvbuf error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif        return -3;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
InBlock.gif    ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuflen, sizeof(int));   
ExpandedSubBlockStart.gif    if (ret < 0)    {   
InBlock.gif        sprintf(errorMessage, "OpenTCPServer setsockopt() sendbuf error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif        return -4;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
ExpandedSubBlockStart.gif    ioctl(sockfd,FIONBIO,&blockORnot);/*block or not*/  
InBlock.gif  
InBlock.gif    laddr.sin_family = PF_INET;   
InBlock.gif    laddr.sin_port = htons(port);   
InBlock.gif    laddr.sin_addr.s_addr = INADDR_ANY;   
InBlock.gif    bzero(&(laddr.sin_zero), 8);   
InBlock.gif  
InBlock.gif    ret = bind(sockfd, (struct sockaddr *)&laddr, sizeof(struct sockaddr));   
ExpandedSubBlockStart.gif    if(ret < 0)    {   
InBlock.gif        sprintf(errorMessage, "OpenTCPServer bind() error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif        close(sockfd);   
InBlock.gif        return -5;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif    ret = listen(sockfd, total);   
ExpandedSubBlockStart.gif    if(ret < 0)    {   
InBlock.gif        sprintf(errorMessage, "OpenTCPServer listen() error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif        close(sockfd);   
InBlock.gif        return -6;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif    sprintf(errorMessage, "OpenTCPServer opened on port.%d(%d) OK, socket(%d), buf(%d:%d)! %s", port, total, sockfd, sendbuflen, recvbuflen, GetCurrentTime(0, 0));   
InBlock.gif    return sockfd;   
ExpandedBlockEnd.gif}
   


ExpandedBlockStart.gif /*************************关于本函数************************************
InBlock.gif*function_name: OpenSCPServer
InBlock.gif*参数说明:port整数型监听端口号,total整数型监听个数,sendbuflen整数型发送缓冲区大小
InBlock.gif*          recvbuflen整数型接收缓冲区大小,blockORnot整数型是否阻塞,reuseORnot整数型是否端口重用
InBlock.gif*purpose: 用来建立一个tcp服务端socket
InBlock.gif*tidied by: zhoulifa(zhoulifa@163.com) 周立发(
http://zhoulifa.9999mb.com)
InBlock.gifLinux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言
InBlock.gif*date time:2006-07-05 20:00:00
InBlock.gif*Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途
InBlock.gif* 但请遵循GPL
InBlock.gif*Thanks to: Paul Sheer 感谢Paul Sheer在select_tut的man手册里提供了这份源代码
InBlock.gif*Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力
InBlock.gif*Note:要使用此函数需要自定义一个全局变量char errorMessage[1024];并包含GetCurrentTime.h头文件
ExpandedBlockEnd.gif********************************************************************
*/

None.gif int
ExpandedBlockStart.gifOpenSCPServer( int port,  int total,  int sendbuflen,  int recvbuflen,  int blockORnot,  int reuseORnot)     {
InBlock.gif    int    sockfd = 0, ret = 0, opt = 0, flags=1;
InBlock.gif    struct sockaddr_in    laddr;
InBlock.gif
InBlock.gif    ret = sockfd = socket(PF_INET, SOCK_STREAM, 0);
ExpandedSubBlockStart.gif    if(ret < 0)    {
InBlock.gif        sprintf(errorMessage, "OpenTCPServer socket() error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));
InBlock.gif        return -1;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseORnot, sizeof(int));
ExpandedSubBlockStart.gif    if(ret < 0)    {
InBlock.gif        sprintf(errorMessage, "OpenTCPServer setsockopt() reuse error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));
InBlock.gif        return -2;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    ret = setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &recvbuflen, sizeof(int));
ExpandedSubBlockStart.gif    if ( ret < 0)    {
InBlock.gif        sprintf(errorMessage, "OpenTCPServer setsockopt() recvbuf error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));
InBlock.gif        return -3;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuflen, sizeof(int));
ExpandedSubBlockStart.gif    if (ret < 0)    {
InBlock.gif        sprintf(errorMessage, "OpenTCPServer setsockopt() sendbuf error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));
InBlock.gif        return -4;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    ioctl(sockfd,FIONBIO,&blockORnot);/*block or not*/
InBlock.gif
InBlock.gif    laddr.sin_family = PF_INET;
InBlock.gif    laddr.sin_port = htons(port);
InBlock.gif    laddr.sin_addr.s_addr = INADDR_ANY;
InBlock.gif    bzero(&(laddr.sin_zero), 8);
InBlock.gif
InBlock.gif    ret = bind(sockfd, (struct sockaddr *)&laddr, sizeof(struct sockaddr));
ExpandedSubBlockStart.gif    if(ret < 0)    {
InBlock.gif        sprintf(errorMessage, "OpenTCPServer bind() error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));
InBlock.gif        close(sockfd);
InBlock.gif        return -5;
ExpandedSubBlockEnd.gif    }

InBlock.gif    ret = listen(sockfd, total);
ExpandedSubBlockStart.gif    if(ret < 0)    {
InBlock.gif        sprintf(errorMessage, "OpenTCPServer listen() error! return:%d, errno=%d, errortext:'%s' %s", ret, errno, strerror(errno), GetCurrentTime(0, 0));
InBlock.gif        close(sockfd);
InBlock.gif        return -6;
ExpandedSubBlockEnd.gif    }

InBlock.gif    sprintf(errorMessage, "OpenTCPServer opened on port.%d(%d) OK, socket(%d), buf(%d:%d)! %s", port, total, sockfd, sendbuflen, recvbuflen, GetCurrentTime(0, 0));
InBlock.gif    return sockfd;
ExpandedBlockEnd.gif}

None.gif


2、连接服务器的函数
ExpandedBlockStart.gif /*************************关于本函数************************************  
InBlock.gif*function_name: ConnectSCPServer  
InBlock.gif*参数说明:serverip服务器IP地址或主机名,serverport服务器端口,blockORnot整数型是否阻塞  
InBlock.gif*purpose: 用来建立一个tcp客户端socket  
InBlock.gif*tidied by: zhoulifa(zhoulifa@163.com) 周立发(
http://zhoulifa.9999mb.com)  
InBlock.gifLinux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言  
InBlock.gif*date time:2006-07-05 20:40:00  
InBlock.gif*Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途  
InBlock.gif* 但请遵循GPL  
InBlock.gif*Thanks to: Paul Sheer 感谢Paul Sheer在select_tut的man手册里提供了这份源代码  
InBlock.gif*Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力  
InBlock.gif*Note:要使用此函数需要自定义一个全局变量char errorMessage[1024];并包含自己编写的GetCurrentTime.h头文件  
ExpandedBlockEnd.gif********************************************************************
*/
  
None.gif int  
ExpandedBlockStart.gifConnectSCPServer( char * serverip,  int serverport,  int blockORnot)     {   
InBlock.gif    int    serversock = 0, ret = 0;   
InBlock.gif    unsigned long    addr;   
InBlock.gif    struct sockaddr_in    sin;   
InBlock.gif    struct hostent *he;   
InBlock.gif  
ExpandedSubBlockStart.gif    if((he=gethostbyname(serverip))== 0) {   
InBlock.gif        sprintf(errorMessage, "ConnectSCPServer IP address '%s' error! return:-1 %s", serverip, GetCurrentTime(0, 0));   
InBlock.gif        return -1;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
InBlock.gif    serversock = socket(PF_INET, SOCK_STREAM, 0);   
ExpandedSubBlockStart.gif    if(serversock == -1)    {   
InBlock.gif        sprintf(errorMessage, "ConnectSCPServer socket() error! return:-2, errno=%d, errortext:'%s' %s", errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif        return -2;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
InBlock.gif    ioctl(serversock, FIONBIO, &blockORnot);  //block or not   
InBlock.gif
  
InBlock.gif    memset((char*)&sin, 0, sizeof(struct sockaddr_in));   
InBlock.gif    sin.sin_family = PF_INET;   
InBlock.gif    sin.sin_port = htons(serverport);   
InBlock.gif    sin.sin_addr = *((struct in_addr *)he->h_addr);   
InBlock.gif  
InBlock.gif    ret = connect(serversock, (struct sockaddr *)&sin, sizeof(sin));   
InBlock.gif  
ExpandedSubBlockStart.gif    if(ret == -1)    {   
InBlock.gif        sprintf(errorMessage, "ConnectSCPServer connect() error! return:-3, errno=%d, errortext:'%s' %s", errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif        close(serversock);   
InBlock.gif        return -3;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
InBlock.gif    return serversock;   
ExpandedBlockEnd.gif}
   
None.gif


ExpandedBlockStart.gif /*************************关于本函数************************************
InBlock.gif*function_name: ConnectSCPServer
InBlock.gif*参数说明:serverip服务器IP地址或主机名,serverport服务器端口,blockORnot整数型是否阻塞
InBlock.gif*purpose: 用来建立一个tcp客户端socket
InBlock.gif*tidied by: zhoulifa(zhoulifa@163.com) 周立发(
http://zhoulifa.9999mb.com)
InBlock.gifLinux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言
InBlock.gif*date time:2006-07-05 20:40:00
InBlock.gif*Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途
InBlock.gif* 但请遵循GPL
InBlock.gif*Thanks to: Paul Sheer 感谢Paul Sheer在select_tut的man手册里提供了这份源代码
InBlock.gif*Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力
InBlock.gif*Note:要使用此函数需要自定义一个全局变量char errorMessage[1024];并包含自己编写的GetCurrentTime.h头文件
ExpandedBlockEnd.gif********************************************************************
*/

None.gif int
ExpandedBlockStart.gifConnectSCPServer( char * serverip,  int serverport,  int blockORnot)     {
InBlock.gif    int    serversock = 0, ret = 0;
InBlock.gif    unsigned long    addr;
InBlock.gif    struct sockaddr_in    sin;
InBlock.gif    struct hostent *he;
InBlock.gif
ExpandedSubBlockStart.gif    if((he=gethostbyname(serverip))== 0) {
InBlock.gif        sprintf(errorMessage, "ConnectSCPServer IP address '%s' error! return:-1 %s", serverip, GetCurrentTime(0, 0));
InBlock.gif        return -1;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    serversock = socket(PF_INET, SOCK_STREAM, 0);
ExpandedSubBlockStart.gif    if(serversock == -1)    {
InBlock.gif        sprintf(errorMessage, "ConnectSCPServer socket() error! return:-2, errno=%d, errortext:'%s' %s", errno, strerror(errno), GetCurrentTime(0, 0));
InBlock.gif        return -2;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    ioctl(serversock, FIONBIO, &blockORnot);  //block or not
InBlock.gif

InBlock.gif    memset((char*)&sin, 0, sizeof(struct sockaddr_in));
InBlock.gif    sin.sin_family = PF_INET;
InBlock.gif    sin.sin_port = htons(serverport);
InBlock.gif    sin.sin_addr = *((struct in_addr *)he->h_addr);
InBlock.gif
InBlock.gif    ret = connect(serversock, (struct sockaddr *)&sin, sizeof(sin));
InBlock.gif
ExpandedSubBlockStart.gif    if(ret == -1)    {
InBlock.gif        sprintf(errorMessage, "ConnectSCPServer connect() error! return:-3, errno=%d, errortext:'%s' %s", errno, strerror(errno), GetCurrentTime(0, 0));
InBlock.gif        close(serversock);
InBlock.gif        return -3;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    return serversock;
ExpandedBlockEnd.gif}


3、发送数据函数Send

ExpandedBlockStart.gif /*************************关于本函数************************************  
InBlock.gif*function_name: Send  
InBlock.gif*参数说明:sock整数型socket,buf待发送的内容,size要发送的大小,flag发送选项,timeout超时时间值  
InBlock.gif*purpose: 用来通过一个socket在指定时间内发送数据  
InBlock.gif*tidied by: zhoulifa(zhoulifa@163.com) 周立发(
http://zhoulifa.9999mb.com)  
InBlock.gifLinux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言  
InBlock.gif*date time:2006-07-05 20:58:00  
InBlock.gif*Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途  
InBlock.gif* 但请遵循GPL  
InBlock.gif*Thanks to: Paul Sheer 感谢Paul Sheer在select_tut的man手册里提供了这份源代码  
InBlock.gif*Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力  
InBlock.gif*Note:要使用此函数需要自定义一个全局变量char errorMessage[1024];并包含自己编写的GetCurrentTime.h头文件  
ExpandedBlockEnd.gif********************************************************************
*/
  
None.gif int  
ExpandedBlockStart.gifSend( int sock,  char * buf, size_t size,  int flag,  int timeout)     {   
InBlock.gif    int i = 0, ret = 0, intretry = 0;   
InBlock.gif  
InBlock.gif    struct timeval tival;   
InBlock.gif    fd_set writefds;   
InBlock.gif    int maxfds = 0;   
InBlock.gif  
InBlock.gif    tival.tv_sec = timeout;   
InBlock.gif    tival.tv_usec = 0;   
InBlock.gif  
InBlock.gif    FD_ZERO(&writefds);   
InBlock.gif  
ExpandedSubBlockStart.gif    if(sock > 0) {   
InBlock.gif        FD_SET(sock, &writefds);   
InBlock.gif        maxfds=((sock > maxfds)?sock:maxfds);   
ExpandedSubBlockEnd.gif    }
   
ExpandedSubBlockStart.gif    else    {   
InBlock.gif        sprintf(errorMessage, "Send socket:%d error! return:-2 %s", sock, GetCurrentTime(0, 0));   
InBlock.gif        return -2;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
InBlock.gif    ret = select(maxfds + 1, NULL, &writefds, NULL, &tival);   
ExpandedSubBlockStart.gif    if(ret <= 0) {   
InBlock.gif        if(ret < 0)    sprintf(errorMessage, "Send socket:%d select() error! return:%d, errno=%d, errortext:'%s' %s", sock, ret, errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif        else sprintf(errorMessage, "Send socket:%d select timeout(%d)! %s", sock, timeout, GetCurrentTime(0, 0));   
InBlock.gif        close(sock);   
InBlock.gif        return -3;   
ExpandedSubBlockEnd.gif    }
   
ExpandedSubBlockStart.gif    if(!(FD_ISSET(sock, &writefds)))    {   
InBlock.gif        sprintf(errorMessage, "Send socket:%d not in writefds! %s", sock, GetCurrentTime(0, 0));   
InBlock.gif        close(sock);   
InBlock.gif        return -4;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
ExpandedSubBlockStart.gif    while(i < size)    {   
InBlock.gif        ret = send(sock, buf + i, size - i, flag);   
ExpandedSubBlockStart.gif        if(ret <= 0)    {   
InBlock.gif            sprintf(errorMessage, "Send socket:%d send() error! return:%d, errno=%d, errortext:'%s' %s", sock, ret, errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif  
InBlock.gif            if (EINTR == errno)   
ExpandedSubBlockStart.gif              if(intretry < 10)  {intretry++;continue;}   
InBlock.gif              else sprintf(errorMessage, "Send socket:%d send() error!EINTR 10 times! %s", sock, GetCurrentTime(0, 0));   
InBlock.gif  
InBlock.gif            close(sock);   
InBlock.gif            return -1;   
ExpandedSubBlockEnd.gif        }
   
InBlock.gif        else i += ret;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif    sprintf(errorMessage, "Send socket:%d send() OK! %d/%d bytes sent! %s", sock, i, size, GetCurrentTime(0, 0));   
InBlock.gif    return i;   
ExpandedBlockEnd.gif}
   

ExpandedBlockStart.gif /*************************关于本函数************************************
InBlock.gif*function_name: Send
InBlock.gif*参数说明:sock整数型socket,buf待发送的内容,size要发送的大小,flag发送选项,timeout超时时间值
InBlock.gif*purpose: 用来通过一个socket在指定时间内发送数据
InBlock.gif*tidied by: zhoulifa(zhoulifa@163.com) 周立发(
http://zhoulifa.9999mb.com)
InBlock.gifLinux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言
InBlock.gif*date time:2006-07-05 20:58:00
InBlock.gif*Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途
InBlock.gif* 但请遵循GPL
InBlock.gif*Thanks to: Paul Sheer 感谢Paul Sheer在select_tut的man手册里提供了这份源代码
InBlock.gif*Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力
InBlock.gif*Note:要使用此函数需要自定义一个全局变量char errorMessage[1024];并包含自己编写的GetCurrentTime.h头文件
ExpandedBlockEnd.gif********************************************************************
*/

None.gif int
None.gifSend( int sock,  char * buf, size_t size,  int flag,  int timeout)    
ExpandedBlockStart.gif {
InBlock.gif    int i = 0, ret = 0, intretry = 0;
InBlock.gif
InBlock.gif    struct timeval tival;
InBlock.gif    fd_set writefds;
InBlock.gif    int maxfds = 0;
InBlock.gif
InBlock.gif    tival.tv_sec = timeout;
InBlock.gif    tival.tv_usec = 0;
InBlock.gif
InBlock.gif    FD_ZERO(&writefds);
InBlock.gif
ExpandedSubBlockStart.gif    if(sock > 0) {
InBlock.gif        FD_SET(sock, &writefds);
InBlock.gif        maxfds=((sock > maxfds)?sock:maxfds);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gif    else    {
InBlock.gif        sprintf(errorMessage, "Send socket:%d error! return:-2 %s", sock, GetCurrentTime(0, 0));
InBlock.gif        return -2;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    ret = select(maxfds + 1, NULL, &writefds, NULL, &tival);
ExpandedSubBlockStart.gif    if(ret <= 0) {
InBlock.gif        if(ret < 0)    sprintf(errorMessage, "Send socket:%d select() error! return:%d, errno=%d, errortext:'%s' %s", sock, ret, errno, strerror(errno), GetCurrentTime(0, 0));
InBlock.gif        else sprintf(errorMessage, "Send socket:%d select timeout(%d)! %s", sock, timeout, GetCurrentTime(0, 0));
InBlock.gif        close(sock);
InBlock.gif        return -3;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gif    if(!(FD_ISSET(sock, &writefds)))    {
InBlock.gif        sprintf(errorMessage, "Send socket:%d not in writefds! %s", sock, GetCurrentTime(0, 0));
InBlock.gif        close(sock);
InBlock.gif        return -4;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    while(i < size)    {
InBlock.gif        ret = send(sock, buf + i, size - i, flag);
ExpandedSubBlockStart.gif        if(ret <= 0)    {
InBlock.gif            sprintf(errorMessage, "Send socket:%d send() error! return:%d, errno=%d, errortext:'%s' %s", sock, ret, errno, strerror(errno), GetCurrentTime(0, 0));
InBlock.gif
InBlock.gif            if (EINTR == errno)
ExpandedSubBlockStart.gif              if(intretry < 10)  {intretry++;continue;}
InBlock.gif              else sprintf(errorMessage, "Send socket:%d send() error!EINTR 10 times! %s", sock, GetCurrentTime(0, 0));
InBlock.gif
InBlock.gif            close(sock);
InBlock.gif            return -1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        else i += ret;
ExpandedSubBlockEnd.gif    }

InBlock.gif    sprintf(errorMessage, "Send socket:%d send() OK! %d/%d bytes sent! %s", sock, i, size, GetCurrentTime(0, 0));
InBlock.gif    return i;
ExpandedBlockEnd.gif}

接收数据函数Recv
ExpandedBlockStart.gif /*************************关于本函数************************************  
InBlock.gif*function_name: Recv  
InBlock.gif*参数说明:sock整数型socket,buf接收数据的缓冲区,size要接收数据的大小,flag接收选项,timeout超时时间值  
InBlock.gif*purpose: 用来从一个socket在指定时间内读取数据  
InBlock.gif*tidied by: zhoulifa(zhoulifa@163.com) 周立发(
http://zhoulifa.9999mb.com)  
InBlock.gifLinux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言  
InBlock.gif*date time:2006-07-05 21:10:00  
InBlock.gif*Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途  
InBlock.gif* 但请遵循GPL  
InBlock.gif*Thanks to: Paul Sheer 感谢Paul Sheer在select_tut的man手册里提供了这份源代码  
InBlock.gif*Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力  
InBlock.gif*Note:要使用此函数需要自定义一个全局变量char errorMessage[1024];并包含自己编写的GetCurrentTime.h头文件  
ExpandedBlockEnd.gif********************************************************************
*/
 
None.gif int  
None.gifRecv( int sock,  char * buf, size_t size,  int flag,  int timeout)    
ExpandedBlockStart.gif {   
InBlock.gif    int i = 0, ret = 0, intretry = 0;   
InBlock.gif  
InBlock.gif    struct timeval tival;   
InBlock.gif    fd_set readfds;   
InBlock.gif    int maxfds = 0;   
InBlock.gif  
InBlock.gif    tival.tv_sec = timeout;   
InBlock.gif    tival.tv_usec = 0;   
InBlock.gif  
InBlock.gif    FD_ZERO(&readfds);   
InBlock.gif  
ExpandedSubBlockStart.gif    if(sock > 0) {   
InBlock.gif        FD_SET(sock, &readfds);   
InBlock.gif        maxfds=((sock > maxfds)?sock:maxfds);   
ExpandedSubBlockEnd.gif    }
   
ExpandedSubBlockStart.gif    else    {   
InBlock.gif        sprintf(errorMessage, "Recv socket:%d error! return:-2 %s", sock, GetCurrentTime(0, 0));   
InBlock.gif        return -2;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
InBlock.gif    ret = select(maxfds + 1, &readfds, NULL, NULL, &tival);   
ExpandedSubBlockStart.gif    if(ret <= 0) {   
InBlock.gif        if(ret < 0)    sprintf(errorMessage, "Recv socket:%d select() error! return:%d, errno=%d, errortext:'%s' %s", sock, ret, errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif        else sprintf(errorMessage, "Recv socket:%d select timeout(%d)! %s", sock, timeout, GetCurrentTime(0, 0));   
InBlock.gif        close(sock);   
InBlock.gif        return -3;   
ExpandedSubBlockEnd.gif    }
   
ExpandedSubBlockStart.gif    if(!(FD_ISSET(sock, &readfds)))    {   
InBlock.gif        sprintf(errorMessage, "Recv socket:%d not in readfds! %s", sock, GetCurrentTime(0, 0));   
InBlock.gif        close(sock);   
InBlock.gif        return -4;   
ExpandedSubBlockEnd.gif    }
   
ExpandedSubBlockStart.gif    while(i < size)    {   
InBlock.gif        ret = recv(sock, buf + i, size - i, flag);   
ExpandedSubBlockStart.gif        if(ret <= 0){   
InBlock.gif            sprintf(errorMessage, "Recv socket:%d recv() error! return:%d, errno=%d, errortext:'%s' %s", sock, ret, errno, strerror(errno), GetCurrentTime(0, 0));   
InBlock.gif            if(errno == EINTR)      
ExpandedSubBlockStart.gif                if(intretry < 10)  {intretry++;continue;}   
InBlock.gif                else sprintf(errorMessage, "Recv socket:%d recv() error! EINTR 10 times! %s", sock, GetCurrentTime(0, 0));   
InBlock.gif            close(sock);   
InBlock.gif            return -1;   
ExpandedSubBlockEnd.gif        }
   
InBlock.gif        else i += ret;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif    sprintf(errorMessage, "Recv socket:%d recv() OK! %d/%d bytes received! %s", sock, i, size, GetCurrentTime(0, 0));   
InBlock.gif    return i;   
ExpandedBlockEnd.gif}
   
目录
相关文章
|
2天前
|
关系型数据库 MySQL Linux
服务器Linux系统配置mysql数据库主从自动备份
这是一个基本的配置主从复制和设置自动备份的指南。具体的配置细节和命令可能因您的环境和需求而有所不同,因此建议在操作前详细阅读MySQL文档和相关资源,并谨慎操作以避免数据丢失或不一致。
11 3
|
3天前
|
Linux C语言
|
4天前
|
Oracle Java 关系型数据库
【服务器】python通过JDBC连接到位于Linux远程服务器上的Oracle数据库
【服务器】python通过JDBC连接到位于Linux远程服务器上的Oracle数据库
14 6
|
6天前
|
运维 监控 安全
2023年最详细的:本地Linux服务器安装宝塔面板,并内网穿透实现公网远程登录
2023年最详细的:本地Linux服务器安装宝塔面板,并内网穿透实现公网远程登录
|
6天前
|
Ubuntu Android开发 数据安全/隐私保护
【Android平板编程】远程Ubuntu服务器Code-Server编程写代码
【Android平板编程】远程Ubuntu服务器Code-Server编程写代码
|
7天前
|
存储 安全 算法
【Linux | C++ 】基于环形队列的多生产者多消费者模型(Linux系统下C++ 代码模拟实现)
【Linux | C++ 】基于环形队列的多生产者多消费者模型(Linux系统下C++ 代码模拟实现)
22 0
|
7天前
|
算法 Linux 数据安全/隐私保护
【Linux | C++ 】生产者消费者模型(Linux系统下C++ 代码模拟实现)
【Linux | C++ 】生产者消费者模型(Linux系统下C++ 代码模拟实现)
12 0
|
8天前
|
Linux 网络安全 数据库
linux centos系统搭建samba文件服务器 NetBIOS解析 (超详细)
linux centos系统搭建samba文件服务器 NetBIOS解析 (超详细)
|
3天前
|
安全 网络协议 Linux
linux必学的60个命令
Linux是一个功能强大的操作系统,提供了许多常用的命令行工具,用于管理文件、目录、进程、网络和系统配置等。以下是Linux必学的60个命令的概览,但请注意,这里可能无法列出所有命令的完整语法和选项,仅作为参考