Fixed error

Fixed translation
This commit is contained in:
Pontus Fransson 2013-11-22 08:56:00 +01:00
parent dcf456ce6d
commit 954a1ac669
12 changed files with 56 additions and 62 deletions

View File

@ -94,12 +94,11 @@ void Connection::Disconnect()
closesocket(mySocket); closesocket(mySocket);
} }
bool Connection::Send(const char message[]) bool Connection::Send(const unsigned char message[])
{ {
int nBytes; int nBytes;
unsigned long messageSize = strlen(message); unsigned long messageSize = strlen((char*)message);
if((nBytes = send(mySocket, (char*)message , messageSize, 0)) == SOCKET_ERROR)
if((nBytes = send(mySocket, message , messageSize, 0)) == SOCKET_ERROR)
{ {
//Send failed! //Send failed!
return false; return false;
@ -108,10 +107,10 @@ bool Connection::Send(const char message[])
return true; return true;
} }
int Connection::Recieve(char message[]) int Connection::Recieve(unsigned char message[])
{ {
int nBytes; int nBytes;
nBytes = recv(mySocket, message , 255, 0); nBytes = recv(mySocket, (char*)message , 255, 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
//Recv failed //Recv failed

View File

@ -22,8 +22,8 @@ namespace Oyster
virtual bool Connect( unsigned short port , const char serverName[] ); virtual bool Connect( unsigned short port , const char serverName[] );
virtual bool InitiateServer( unsigned short port ); virtual bool InitiateServer( unsigned short port );
virtual void Disconnect(); virtual void Disconnect();
virtual bool Send(const char message[]); virtual bool Send(const unsigned char message[]);
virtual int Recieve(char message[]); virtual int Recieve(char unsigned message[]);
virtual int Listen(); virtual int Listen();
private: private:

View File

@ -14,8 +14,8 @@ namespace Oyster
public: public:
virtual void Disconnect() = 0; virtual void Disconnect() = 0;
virtual bool Send( const char message[] ) = 0; virtual bool Send( const unsigned char message[] ) = 0;
virtual int Recieve(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 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; };

View File

@ -10,7 +10,7 @@ namespace Oyster
public: public:
virtual unsigned char* Translate (Protocols::ProtocolHeader &header ) = 0; virtual unsigned char* Translate (Protocols::ProtocolHeader &header ) = 0;
virtual Protocols::ProtocolHeader& Translate ( unsigned char message[] ) = 0; virtual Protocols::ProtocolHeader* Translate ( unsigned char message[] ) = 0;
}; };
} }

View File

@ -25,9 +25,9 @@ unsigned char* Translator::Translate( ProtocolHeader &header )
return message->GetMsg(); 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(); MessageHeader *message = new MessageHeader();
message->Translate(msg, *header); message->Translate(msg, *header);
@ -46,7 +46,5 @@ ProtocolHeader& Translator::Translate(unsigned char msg[] )
} }
message->Translate(msg, *header); message->Translate(msg, *header);
return *header; return header;
} }

View File

@ -16,7 +16,7 @@ namespace Oyster
~Translator(){}; ~Translator(){};
unsigned char* Translate (Protocols::ProtocolHeader &header ); unsigned char* Translate (Protocols::ProtocolHeader &header );
Protocols::ProtocolHeader& Translate (unsigned char msg[] ); Protocols::ProtocolHeader* Translate (unsigned char msg[] );
}; };
} }
} }

View File

@ -19,12 +19,12 @@ bool Client::Connect(unsigned int port, char filename[])
return true; return true;
} }
void Client::Send(char msg[]) void Client::Send(unsigned char msg[])
{ {
connection->Send(msg); connection->Send(msg);
} }
void Client::Recv(char msg[]) void Client::Recv(unsigned char msg[])
{ {
connection->Recieve(msg); connection->Recieve(msg);
} }

View File

@ -21,8 +21,8 @@ namespace Oyster
bool Connect(unsigned int port, char filename[]); bool Connect(unsigned int port, char filename[]);
void Send(char msg[]); void Send(unsigned char msg[]);
void Recv(char msg[]); void Recv(unsigned char msg[]);
private: private:
::Oyster::Network::Connection* connection; ::Oyster::Network::Connection* connection;

View File

@ -9,6 +9,10 @@ void ShutdownSockets();
bool InitSockets(); bool InitSockets();
void chat(Client client); void chat(Client client);
#include "../NetworkDependencies/Protocols.h"
#include "../NetworkDependencies/Translator.h"
using namespace Oyster::Network::Protocols;
int main() int main()
{ {
char msgRecv[255] = "\0"; char msgRecv[255] = "\0";
@ -21,10 +25,13 @@ int main()
Client client; Client client;
//Connect to server //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 //Recieve message
//client.Recv(msgRecv); //client.Recv(msgRecv);
@ -51,8 +58,8 @@ void ShutdownSockets()
void chat(Client client) void chat(Client client)
{ {
char msgRecv[255] = "\0"; unsigned char msgRecv[255] = "\0";
char msgSend[255] = "\0"; unsigned char msgSend[255] = "\0";
bool chatDone = false; bool chatDone = false;
@ -62,18 +69,18 @@ void chat(Client client)
cout<< "Client 2: " << msgRecv << endl; 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); client.Send(msgSend);
} }

View File

@ -12,12 +12,12 @@ Client::~Client()
delete connection; delete connection;
} }
void Client::Send(char buffer[]) void Client::Send(unsigned char buffer[])
{ {
connection->Send(buffer); connection->Send(buffer);
} }
void Client::Recv(char buffer[]) void Client::Recv(unsigned char buffer[])
{ {
connection->Recieve(buffer); connection->Recieve(buffer);
} }

View File

@ -20,8 +20,8 @@ namespace Oyster
Client(unsigned int socket); Client(unsigned int socket);
~Client(); ~Client();
void Send(char buffer[]); void Send(unsigned char buffer[]);
void Recv(char buffer[]); void Recv(unsigned char buffer[]);
private: private:
::Oyster::Network::Connection* connection; ::Oyster::Network::Connection* connection;

View File

@ -16,28 +16,9 @@ using namespace ::Protocols;
int main() int main()
{ {
unsigned char* recvBuffer; unsigned char* recvBuffer = new unsigned char[255];
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<ProtocolTest*>(&asd)->textMessage << endl;
cout << "Server" << endl; cout << "Server" << endl;
Translator t;
if(!InitSockets()) if(!InitSockets())
{ {
@ -59,21 +40,30 @@ int main()
Client client2(clientSocket); Client client2(clientSocket);
cout << "Second client connected." << endl; cout << "Second client connected." << endl;
client1.Send("Hej"); client1.Send((unsigned char*)"Hej");
ProtocolHeader* header = NULL;
while(1) while(1)
{ {
/*client1.Recv(recvBuffer); client1.Recv(recvBuffer);
cout << "Client1: " << recvBuffer << endl;
header = t.Translate(recvBuffer);
cout << header->clientID << ' ' << header->packageType << ' ' << header->size << endl;
cout << "Client1: " << ((ProtocolTest*)header)->textMessage << endl;
client2.Send(recvBuffer); client2.Send(recvBuffer);
client2.Recv(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(); ShutdownSockets();
delete[] recvBuffer;
system("pause"); system("pause");
return 0; return 0;
} }