2013-11-19 12:38:13 +01:00
|
|
|
#include "Connection.h"
|
|
|
|
|
2013-11-19 13:42:50 +01:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
2013-11-25 11:39:38 +01:00
|
|
|
using namespace Oyster::Network;
|
2013-11-19 12:38:13 +01:00
|
|
|
|
|
|
|
Connection::~Connection()
|
|
|
|
{
|
2013-11-25 11:39:38 +01:00
|
|
|
closesocket( this->socket );
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Connection::Connect(unsigned short port , const char serverName[])
|
|
|
|
{
|
2013-11-25 11:39:38 +01:00
|
|
|
this->socket = ::socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(this->socket == SOCKET_ERROR)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
//error opening socket
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-22 09:17:07 +01:00
|
|
|
struct hostent *hostEnt;
|
|
|
|
if((hostEnt = gethostbyname(serverName)) == NULL)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
//couldn't find host
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-25 11:39:38 +01:00
|
|
|
|
2013-11-19 12:38:13 +01:00
|
|
|
struct sockaddr_in server;
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = htons(port);
|
2013-11-22 09:17:07 +01:00
|
|
|
server.sin_addr.s_addr = *(unsigned long*) hostEnt->h_addr;
|
2013-11-19 12:38:13 +01:00
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
2013-11-25 11:39:38 +01:00
|
|
|
if(connect(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
//Error connecting to server
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Sleep(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
//connection succesfull!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Connection::InitiateServer(unsigned short port)
|
|
|
|
{
|
2013-11-25 11:39:38 +01:00
|
|
|
this->socket = ::socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(this->socket == SOCKET_ERROR)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
//Error opening socket!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in server;
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = htons(port);
|
|
|
|
server.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
2013-11-25 11:39:38 +01:00
|
|
|
if(bind(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
//Bind failed!;
|
2013-11-25 11:39:38 +01:00
|
|
|
closesocket(this->socket);
|
2013-11-19 12:38:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-22 09:17:07 +01:00
|
|
|
//not our Listen function! its trying to keep our socket open for connections
|
2013-11-25 11:39:38 +01:00
|
|
|
if(listen(this->socket, 5) == SOCKET_ERROR)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
//"Listen failed!
|
2013-11-25 11:39:38 +01:00
|
|
|
closesocket(this->socket);
|
2013-11-19 12:38:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Server started!
|
2013-11-25 11:39:38 +01:00
|
|
|
return this->socket;
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::Disconnect()
|
|
|
|
{
|
2013-11-25 11:39:38 +01:00
|
|
|
closesocket(this->socket);
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-22 08:56:00 +01:00
|
|
|
bool Connection::Send(const unsigned char message[])
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
int nBytes;
|
2013-11-22 08:56:00 +01:00
|
|
|
unsigned long messageSize = strlen((char*)message);
|
2013-11-22 09:24:44 +01:00
|
|
|
|
2013-11-22 09:43:24 +01:00
|
|
|
messageSize = 255;
|
2013-11-25 11:39:38 +01:00
|
|
|
nBytes = send(this->socket, (char*)message , messageSize, 0);
|
2013-11-22 09:17:07 +01:00
|
|
|
if(nBytes == SOCKET_ERROR)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
//Send failed!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-22 08:56:00 +01:00
|
|
|
int Connection::Recieve(unsigned char message[])
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
int nBytes;
|
2013-11-25 11:39:38 +01:00
|
|
|
nBytes = recv(this->socket, (char*)message , 255, 0);
|
2013-11-19 12:38:13 +01:00
|
|
|
if(nBytes == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
//Recv failed
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-19 14:21:25 +01:00
|
|
|
message[nBytes] = '\0';
|
2013-11-19 14:18:34 +01:00
|
|
|
|
2013-11-19 12:38:13 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Connection::Listen()
|
|
|
|
{
|
|
|
|
int clientSocket;
|
2013-11-25 11:39:38 +01:00
|
|
|
if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
//failed
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientSocket;
|
|
|
|
}
|