Iconnection interface
This commit is contained in:
parent
12d72b8b6e
commit
56c32fcbc3
|
@ -0,0 +1,141 @@
|
|||
#include "Connection.h"
|
||||
|
||||
Connection::Connection()
|
||||
{
|
||||
mySocket = NULL;
|
||||
}
|
||||
|
||||
Connection::Connection(int socket)
|
||||
{
|
||||
mySocket = socket;
|
||||
}
|
||||
|
||||
Connection::~Connection()
|
||||
{
|
||||
mySocket = NULL;
|
||||
|
||||
if(socket != NULL)
|
||||
{
|
||||
closesocket( mySocket );
|
||||
mySocket = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool Connection::Connect(unsigned short port , const char serverName[])
|
||||
{
|
||||
mySocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(mySocket == SOCKET_ERROR)
|
||||
{
|
||||
//error opening socket
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
struct hostent *hostEntry;
|
||||
if((hostEntry = gethostbyname(serverName)) == NULL)
|
||||
{
|
||||
//couldn't find host
|
||||
return false;
|
||||
}
|
||||
struct sockaddr_in server;
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(port);
|
||||
server.sin_addr.s_addr = *(unsigned long*) hostEntry->h_addr;
|
||||
|
||||
while(1)
|
||||
{
|
||||
if(connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
||||
{
|
||||
//Error connecting to server
|
||||
return false;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
Sleep(10);
|
||||
}
|
||||
|
||||
//connection succesfull!
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool Connection::InitiateServer(unsigned short port)
|
||||
{
|
||||
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(mySocket == SOCKET_ERROR)
|
||||
{
|
||||
//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;
|
||||
|
||||
if(bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
||||
{
|
||||
//Bind failed!;
|
||||
closesocket(mySocket);
|
||||
return false;
|
||||
}
|
||||
|
||||
//not our Listen function!
|
||||
if(listen(mySocket, 5) == SOCKET_ERROR)
|
||||
{
|
||||
//"Listen failed!
|
||||
closesocket(mySocket);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Server started!
|
||||
return mySocket;
|
||||
}
|
||||
|
||||
void Connection::Disconnect()
|
||||
{
|
||||
closesocket(mySocket);
|
||||
}
|
||||
|
||||
bool Connection::Send(int socket , const char message[])
|
||||
{
|
||||
int nBytes;
|
||||
unsigned long messageSize = strlen(message);
|
||||
|
||||
if((nBytes = send(socket, message , messageSize)) == SOCKET_ERROR)
|
||||
{
|
||||
//Send failed!
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int Recieve(int socket, char message[])
|
||||
{
|
||||
int nBytes;
|
||||
nBytes = recv(socket, message , 255, 0);
|
||||
if(nBytes == SOCKET_ERROR)
|
||||
{
|
||||
//Recv failed
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Connection::Listen()
|
||||
{
|
||||
int clientSocket;
|
||||
if((clientSocket = accept(mySocket, NULL, NULL)) == INVALID_SOCKET)
|
||||
{
|
||||
//failed
|
||||
return -1;
|
||||
}
|
||||
|
||||
return clientSocket;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
//////////////////////////////////
|
||||
// Created by Sam Svensson 2013 //
|
||||
//////////////////////////////////
|
||||
|
||||
#ifndef NETWORK_DEPENDENCIES_CONNECTION_H
|
||||
#define NETWORK_DEPENDENCIES_CONNECTION_H
|
||||
|
||||
#include "IConnection.h"
|
||||
|
||||
class Connection : public IConnection
|
||||
{
|
||||
private:
|
||||
int mySocket;
|
||||
|
||||
public:
|
||||
Connection();
|
||||
Connection(int socket);
|
||||
~Connection();
|
||||
|
||||
virtual bool Connect( unsigned short port , const char serverName[] );
|
||||
virtual bool InitiateServer( unsigned short port );
|
||||
virtual void Disconnect();
|
||||
virtual bool Send(int socket , const char message[]);
|
||||
virtual int Recieve(int socket);
|
||||
virtual int Listen();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,22 @@
|
|||
//////////////////////////////////
|
||||
// Created by Sam Svensson 2013 //
|
||||
//////////////////////////////////
|
||||
|
||||
#ifndef NETWORK_DEPENDENCIES_I_CONNECTION_H
|
||||
#define NETWORK_DEPENDENCIES_I_CONNECTION_H
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <iostream>
|
||||
|
||||
class IConnection
|
||||
{
|
||||
public:
|
||||
virtual bool Connect( unsigned short port, const char serverName[] ) = 0;
|
||||
virtual bool InitiateServer( unsigned short port ) = 0;
|
||||
virtual void Disconnect() = 0;
|
||||
virtual bool Send(int socket , const char message[]); = 0;
|
||||
virtual int Recieve( int socket ) = 0;
|
||||
virtual int Listen() = 0;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -151,8 +151,13 @@
|
|||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Connection.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Connection.h" />
|
||||
<ClInclude Include="IConnection.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
|
|
@ -18,5 +18,16 @@
|
|||
<ClCompile Include="main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Connection.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="IConnection.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Connection.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
Reference in New Issue