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

📄 client.cc

📁 Linux下使用Select模型的两个类Client And Server
💻 CC
字号:
#include "client.h"client::client(int fd, sockaddr_in sinaddr, int timeout){    _fd = fd;    _timeout = timeout;    fcntl( _fd, F_SETFL, fcntl( _fd, F_GETFL ) | O_NONBLOCK );    buffer = NULL;    buffer = new char[BUFFERSIZE];}client::~client(){    close(_fd);    delete buffer;    buffer = NULL;}bool client::update(){    fd_set fdClient;        FD_ZERO( &fdClient );    FD_SET( _fd, &fdClient );        struct timeval tv;        tv.tv_sec = _timeout;    tv.tv_usec = 0;            if( select( _fd + 1, &fdClient, NULL, NULL, &tv ) == -1 ) {        return true;    }        if( FD_ISSET( _fd, &fdClient ) ) {        memset( buffer, 0, sizeof( char ) * BUFFERSIZE );                int c = recv( _fd, buffer, BUFFERSIZE, 0 );                if( c == -1 && errno != EWOULDBLOCK ) {            if( errno != ECONNRESET )                cout << "peer reset the connection" << endl;            return true;        }        else if( c == 0 )            return true;        else if( c > 0 ) {            _value = string( buffer, c );            cout << _value << endl;        }        else {            return true;        }    }    return false;}

⌨️ 快捷键说明

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