ErrorMessages everywhere!
This commit is contained in:
parent
45c145439a
commit
f6ade9e4fd
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <winsock2.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace Oyster::Network;
|
||||
|
||||
|
@ -10,13 +11,12 @@ Connection::~Connection()
|
|||
closesocket( this->socket );
|
||||
}
|
||||
|
||||
bool Connection::Connect(unsigned short port , const char serverName[])
|
||||
int Connection::Connect(unsigned short port , const char serverName[])
|
||||
{
|
||||
struct hostent *hostEnt;
|
||||
if((hostEnt = gethostbyname(serverName)) == NULL)
|
||||
{
|
||||
//couldn't find host
|
||||
return false;
|
||||
return WSAGetLastError();
|
||||
}
|
||||
|
||||
struct sockaddr_in server;
|
||||
|
@ -26,20 +26,20 @@ bool Connection::Connect(unsigned short port , const char serverName[])
|
|||
|
||||
if(connect(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
||||
{
|
||||
//Error connecting to server
|
||||
return false;
|
||||
return WSAGetLastError();
|
||||
}
|
||||
|
||||
//connection succesfull!
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Connection::InitiateServer(unsigned short port)
|
||||
int Connection::InitiateServer(unsigned short port)
|
||||
{
|
||||
if(!initiateSocket())
|
||||
int errorCode = 0;
|
||||
|
||||
if((errorCode = initiateSocket()) != 0)
|
||||
{
|
||||
//Error opening socket!
|
||||
return false;
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
struct sockaddr_in server;
|
||||
|
@ -49,39 +49,38 @@ bool Connection::InitiateServer(unsigned short port)
|
|||
|
||||
if(bind(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
||||
{
|
||||
//Bind failed!;
|
||||
errorCode = WSAGetLastError();
|
||||
closesocket(this->socket);
|
||||
return false;
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
//not our Listen function! its trying to keep our socket open for connections
|
||||
if(listen(this->socket, 5) == SOCKET_ERROR)
|
||||
{
|
||||
//"Listen failed!
|
||||
errorCode = WSAGetLastError();
|
||||
closesocket(this->socket);
|
||||
return false;
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
//Server started!
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Connection::InitiateClient()
|
||||
int Connection::InitiateClient()
|
||||
{
|
||||
if(!initiateSocket())
|
||||
{
|
||||
return false;
|
||||
int errorCode;
|
||||
return initiateSocket();
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Connection::Disconnect()
|
||||
int Connection::Disconnect()
|
||||
{
|
||||
closesocket(this->socket);
|
||||
|
||||
return WSAGetLastError();
|
||||
}
|
||||
|
||||
bool Connection::Send(const unsigned char message[])
|
||||
int Connection::Send(const unsigned char message[])
|
||||
{
|
||||
int nBytes;
|
||||
unsigned long messageSize = strlen((char*)message);
|
||||
|
@ -90,13 +89,10 @@ bool Connection::Send(const unsigned char message[])
|
|||
nBytes = send(this->socket, (char*)message , messageSize, 0);
|
||||
if(nBytes == SOCKET_ERROR)
|
||||
{
|
||||
//Send failed!
|
||||
return false;
|
||||
return WSAGetLastError();
|
||||
}
|
||||
|
||||
std::cout << "Size of the sent data: " << nBytes << " bytes" << std::endl;
|
||||
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Connection::Recieve(unsigned char message[])
|
||||
|
@ -106,27 +102,20 @@ int Connection::Recieve(unsigned char message[])
|
|||
nBytes = recv(this->socket, (char*)message, 1600, 0);
|
||||
if(nBytes == SOCKET_ERROR)
|
||||
{
|
||||
//Recv failed
|
||||
return -1;
|
||||
return WSAGetLastError();
|
||||
}
|
||||
|
||||
message[nBytes] = '\0';
|
||||
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Connection::Listen()
|
||||
{
|
||||
int optlen = sizeof(int);
|
||||
int optval;
|
||||
int Return = getsockopt(this->socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
|
||||
std::cout << optval << std::endl;
|
||||
|
||||
int clientSocket;
|
||||
if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
|
||||
{
|
||||
//failed
|
||||
return -1;
|
||||
return WSAGetLastError();
|
||||
}
|
||||
|
||||
return clientSocket;
|
||||
|
@ -135,14 +124,13 @@ int Connection::Listen()
|
|||
///////////////////////////////////////
|
||||
//Private functions
|
||||
///////////////////////////////////////
|
||||
bool Connection::initiateSocket()
|
||||
int Connection::initiateSocket()
|
||||
{
|
||||
this->socket = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(this->socket == SOCKET_ERROR)
|
||||
{
|
||||
//error opening socket
|
||||
return false;
|
||||
return WSAGetLastError();
|
||||
}
|
||||
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -19,18 +19,18 @@ namespace Oyster
|
|||
Connection(int socket) { this->socket = socket; };
|
||||
virtual ~Connection();
|
||||
|
||||
virtual bool InitiateServer( unsigned short port );
|
||||
virtual bool InitiateClient();
|
||||
virtual int InitiateServer( unsigned short port );
|
||||
virtual int InitiateClient();
|
||||
|
||||
virtual bool Send(const unsigned char message[]);
|
||||
virtual int Send(const unsigned char message[]);
|
||||
virtual int Recieve(unsigned char message[]);
|
||||
|
||||
virtual void Disconnect();
|
||||
virtual bool Connect( unsigned short port , const char serverName[] );
|
||||
virtual int Disconnect();
|
||||
virtual int Connect( unsigned short port , const char serverName[] );
|
||||
virtual int Listen();
|
||||
|
||||
private:
|
||||
bool initiateSocket();
|
||||
int initiateSocket();
|
||||
|
||||
int socket;
|
||||
|
||||
|
|
|
@ -13,13 +13,13 @@ namespace Oyster
|
|||
{
|
||||
|
||||
public:
|
||||
virtual void Disconnect() = 0;
|
||||
virtual bool Send( const unsigned char message[] ) = 0;
|
||||
virtual int Disconnect() = 0;
|
||||
virtual int Send( const unsigned char message[] ) = 0;
|
||||
virtual int Recieve(unsigned char message[]) = 0;
|
||||
virtual bool InitiateServer( unsigned short port ) { return false; };
|
||||
virtual bool InitiateClient() { return false; };
|
||||
virtual int InitiateServer( unsigned short port ) { return false; };
|
||||
virtual int InitiateClient() { return false; };
|
||||
virtual int Listen() { return -1; };
|
||||
virtual bool Connect( unsigned short port, const char serverName[] ) { return false; };
|
||||
virtual int Connect( unsigned short port, const char serverName[] ) { return false; };
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,37 @@
|
|||
#include "WinsockFunctions.h"
|
||||
#include <WinSock2.h>
|
||||
|
||||
bool InitSockets()
|
||||
bool InitWinSock()
|
||||
{
|
||||
WSADATA wsaData;
|
||||
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
|
||||
}
|
||||
|
||||
void ShutdownSockets()
|
||||
void ShutdownWinSock()
|
||||
{
|
||||
WSACleanup();
|
||||
}
|
||||
|
||||
std::wstring GetErrorMessage(int errorCode)
|
||||
{
|
||||
LPWSTR lpMessage;
|
||||
std::wstring retVal(L"Succesful");
|
||||
|
||||
DWORD bufLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS ,
|
||||
NULL,
|
||||
errorCode ,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ,
|
||||
(LPWSTR)&lpMessage,
|
||||
0 ,
|
||||
NULL );
|
||||
|
||||
if(bufLen)
|
||||
{
|
||||
retVal = lpMessage;
|
||||
|
||||
LocalFree(lpMessage);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
}
|
|
@ -4,8 +4,12 @@
|
|||
/////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013 //
|
||||
/////////////////////////////////////
|
||||
#include <string>
|
||||
|
||||
void ShutdownSockets();
|
||||
bool InitSockets();
|
||||
|
||||
void ShutdownWinSock();
|
||||
bool InitWinSock();
|
||||
|
||||
std::wstring GetErrorMessage(int errorCode);
|
||||
|
||||
#endif
|
|
@ -13,20 +13,29 @@ Client::~Client()
|
|||
connection = 0;
|
||||
}
|
||||
|
||||
bool Client::Connect(unsigned int port, char filename[])
|
||||
int Client::Connect(unsigned int port, char filename[])
|
||||
{
|
||||
connection->InitiateClient();
|
||||
connection->Connect(port, filename);
|
||||
int errorCode;
|
||||
|
||||
return true;
|
||||
if((errorCode = connection->InitiateClient()) != 0)
|
||||
{
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
void Client::Send(unsigned char msg[])
|
||||
if((errorCode = connection->Connect(port, filename)) != 0)
|
||||
{
|
||||
connection->Send(msg);
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
void Client::Recv(unsigned char msg[])
|
||||
{
|
||||
connection->Recieve(msg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Client::Send(unsigned char msg[])
|
||||
{
|
||||
return connection->Send(msg);
|
||||
}
|
||||
|
||||
int Client::Recv(unsigned char msg[])
|
||||
{
|
||||
return connection->Recieve(msg);
|
||||
}
|
|
@ -19,10 +19,10 @@ namespace Oyster
|
|||
Client();
|
||||
~Client();
|
||||
|
||||
bool Connect(unsigned int port, char filename[]);
|
||||
int Connect(unsigned int port, char filename[]);
|
||||
|
||||
void Send(unsigned char msg[]);
|
||||
void Recv(unsigned char msg[]);
|
||||
int Send(unsigned char msg[]);
|
||||
int Recv(unsigned char msg[]);
|
||||
|
||||
private:
|
||||
::Oyster::Network::Connection* connection;
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
#include <WinSock2.h>
|
||||
#include <vld.h>
|
||||
#include "../NetworkDependencies/WinsockFunctions.h"
|
||||
#include "..\NetworkDependencies\Translator.h"
|
||||
#include "..\NetworkDependencies\Protocols.h"
|
||||
#include "../NetworkDependencies/Translator.h"
|
||||
#include "../NetworkDependencies/Protocols.h"
|
||||
#include "Client.h"
|
||||
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
@ -16,9 +16,11 @@ void chat(Client &client);
|
|||
|
||||
int main()
|
||||
{
|
||||
int errorCode;
|
||||
|
||||
char msgRecv[255] = "\0";
|
||||
|
||||
InitSockets();
|
||||
InitWinSock();
|
||||
|
||||
cout << "Client" << endl;
|
||||
|
||||
|
@ -26,11 +28,17 @@ int main()
|
|||
Client client;
|
||||
|
||||
//Connect to server
|
||||
client.Connect(9876, "localhost");
|
||||
errorCode = client.Connect(9876, "localhost");
|
||||
|
||||
if(errorCode != 0)
|
||||
{
|
||||
wstring errorTest = GetErrorMessage(errorCode);
|
||||
wcout << "errorMessage: " << errorTest << endl;
|
||||
}
|
||||
|
||||
chat(client);
|
||||
|
||||
ShutdownSockets();
|
||||
ShutdownWinSock();
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
|
|
|
@ -12,12 +12,12 @@ Client::~Client()
|
|||
delete connection;
|
||||
}
|
||||
|
||||
void Client::Send(unsigned char buffer[])
|
||||
int Client::Send(unsigned char buffer[])
|
||||
{
|
||||
connection->Send(buffer);
|
||||
return connection->Send(buffer);
|
||||
}
|
||||
|
||||
void Client::Recv(unsigned char buffer[])
|
||||
int Client::Recv(unsigned char buffer[])
|
||||
{
|
||||
connection->Recieve(buffer);
|
||||
return connection->Recieve(buffer);
|
||||
}
|
|
@ -19,8 +19,8 @@ namespace Oyster
|
|||
Client(unsigned int socket);
|
||||
~Client();
|
||||
|
||||
void Send(unsigned char buffer[]);
|
||||
void Recv(unsigned char buffer[]);
|
||||
int Send(unsigned char buffer[]);
|
||||
int Recv(unsigned char buffer[]);
|
||||
|
||||
private:
|
||||
::Oyster::Network::Connection* connection;
|
||||
|
|
|
@ -18,10 +18,11 @@ int main()
|
|||
unsigned char* recvBuffer = new unsigned char[1601];
|
||||
cout << "Server" << endl;
|
||||
Translator t;
|
||||
int errorCode;
|
||||
|
||||
if(!InitSockets())
|
||||
if(!InitWinSock())
|
||||
{
|
||||
cout << "Sockets failed to initialize" << endl;
|
||||
cout << "errorMessage: unable to start winsock" << endl;
|
||||
}
|
||||
|
||||
//Create socket
|
||||
|
@ -84,7 +85,7 @@ int main()
|
|||
client1.Send(recvBuffer);
|
||||
}
|
||||
|
||||
ShutdownSockets();
|
||||
ShutdownWinSock();
|
||||
delete[] recvBuffer;
|
||||
delete set;
|
||||
|
||||
|
|
Loading…
Reference in New Issue