Fixed connection split up connect function and added initiateClient()
This commit is contained in:
parent
640ac2172a
commit
82b8ef7b05
|
@ -12,14 +12,6 @@ Connection::~Connection()
|
||||||
|
|
||||||
bool Connection::Connect(unsigned short port , const char serverName[])
|
bool Connection::Connect(unsigned short port , const char serverName[])
|
||||||
{
|
{
|
||||||
this->socket = ::socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if(this->socket == SOCKET_ERROR)
|
|
||||||
{
|
|
||||||
//error opening socket
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct hostent *hostEnt;
|
struct hostent *hostEnt;
|
||||||
if((hostEnt = gethostbyname(serverName)) == NULL)
|
if((hostEnt = gethostbyname(serverName)) == NULL)
|
||||||
{
|
{
|
||||||
|
@ -32,19 +24,10 @@ bool Connection::Connect(unsigned short port , const char serverName[])
|
||||||
server.sin_port = htons(port);
|
server.sin_port = htons(port);
|
||||||
server.sin_addr.s_addr = *(unsigned long*) hostEnt->h_addr;
|
server.sin_addr.s_addr = *(unsigned long*) hostEnt->h_addr;
|
||||||
|
|
||||||
while(1)
|
if(connect(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
if(connect(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
//Error connecting to server
|
||||||
{
|
return false;
|
||||||
//Error connecting to server
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Sleep(10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//connection succesfull!
|
//connection succesfull!
|
||||||
|
@ -77,11 +60,23 @@ bool Connection::InitiateServer(unsigned short port)
|
||||||
{
|
{
|
||||||
//"Listen failed!
|
//"Listen failed!
|
||||||
closesocket(this->socket);
|
closesocket(this->socket);
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Server started!
|
//Server started!
|
||||||
return this->socket;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Connection::InitiateClient()
|
||||||
|
{
|
||||||
|
this->socket = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if(this->socket == SOCKET_ERROR)
|
||||||
|
{
|
||||||
|
//error opening socket
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::Disconnect()
|
void Connection::Disconnect()
|
||||||
|
|
|
@ -19,15 +19,14 @@ namespace Oyster
|
||||||
Connection(int socket) { this->socket = socket; };
|
Connection(int socket) { this->socket = socket; };
|
||||||
virtual ~Connection();
|
virtual ~Connection();
|
||||||
|
|
||||||
virtual bool Connect( unsigned short port , const char serverName[] );
|
|
||||||
virtual bool InitiateServer( unsigned short port );
|
virtual bool InitiateServer( unsigned short port );
|
||||||
//virutal bool initiateClient();
|
virtual bool InitiateClient();
|
||||||
|
|
||||||
virtual void Disconnect();
|
|
||||||
|
|
||||||
virtual bool Send(const unsigned char message[]);
|
virtual bool Send(const unsigned char message[]);
|
||||||
virtual int Recieve(unsigned char message[]);
|
virtual int Recieve(unsigned char message[]);
|
||||||
|
|
||||||
|
virtual void Disconnect();
|
||||||
|
virtual bool Connect( unsigned short port , const char serverName[] );
|
||||||
virtual int Listen();
|
virtual int Listen();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -17,7 +17,7 @@ namespace Oyster
|
||||||
virtual bool Send( const unsigned char message[] ) = 0;
|
virtual bool Send( const unsigned char message[] ) = 0;
|
||||||
virtual int Recieve(unsigned char message[]) = 0;
|
virtual int Recieve(unsigned char message[]) = 0;
|
||||||
virtual bool InitiateServer( unsigned short port ) { return false; };
|
virtual bool InitiateServer( unsigned short port ) { return false; };
|
||||||
virtual bool initiateClient( unsigned short port ) { return false; };
|
virtual bool InitiateClient() { return false; };
|
||||||
virtual int Listen() { return -1; };
|
virtual int Listen() { return -1; };
|
||||||
virtual bool Connect( unsigned short port, const char serverName[] ) { return false; };
|
virtual bool Connect( unsigned short port, const char serverName[] ) { return false; };
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,6 +15,7 @@ Client::~Client()
|
||||||
|
|
||||||
bool Client::Connect(unsigned int port, char filename[])
|
bool Client::Connect(unsigned int port, char filename[])
|
||||||
{
|
{
|
||||||
|
connection->InitiateClient();
|
||||||
connection->Connect(port, filename);
|
connection->Connect(port, filename);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in New Issue