m1cRo
Posts: 9 Join date: 2008-04-30
 | Subject: MySocketServer Class Wed Apr 30, 2008 2:53 pm | |
| Paidia edw exw enan server me asynchrona sockets  MySocketServer.h | Code: | /* ** Written by m1cRo ** e-mail: m1cRo@mail.com** http://www.hack.dom.gr */ //--------------------------------------------------------------------------- #include <winsock2.h> #include <string> #include <windows.h> #include <vector.h> #include "socketException.h" #ifndef MySocketServerH #define MySocketServerH using namespace std; class MySocketServer{ private : SOCKET sock_; HANDLE acceptThreadHandle_; vector<SOCKET> clientSockList_; vector<SOCKET>::iterator nextSocket_; CRITICAL_SECTION sect_; public : /*============================================= * Event Functions *///=========================================== virtual void onAccept(SOCKET sock,sockaddr_in clientAddr); virtual void onReceive(SOCKET sock,char *recBuf,int recLen); virtual void onDisconnect(SOCKET sock); virtual void onConnectionError(SOCKET sock); //============================================= MySocketServer(unsigned short listenPort) throw(SocketException); void loadLibrary(void) throw(SocketException); void removeFromClientSockList(SOCKET sock); void acceptFunction(void); void startReceive(void); void startListen(int maxClients); void disconnectClient(SOCKET sock); vector<SOCKET> getClientSockList(void); static DWORD WINAPI startAcceptThread(LPVOID param){ MySocketServer* _this=(MySocketServer*)param; _this->acceptFunction(); return 0; } ~MySocketServer(); }; #endif |
MySocketServer.cpp
| Code: | //---------------------------------------------------------------------------
#include "MySocketServer.h" #pragma hdrstop
void MySocketServer::acceptFunction(){ while(1){ InitializeCriticalSection(§_); EnterCriticalSection(§_); sockaddr_in cAddr; SOCKET cSock; int caddrSize=sizeof(sockaddr); cSock=accept(this->sock_,(sockaddr*)&cAddr,&caddrSize); if(cSock!=INVALID_SOCKET){ ULONG uBlock=1; ioctlsocket(cSock,FIONBIO,&uBlock); onAccept(cSock,cAddr); clientSockList_.push_back(cSock); } LeaveCriticalSection(§_); } }
void MySocketServer::loadLibrary(void) throw(SocketException){ WSAData data; if(WSAStartup(MAKEWORD(2,2),&data)<0){ throw SocketException("Load WinSock library Error"); } sock_=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(sock_==INVALID_SOCKET){ throw SocketException("Create Socket error"); } }
void MySocketServer::removeFromClientSockList(SOCKET sock){ for(nextSocket_=clientSockList_.begin();nextSocket_!=clientSockList_.end();nextSocket_++){ if(sock==*nextSocket_._Myptr){ clientSockList_.erase(nextSocket_,nextSocket_+1); break; } } }
MySocketServer::MySocketServer(unsigned short listenPort) throw(SocketException){ loadLibrary(); sockaddr_in addr; addr.sin_family=AF_INET; addr.sin_port=htons(listenPort); addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
if(bind(sock_,(sockaddr*)&addr,sizeof(sockaddr))<0){ throw SocketException("Bind Socket Error"); } }
void MySocketServer::startReceive(void){ while(1){ Sleep(100); for(nextSocket_=clientSockList_.begin();nextSocket_!=clientSockList_.end();++nextSocket_){ char recBuf[1025]; memset(recBuf,0,1025); int recLen=recv(*nextSocket_._Myptr,recBuf,1024,0); int opt=0; int len=sizeof(opt); if (getsockopt(*nextSocket_._Myptr,SOL_SOCKET,SO_ERROR, (char*)&opt, &len)==SOCKET_ERROR){ onConnectionError(*nextSocket_._Myptr); clientSockList_.erase(nextSocket_,nextSocket_+1); break; } int err=WSAGetLastError(); if(recLen==0){ onDisconnect(*nextSocket_._Myptr); clientSockList_.erase(nextSocket_,nextSocket_+1); break; }else if(recLen>0){ onReceive(*nextSocket_._Myptr,recBuf,recLen); } } } }
void MySocketServer::startListen(int maxClients){ if(listen(sock_,maxClients)<0){ throw SocketException("Listen Socket Error"); } DWORD dwThreadId; acceptThreadHandle_=CreateThread(0,0,startAcceptThread,this,0,&dwThreadId); clientSockList_.clear(); if(acceptThreadHandle_==NULL){ throw SocketException("Create Accept Thread Error"); } startReceive(); }
MySocketServer::~MySocketServer(){ this->clientSockList_.clear(); shutdown(sock_,SD_BOTH); closesocket(sock_); WSACleanup(); }
void MySocketServer::disconnectClient(SOCKET sock){ shutdown(sock,SD_BOTH); closesocket(sock); removeFromClientSockList(sock); }
vector<SOCKET> MySocketServer::getClientSockList(void){ return clientSockList_; } #pragma package(smart_init)
void MySocketServer::onReceive(SOCKET sock,char *recBuf,int recLen){ }
void MySocketServer::onAccept(SOCKET sock,sockaddr_in clientAddr){ }
void MySocketServer::onDisconnect(SOCKET sock){ }
void MySocketServer::onConnectionError(SOCKET sock){ }
|
SocketException.h
| Code: | //--------------------------------------------------------------------------- #include <exception> #include <string>
#ifndef SocketExceptionH #define SocketExceptionH
using namespace std; class SocketException : public exception { public: /** * Construct a SocketException with a explanatory message. * @param message explanatory message * @param incSysMsg true if system message (from strerror(errno)) * should be postfixed to the user provided message */ SocketException(const string &message, bool inclSysMsg = false) throw();
/** * Provided just to guarantee that no exceptions are thrown. */ ~SocketException() throw(); /** * Get the exception message * @return exception message */ const char *what() const throw();
private: string userMessage; // Exception message }; #endif
|
SocketException.cpp
| Code: | //---------------------------------------------------------------------------
#pragma hdrstop
#include "SocketException.h"
/* SOCKET EXCEPTION CLASS REFERENCES */ SocketException::SocketException(const string &message, bool inclSysMsg) throw() : userMessage(message) { if (inclSysMsg) { userMessage.append(": "); userMessage.append(strerror(errno)); } }
SocketException::~SocketException() throw() { }
const char *SocketException::what() const throw() { return userMessage.c_str(); }
#pragma package(smart_init)
|
Paidia gia na doulepsei kai allou apla alllaxte ligo tis sinariseis me to vector Sto c++ builder douleuei sigoura .
http://rapidshare.com/files/111498157/AsynchroniusSocketServer.zip.html
Last edited by m1cRo on Wed May 07, 2008 10:35 am; edited 1 time in total |
|
Spoofer Broadcaster

Posts: 14 Join date: 2008-04-29
 | Subject: Re: MySocketServer Class Wed Apr 30, 2008 3:44 pm | |
| Tha protimousa na evlepa kati paromoio se C wraio m1cRo kai as mpainoume ligaki oloi sto klima na prosferoume stous gurw mas. Be open source. ps: Gia osous o kwdikas fainetai akanohtos kai theloun na ton katalavoun mporoun na diavasoun gia asynchronous socket programming. |
|
r1Nu- Admin


Posts: 13 Join date: 2008-04-24 Age: 19 Location: 192.178.**.**
 | |
m1cRo
Posts: 9 Join date: 2008-04-30
 | Subject: Re: MySocketServer Class Sat May 24, 2008 4:16 pm | |
| |
|
m1cRo
Posts: 9 Join date: 2008-04-30
 | Subject: Re: MySocketServer Class Mon May 26, 2008 12:55 pm | |
| |
|