//
// File: client.h
// Author: root
//
// Created on February 19, 2008, 9:36 AM
//
#ifndef _CLIENT_H
#define _CLIENT_H
#include <sys/socket.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
class client{
public:
enum {BUFFERSIZE = 1024};
client(int fd,struct sockaddr_in sinaddr,int timeout);
~client();
bool update();
string _value;
private:
int _fd;
int _timeout;
char *buffer;
};
#endif /* _CLIENT_H */
#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;
}
//
// File: server.h
// Author: root
//
// Created on February 19, 2008, 9:34 AM
//
#ifndef _SERVER_H
#define _SERVER_H
#include <vector>
#include <iostream>
#include <string>
#include "client.h"
using namespace std;
class server{
public:
server(int port);
~server();
bool update(bool block);
vector<client *> clients;
enum {MAXCLIENTS = 11};
void kill(){
_kill = true;
}
private:
int _fd;
bool _kill;
};
#endif /* _SERVER_H */
#include "server.h"
server::server(int port = 10000){
_kill = false;
struct sockaddr_in sin;
memset( &sin, 0, sizeof( sin ) );
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
_fd = socket( PF_INET, SOCK_STREAM, 0 );
int optval = 1;
setsockopt( _fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof( int ) );
if( bind( _fd, (struct sockaddr *)&sin, sizeof( sin ) ) == -1 ) {
cout << "bind error" << endl;
}
// listen, queue length 8
if( listen( _fd, 8 ) == -1 ) {
cout << "listen error" << endl;
//exit( 1 );
}
}
server::~server(){
close(_fd);
for( vector<client *> :: iterator i = clients.begin( ); i != clients.end( ); i++ )
delete *i;
clients.clear( );
}
bool server::update(bool block){
if( clients.size( ) < MAXCLIENTS ) {
fd_set fdServer;
FD_ZERO( &fdServer );
FD_SET( _fd, &fdServer );
struct timeval tv;
if( block ) {
// block for 100 ms to keep from eating up all cpu time
tv.tv_sec = 0;
tv.tv_usec = 100000;
}
else {
tv.tv_sec = 0;
tv.tv_usec = 0;
}
if( select( _fd + 1, &fdServer, NULL, NULL, &tv ) == -1 )
{
FD_ZERO( &fdServer );
usleep( 100 );
}
if( FD_ISSET( _fd, &fdServer ) ) {
struct sockaddr_in adrFrom;
int iAddrLen = sizeof( adrFrom );
int sckClient;
if( ( sckClient = accept( _fd, (struct sockaddr *)&adrFrom, (socklen_t *)&iAddrLen ) ) == -1 )
cout << "accept error" << endl;
else{
clients.push_back( new client( sckClient, adrFrom, 0) );
cout << "clients comes" << endl;
}
}
}
else {
// maximum connections reached
usleep( 100 );
}
// process
for( vector<client *> :: iterator i = clients.begin( ); i != clients.end( ); ) {
if( (*i)->update( ) ) {
delete *i;
i = clients.erase( i );
cout << "clients exit" << endl;
}
else
i++;
}
return _kill;
}
分享到:
相关推荐
C# socket封装类和组件含例程全部开源,可以说全网封装最好的C#类,十分强大,亲测很好,值得参考和学习
C# Socket封装类和组件源码,非常适合新手学习
开源socket封装类SimpleSocket,学习网络编程很不错的代码
vc socket封装类通讯CAsyncSocket,支持多客户端连接,通讯。
本人为套接字编程更方便,将SOCKET封装为两个类CServer和CClient。另外配一个用于流式套接字传输的数据包类。
主要介绍了C++自定义封装socket操作业务类,结合完整实例形式分析了Linux环境下C++操作socket的封装业务类,可实现基本的socket连接、参数设置、发送请求等基本功能,需要的朋友可以参考下
使用vc,对socket进行了类封装,同时采用多线程处理的方式。对于初学者和使用者有很好的借鉴价值。
一份很不错的Socket封装的代码,设计思想很好,很实用!大家一起学习一下。
经过封装的socket类,内附简单的pop3和http协议的简单应用源码
c# socket tcp 通信案例 c# socket tcp 通信案例 c# socket tcp 通信案例
A simple application using IO Completion Ports and WinSock。老外写的IOCP封装类,使用了线程池和内存池。很不错的东西.资源里面有文档和例子程序
代码为codeproject源码,分3个专辑讲解的很详细。有具体的封装类。并且都有文档和例子程序。是学习网络编程的好东西
C# Socket通讯框架,封装了Client和Server端类库,带演示实例和使用手册.
delphi socket udp 封装类 很好用的。
简化C# socket连接直接使用该类进行socket 连接
通过重新整理,打造了一个SOCKET封装类,能够高效的完成目前的通信问题,只需直接调用即可。
类c++封装的一个socket类,对新手还是有点帮助
完全用API封装操作socket,适合不使用MFC的场合。
socket封装库,有服务器和客户端,客户端和服务器集于一体,可以相互发送消息。
waisock-功能齐全的C语言socket及SSL封装类及使用例子源代码。内含调用socket封装类和监控通信的例子,其中监控代理和监控终端可以直接在自己的项目中使用。