This is my socket.cpp
#pragma hdrstop
#include "MySocketClient.h"
MySocketClient::MySocketClient() throw(SocketException){
WSAData data;
InitializeCriticalSection(§_);
if(WSAStartup(MAKEWORD(2,2),&data)!=0){
throw SocketException("Cant load WinSock library");
}
connected_=false;
}
//-----------------------------------------------------------------------------
MySocketClient::~MySocketClient(){
WSACleanup();
}
//-----------------------------------------------------------------------------
void MySocketClient::receive(void){
char recBuf[1024];
while(1){
EnterCriticalSection(§_);
memset(recBuf,0,1024);
int recLen=recv(sock_,recBuf,1024,0);
if(recLen==0){
onDisconnect(sock_);
connected_=false;
break;
}else if(recLen<0){
connected_=false;
onConnectionError(sock_);
break;
}else{
onReceive(sock_,recBuf,recLen);
}
LeaveCriticalSection(§_);
}
}
//------------------------------------------------------------------------------
void MySocketClient::sendData(char *sendBuf,int sendLen) throw(SocketException){
onSendData(sendBuf,sendLen);
if(send(sock_,sendBuf,sendLen,0)<0){
onConnectionError(sock_);
}
Sleep(200);
}
//------------------------------------------------------------------------------
void MySocketClient::Connect(string hostname,unsigned short port) throw(SocketException){
sock_=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sock_==INVALID_SOCKET){
throw SocketException("Cant create socket");
}
HOSTENT *host=gethostbyname(hostname.c_str());
if(host==NULL){
throw SocketException("Cant resolve host");
}
addr_.sin_family=AF_INET;
addr_.sin_port=htons(port);
addr_.sin_addr.S_un.S_addr=*(unsigned long*)host->h_addr_list[0];
if(connect(sock_,(sockaddr*)&addr_,sizeof(sockaddr))==SOCKET_ERROR){
throw SocketException("Connect Error");
}
DWORD dwThreadId;
threadHandle_=CreateThread(NULL,0,startReceive,this,0,&dwThreadId);
if(threadHandle_==NULL){
connected_=false;
shutdown(sock_,SD_BOTH);
closesocket(sock_);
throw SocketException("Receive thread create error");
}
connected_=true;
onConnect(sock_);
}
//------------------------------------------------------------------------------
void MySocketClient::Disconnect(void){
TerminateThread(threadHandle_,0);
shutdown(sock_,SD_BOTH);
closesocket(sock_);
}
//Virtual functions
void MySocketClient::onReceive(SOCKET sock,char *recBuf,int recLen) throw(SocketException){
}
void MySocketClient::onDisconnect(SOCKET sock){
}
void MySocketClient::onConnectionError(SOCKET sock){
}
void MySocketClient::onSendData(char *sendBuf,int sendLen){
}
void MySocketClient::onConnect(SOCKET sock){
}
#pragma package(smart_init)
This is mysocket.h
//---------------------------------------------------------------------------
#ifndef MySocketClientH
#define MySocketClientH
#include <winsock2.h>
#include <string.h>
#include "socketException.h"
#include <process.h>
using namespace std;
class MySocketClient{
private :
sockaddr_in addr_;
unsigned short port_;
string hostname_;
CRITICAL_SECTION sect_;
HANDLE threadHandle_;
bool connected_;
protected :
SOCKET sock_;
//Virtual functions
virtual void onConnect(SOCKET sock);
virtual void onReceive(SOCKET sock,char *recBuf,int recLen) throw(SocketException);
virtual void onDisconnect(SOCKET sock) ;
virtual void onConnectionError(SOCKET sock);
virtual void onSendData(char *sendBuf,int sendLen);
private :
void receive(void);
static DWORD WINAPI startReceive(LPVOID param){
MySocketClient *_this=(MySocketClient*)param;
_this->receive();
return 0;
}
public :
MySocketClient() throw(SocketException);
~MySocketClient();
void sendData(char *sendBuf,int sendLen) throw(SocketException);
void Connect(string hostname,unsigned short port) throw(SocketException);
void Disconnect(void);
bool isConnected(void){
return connected_;
}
};
#endif
The question that i asked is on the followink link https://stackoverflow.com/questions/12471761/creating-multiple-tcp-socket-connections