⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpclient.cpp

📁 TCP的服务器与客户端基类
💻 CPP
字号:
#include "TcpClient.h"

/*
 ****************************************************************************
 *                           CLASS IMPLEMENTATION                           *
 ****************************************************************************
 */

/* for CTcpClient */

/*
 ****************************************************************************
 *                                                                          *
 *  Function Name : CTcpClient()                                            *
 *  Description   : The default constructor. However, it has been disabled. *
 *  Parameter(s)  : None.                                                   *
 *  Return        : None.                                                   *
 *                                                                          *
 ****************************************************************************
 */

CTcpClient::CTcpClient(void)
{
}
 
/*
 ****************************************************************************
 *                                                                          *
 *  Function Name : CTcpClient()                                            *
 *  Description   : The construct function.                                 *
 *  Parameter(s)  : @tgt : the target's ip address in nework order.         *
 *  Return        :                                                         *
 *                                                                          *
 ****************************************************************************
 */

CTcpClient::CTcpClient(unsigned long tgt, short port) : pt(port), dest(tgt)
{
    #ifdef __linux
    sock = 0;
    #endif
}

/*
 ****************************************************************************
 *                                                                          *
 *  Function Name : ~CTcpClient()                                           *
 *  Description   : The deconstruct function.                               *
 *  Parameter(s)  : None.                                                   *
 *  Return        : None.                                                   *
 *                                                                          *
 ****************************************************************************
 */

CTcpClient::~CTcpClient(void)
{
    #ifdef __linux
    if (sock > 0)
    {
        close(sock);
    }
    #endif

    #ifdef WIN32
    closesocket(sock);
    WSACleanup();
    #endif
}

/*
 ****************************************************************************
 *                                                                          *
 *  Function Name : Init()                                                  *
 *  Description   : To initialize the tcp client.                           *
 *  Parameter(s)  : None.                                                   *
 *  Return        : @ 0 : success.                                          *
 *                  @-1 : failure.                                          *
 *                                                                          *
 ****************************************************************************
 */

int CTcpClient::Init(void)
{
    int ret = 0;

    #ifdef WIN32
    WinVersion = MAKEWORD(2, 2);
    if (WSAStartup(WinVersion, &wsa) < 0)
    {
        DBG("TcpClient::Init() WSAStartup failure!\n");
	return -1;
    }
    #endif

    sock = socket(AF_INET, SOCK_STREAM, 0);
    #ifdef __linux
    if (sock < 0)
    #else
    if (sock == INVALID_SOCKET)
    #endif
    {
        DBG("CTcpClient::Init() Create Socket Error!\n");
	return -1;
    }

    struct sockaddr_in serv;
    memset(&serv, 0, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_port   = htons(pt);
    serv.sin_addr.s_addr = dest;

    ret = connect(sock, (struct sockaddr *)(&serv), sizeof(serv));
    #ifdef __linux
    if (ret < 0)
    #else
    if (ret == SOCKET_ERROR)
    #endif
    {
        DBG("CTcpClient::Init() connect error!\n");
	return -1;
    }

    return 0;
}
 
/*
 ****************************************************************************
 *                                                                          *
 *  Function Name : read()                                                  *
 *  Description   : To receive a content buffer from a socket.              *
 *  Parameter(s)  : @buf : A pointer to which content will be stored.       *
 *                  @len : The length of the buffer.                        *
 *  Return        : @>0  : The bytes of read is returned.                   *
 *                  @ 0  : The peer has been shut down.                     *
 *                  @-1  : Failure.                                         *
 *                                                                          *
 ****************************************************************************
 */

__int32_t CTcpClient::Read(void *buf, __u_int len)
{
    __int32_t ret = 0;

    #ifdef __linux
    ret = recv(sock, buf, len, 0);
    if (ret < 0)
    #else
    ret = recv(sock, reinterpret_cast<char *>(buf), len, 0);
    if (ret == SOCKET_ERROR)
    #endif
    {
        DBG("CTcpClient::Read() recv error!\n");
	return -1;
    }

    return ret;
}

/*
 ****************************************************************************
 *                                                                          *
 *  Function Name : Write()                                                 *
 *  Description   : To send a content buffer.                               *
 *  Parameter(s)  : @buf : A pointer to which the content is stored.        *
 *                  @len : The length of the buffer.                        *
 *  Return        : @>0  : The bytes of send is returned.                   *
 *                  @-1  : Some error has occured.                          *
 *                                                                          *
 ****************************************************************************
 */

__int32_t CTcpClient::Write(void *buf, __u_int len)
{
    __int32_t ret = 0;

    #ifdef __linux
    ret = send(sock, buf, len, 0);
    if (ret < 0)
    #else
    ret = send(sock, reinterpret_cast<char *>(buf), len, 0);
    if (ret == SOCKET_ERROR)
    #endif
    {
        DBG("CTcpClient::Write send data error!\n");
	return -1;
    }

    return ret;
}

/*
 ****************************************************************************
 *                                                                          *
 *  Function Name : ReadWithinTime()                                        *
 *  Description   : To receive a data buffer within a given time. If nothing*
 *                  is available, then break.                               *
 *  Parameter(s)  : @buf : a pointer to which the buffer was received.      *
 *                  @len : the length of the content buffer.                *
 *                  @sec : the maximum time to wait.                        *
 *  Return        : @ >0 : the bytes of read is returned.                   *
 *                  @ =0 : the expected time is expired.                    *
 *                  @ <0 : some error have occured.                         *
 *                                                                          *
 ****************************************************************************
 */

__int32_t CTcpClient::ReadWithinTime(void *buf, __u_int len, int sec)
{
    __int32_t ret = 0;

    fd_set read_set;
    struct timeval tv;
    memset(&tv, 0, sizeof(tv));

    tv.tv_sec  = sec;
    tv.tv_usec = 0;

    FD_ZERO(&read_set);
    FD_SET(sock, &read_set);

    #ifdef __linux
    ret = select(sock + 1, &read_set, NULL, NULL, &tv);
    if (ret <= 0)
    {
        return ret;
    }
    #endif
    
    #ifdef WIN32
    ret = select(0, &read_set, NULL, NULL, &tv);
    if (ret == SOCKET_ERROR)
    {
        return -1;
    }
    if (ret == 0)
    {
        return 0;
    }
    #endif

    #ifdef __linux
    ret = recv(sock, buf, len, 0);
    if (ret < 0)
    #else 
    ret = recv(sock, reinterpret_cast<char *>(buf), len, 0);
    if (ret == SOCKET_ERROR)
    #endif
    {
        return -1;
    }

    return ret;
}

/* the end */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -