unsigned char instead of char and chat program with protocols

This commit is contained in:
Sam Mario Svensson 2013-11-22 08:54:26 +01:00
parent dcf456ce6d
commit 4142688f6c
10 changed files with 79 additions and 37 deletions

View File

@ -26,8 +26,8 @@ bool Connection::Connect(unsigned short port , const char serverName[])
}
struct hostent *hostEntry;
if((hostEntry = gethostbyname(serverName)) == NULL)
struct hostent *hostEnt;
if((hostEnt = gethostbyname(serverName)) == NULL)
{
//couldn't find host
return false;
@ -35,7 +35,7 @@ bool Connection::Connect(unsigned short port , const char serverName[])
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = *(unsigned long*) hostEntry->h_addr;
server.sin_addr.s_addr = *(unsigned long*) hostEnt->h_addr;
while(1)
{
@ -77,7 +77,7 @@ bool Connection::InitiateServer(unsigned short port)
return false;
}
//not our Listen function!
//not our Listen function! its trying to keep our socket open for connections
if(listen(mySocket, 5) == SOCKET_ERROR)
{
//"Listen failed!
@ -94,12 +94,13 @@ 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);
messageSize = 18;
nBytes = send(mySocket, (char*)message , messageSize, 0);
if(nBytes == SOCKET_ERROR)
{
//Send failed!
return false;
@ -108,10 +109,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

View File

@ -20,10 +20,13 @@ namespace Oyster
~Connection();
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 bool Send(const char message[]);
virtual int Recieve(char message[]);
virtual bool Send(const unsigned char message[]);
virtual int Recieve(unsigned char message[]);
virtual int Listen();
private:

View File

@ -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; };

View File

@ -32,6 +32,14 @@ namespace Oyster
std::string textMessage;
ProtocolTest() { this->packageType = package_type_test; }
};
/*struct Prutt
{
PackageType t;
union PRUTT
{
ProtocolTest *ptest,
};
};*/
}
}
}

View File

@ -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);
}

View File

@ -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;

View File

@ -1,10 +1,14 @@
#include <iostream>
#include "Client.h"
#include <WinSock2.h>
#include "..\NetworkDependencies\Translator.h"
using namespace std;
using namespace Oyster::Network::Protocols;;
using namespace Oyster::Network::Client;
#pragma comment(lib, "ws2_32.lib")
void ShutdownSockets();
bool InitSockets();
void chat(Client client);
@ -23,7 +27,6 @@ int main()
//Connect to server
client.Connect(9876, "10.0.0.3");
chat(client);
//Recieve message
@ -51,31 +54,58 @@ void ShutdownSockets()
void chat(Client client)
{
char msgRecv[255] = "\0";
char msgSend[255] = "\0";
Oyster::Network::Translator *t = new Oyster::Network::Translator();
unsigned char msgRecv[255] = "\0";
string msgSend = "";
ProtocolHeader header;
ProtocolTest test;
bool chatDone = false;
while(!chatDone)
{
client.Recv(msgRecv);
header = t->Translate(msgRecv);
cout<< "Client 2: " << msgRecv << endl;
cin.getline(msgSend , 255 , '\n');
if(strlen(msgSend) < 1)
switch(header.packageType)
{
strcpy_s(msgSend , " ");
case package_type_header:
break;
case package_type_test:
cout <<"Client 2: " <<((ProtocolTest*)&header)->textMessage <<endl;
break;
}
if(msgSend != "exit")
std::getline(std::cin, msgSend);
std::cin.clear();
if(msgSend.length() < 1)
{
if(strlen(msgSend) < 1)
//memcpy(msgSend, " " , 1);
msgSend = " ";
//strcpy_s((char)msgSend , " ");
}
if( msgSend != "exit")
{
if(msgSend.length() < 1)
{
strcpy_s(msgSend, "ERROR");
//memcpy(msgSend, "ERROR" , 5);
msgSend = "ERROR!";
//strcpy_s(msgSend, "ERROR");
}
client.Send(msgSend);
test.packageType = package_type_test;
test.size = msgSend.length();
test.textMessage = msgSend;
unsigned char *message = t->Translate(test);
client.Send(message);
}
else

View File

@ -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);
}

View File

@ -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;

View File

@ -59,7 +59,7 @@ int main()
Client client2(clientSocket);
cout << "Second client connected." << endl;
client1.Send("Hej");
client1.Send((unsigned char*) "Hej");
while(1)
{