diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 8a2081c0..c84d3344 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -94,12 +94,11 @@ void Connection::Disconnect() closesocket(mySocket); } -bool Connection::Send(const char message[]) +bool Connection::Send(const unsigned char message[]) { int nBytes; - unsigned long messageSize = strlen(message); - - if((nBytes = send(mySocket, message , messageSize, 0)) == SOCKET_ERROR) + unsigned long messageSize = strlen((char*)message); + if((nBytes = send(mySocket, (char*)message , messageSize, 0)) == SOCKET_ERROR) { //Send failed! return false; @@ -108,10 +107,10 @@ bool Connection::Send(const char message[]) return true; } -int Connection::Recieve(char message[]) +int Connection::Recieve(unsigned char message[]) { int nBytes; - nBytes = recv(mySocket, message , 255, 0); + nBytes = recv(mySocket, (char*)message , 255, 0); if(nBytes == SOCKET_ERROR) { //Recv failed diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index 0070fed6..5febafa5 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -22,8 +22,8 @@ namespace Oyster virtual bool Connect( unsigned short port , const char serverName[] ); virtual bool InitiateServer( unsigned short port ); virtual void Disconnect(); - virtual bool Send(const char message[]); - virtual int Recieve(char message[]); + virtual bool Send(const unsigned char message[]); + virtual int Recieve(char unsigned message[]); virtual int Listen(); private: diff --git a/Code/Network/NetworkDependencies/IConnection.h b/Code/Network/NetworkDependencies/IConnection.h index 1443b781..91b7c7b4 100644 --- a/Code/Network/NetworkDependencies/IConnection.h +++ b/Code/Network/NetworkDependencies/IConnection.h @@ -14,8 +14,8 @@ namespace Oyster public: virtual void Disconnect() = 0; - virtual bool Send( const char message[] ) = 0; - virtual int Recieve(char message[]) = 0; + virtual bool Send( const unsigned char message[] ) = 0; + virtual int Recieve(unsigned char message[]) = 0; virtual bool InitiateServer( unsigned short port ) { return false; }; virtual int Listen() { return -1; }; virtual bool Connect( unsigned short port, const char serverName[] ) { return false; }; diff --git a/Code/Network/NetworkDependencies/ITranslate.h b/Code/Network/NetworkDependencies/ITranslate.h index 6f24cd72..d17325a0 100644 --- a/Code/Network/NetworkDependencies/ITranslate.h +++ b/Code/Network/NetworkDependencies/ITranslate.h @@ -10,7 +10,7 @@ namespace Oyster public: virtual unsigned char* Translate (Protocols::ProtocolHeader &header ) = 0; - virtual Protocols::ProtocolHeader& Translate ( unsigned char message[] ) = 0; + virtual Protocols::ProtocolHeader* Translate ( unsigned char message[] ) = 0; }; } diff --git a/Code/Network/NetworkDependencies/Translator.cpp b/Code/Network/NetworkDependencies/Translator.cpp index 30e1e273..2915f913 100644 --- a/Code/Network/NetworkDependencies/Translator.cpp +++ b/Code/Network/NetworkDependencies/Translator.cpp @@ -25,9 +25,9 @@ unsigned char* Translator::Translate( ProtocolHeader &header ) return message->GetMsg(); } -ProtocolHeader& Translator::Translate(unsigned char msg[] ) +ProtocolHeader* Translator::Translate(unsigned char msg[] ) { - ProtocolHeader* header = new ProtocolHeader(); + ProtocolHeader *header = new ProtocolHeader(); MessageHeader *message = new MessageHeader(); message->Translate(msg, *header); @@ -46,7 +46,5 @@ ProtocolHeader& Translator::Translate(unsigned char msg[] ) } message->Translate(msg, *header); - return *header; -} - - + return header; +} \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Translator.h b/Code/Network/NetworkDependencies/Translator.h index c9eb7362..14bf4318 100644 --- a/Code/Network/NetworkDependencies/Translator.h +++ b/Code/Network/NetworkDependencies/Translator.h @@ -16,7 +16,7 @@ namespace Oyster ~Translator(){}; unsigned char* Translate (Protocols::ProtocolHeader &header ); - Protocols::ProtocolHeader& Translate (unsigned char msg[] ); + Protocols::ProtocolHeader* Translate (unsigned char msg[] ); }; } } diff --git a/Code/Network/OysterNetworkClient/Client.cpp b/Code/Network/OysterNetworkClient/Client.cpp index a32f0996..3569b7d4 100644 --- a/Code/Network/OysterNetworkClient/Client.cpp +++ b/Code/Network/OysterNetworkClient/Client.cpp @@ -19,12 +19,12 @@ bool Client::Connect(unsigned int port, char filename[]) return true; } -void Client::Send(char msg[]) +void Client::Send(unsigned char msg[]) { connection->Send(msg); } -void Client::Recv(char msg[]) +void Client::Recv(unsigned char msg[]) { connection->Recieve(msg); } \ No newline at end of file diff --git a/Code/Network/OysterNetworkClient/Client.h b/Code/Network/OysterNetworkClient/Client.h index e08d6348..a48e37d9 100644 --- a/Code/Network/OysterNetworkClient/Client.h +++ b/Code/Network/OysterNetworkClient/Client.h @@ -21,8 +21,8 @@ namespace Oyster bool Connect(unsigned int port, char filename[]); - void Send(char msg[]); - void Recv(char msg[]); + void Send(unsigned char msg[]); + void Recv(unsigned char msg[]); private: ::Oyster::Network::Connection* connection; diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index a6002d0a..2e6a4bfe 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -9,6 +9,10 @@ void ShutdownSockets(); bool InitSockets(); void chat(Client client); +#include "../NetworkDependencies/Protocols.h" +#include "../NetworkDependencies/Translator.h" +using namespace Oyster::Network::Protocols; + int main() { char msgRecv[255] = "\0"; @@ -21,10 +25,13 @@ int main() Client client; //Connect to server - client.Connect(9876, "10.0.0.3"); + client.Connect(9876, "127.0.0.1"); + unsigned char* recvBuffer = new unsigned char[255]; - chat(client); + client.Send(recvBuffer); + + //chat(client); //Recieve message //client.Recv(msgRecv); @@ -51,8 +58,8 @@ void ShutdownSockets() void chat(Client client) { - char msgRecv[255] = "\0"; - char msgSend[255] = "\0"; + unsigned char msgRecv[255] = "\0"; + unsigned char msgSend[255] = "\0"; bool chatDone = false; @@ -62,18 +69,18 @@ void chat(Client client) cout<< "Client 2: " << msgRecv << endl; - cin.getline(msgSend , 255 , '\n'); + cin.getline((char*)msgSend , 255 , '\n'); - if(strlen(msgSend) < 1) + if(strlen((char*)msgSend) < 1) { - strcpy_s(msgSend , " "); + memcpy(msgSend, " ", 1); } - if(msgSend != "exit") + if((char*)msgSend != "exit") { - if(strlen(msgSend) < 1) + if(strlen((char*)msgSend) < 1) { - strcpy_s(msgSend, "ERROR"); + memcpy(msgSend, "ERROR", 1); } client.Send(msgSend); } diff --git a/Code/Network/OysterNetworkServer/Client.cpp b/Code/Network/OysterNetworkServer/Client.cpp index f86d37b8..8c2bdd0d 100644 --- a/Code/Network/OysterNetworkServer/Client.cpp +++ b/Code/Network/OysterNetworkServer/Client.cpp @@ -12,12 +12,12 @@ Client::~Client() delete connection; } -void Client::Send(char buffer[]) +void Client::Send(unsigned char buffer[]) { connection->Send(buffer); } -void Client::Recv(char buffer[]) +void Client::Recv(unsigned char buffer[]) { connection->Recieve(buffer); } \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/Client.h b/Code/Network/OysterNetworkServer/Client.h index 2d1de909..05215b31 100644 --- a/Code/Network/OysterNetworkServer/Client.h +++ b/Code/Network/OysterNetworkServer/Client.h @@ -20,8 +20,8 @@ namespace Oyster Client(unsigned int socket); ~Client(); - void Send(char buffer[]); - void Recv(char buffer[]); + void Send(unsigned char buffer[]); + void Recv(unsigned char buffer[]); private: ::Oyster::Network::Connection* connection; diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index a399dc62..a75c4228 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -16,28 +16,9 @@ using namespace ::Protocols; int main() { - unsigned char* recvBuffer; - Translator t; - ProtocolTest header; - header.clientID = 1; - header.packageType = package_type_test; - header.size = 12; - header.textMessage = "Hej"; - - recvBuffer = t.Translate(header); - - ProtocolHeader& asd = t.Translate(recvBuffer); - switch(asd.packageType) - { - case package_type_test: - - break; - } - cout << static_cast(&asd)->textMessage << endl; - - + unsigned char* recvBuffer = new unsigned char[255]; cout << "Server" << endl; - + Translator t; if(!InitSockets()) { @@ -59,21 +40,30 @@ int main() Client client2(clientSocket); cout << "Second client connected." << endl; - client1.Send("Hej"); + client1.Send((unsigned char*)"Hej"); + ProtocolHeader* header = NULL; while(1) { - /*client1.Recv(recvBuffer); - cout << "Client1: " << recvBuffer << endl; + client1.Recv(recvBuffer); + + header = t.Translate(recvBuffer); + cout << header->clientID << ' ' << header->packageType << ' ' << header->size << endl; + cout << "Client1: " << ((ProtocolTest*)header)->textMessage << endl; client2.Send(recvBuffer); client2.Recv(recvBuffer); - cout << "Client2: " << recvBuffer << endl; - client1.Send(recvBuffer);*/ + + header = t.Translate(recvBuffer); + cout << header->clientID << ' ' << header->packageType << ' ' << header->size << endl; + cout << "Client1: " << ((ProtocolTest*)header)->textMessage << endl; + client1.Send(recvBuffer); } ShutdownSockets(); + delete[] recvBuffer; + system("pause"); return 0; }