unsigned char instead of char and chat program with protocols
This commit is contained in:
parent
dcf456ce6d
commit
4142688f6c
|
@ -26,8 +26,8 @@ bool Connection::Connect(unsigned short port , const char serverName[])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct hostent *hostEntry;
|
struct hostent *hostEnt;
|
||||||
if((hostEntry = gethostbyname(serverName)) == NULL)
|
if((hostEnt = gethostbyname(serverName)) == NULL)
|
||||||
{
|
{
|
||||||
//couldn't find host
|
//couldn't find host
|
||||||
return false;
|
return false;
|
||||||
|
@ -35,7 +35,7 @@ bool Connection::Connect(unsigned short port , const char serverName[])
|
||||||
struct sockaddr_in server;
|
struct sockaddr_in server;
|
||||||
server.sin_family = AF_INET;
|
server.sin_family = AF_INET;
|
||||||
server.sin_port = htons(port);
|
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)
|
while(1)
|
||||||
{
|
{
|
||||||
|
@ -77,7 +77,7 @@ bool Connection::InitiateServer(unsigned short port)
|
||||||
return false;
|
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)
|
if(listen(mySocket, 5) == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
//"Listen failed!
|
//"Listen failed!
|
||||||
|
@ -94,12 +94,13 @@ 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);
|
||||||
|
messageSize = 18;
|
||||||
if((nBytes = send(mySocket, message , messageSize, 0)) == SOCKET_ERROR)
|
nBytes = send(mySocket, (char*)message , messageSize, 0);
|
||||||
|
if(nBytes == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
//Send failed!
|
//Send failed!
|
||||||
return false;
|
return false;
|
||||||
|
@ -108,10 +109,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
|
||||||
|
|
|
@ -21,9 +21,12 @@ 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 int Recieve(char message[]);
|
virtual bool Send(const unsigned char message[]);
|
||||||
|
virtual int Recieve(unsigned char message[]);
|
||||||
|
|
||||||
virtual int Listen();
|
virtual int Listen();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -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; };
|
||||||
|
|
|
@ -32,6 +32,14 @@ namespace Oyster
|
||||||
std::string textMessage;
|
std::string textMessage;
|
||||||
ProtocolTest() { this->packageType = package_type_test; }
|
ProtocolTest() { this->packageType = package_type_test; }
|
||||||
};
|
};
|
||||||
|
/*struct Prutt
|
||||||
|
{
|
||||||
|
PackageType t;
|
||||||
|
union PRUTT
|
||||||
|
{
|
||||||
|
ProtocolTest *ptest,
|
||||||
|
};
|
||||||
|
};*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
#include <WinSock2.h>
|
#include <WinSock2.h>
|
||||||
|
#include "..\NetworkDependencies\Translator.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
using namespace Oyster::Network::Protocols;;
|
||||||
using namespace Oyster::Network::Client;
|
using namespace Oyster::Network::Client;
|
||||||
#pragma comment(lib, "ws2_32.lib")
|
#pragma comment(lib, "ws2_32.lib")
|
||||||
|
|
||||||
|
|
||||||
void ShutdownSockets();
|
void ShutdownSockets();
|
||||||
bool InitSockets();
|
bool InitSockets();
|
||||||
void chat(Client client);
|
void chat(Client client);
|
||||||
|
@ -23,7 +27,6 @@ int main()
|
||||||
//Connect to server
|
//Connect to server
|
||||||
client.Connect(9876, "10.0.0.3");
|
client.Connect(9876, "10.0.0.3");
|
||||||
|
|
||||||
|
|
||||||
chat(client);
|
chat(client);
|
||||||
|
|
||||||
//Recieve message
|
//Recieve message
|
||||||
|
@ -51,8 +54,13 @@ void ShutdownSockets()
|
||||||
|
|
||||||
void chat(Client client)
|
void chat(Client client)
|
||||||
{
|
{
|
||||||
char msgRecv[255] = "\0";
|
Oyster::Network::Translator *t = new Oyster::Network::Translator();
|
||||||
char msgSend[255] = "\0";
|
|
||||||
|
unsigned char msgRecv[255] = "\0";
|
||||||
|
string msgSend = "";
|
||||||
|
|
||||||
|
ProtocolHeader header;
|
||||||
|
ProtocolTest test;
|
||||||
|
|
||||||
bool chatDone = false;
|
bool chatDone = false;
|
||||||
|
|
||||||
|
@ -60,22 +68,44 @@ void chat(Client client)
|
||||||
{
|
{
|
||||||
client.Recv(msgRecv);
|
client.Recv(msgRecv);
|
||||||
|
|
||||||
cout<< "Client 2: " << msgRecv << endl;
|
header = t->Translate(msgRecv);
|
||||||
|
|
||||||
cin.getline(msgSend , 255 , '\n');
|
switch(header.packageType)
|
||||||
|
|
||||||
if(strlen(msgSend) < 1)
|
|
||||||
{
|
{
|
||||||
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(msgSend, "ERROR");
|
//strcpy_s((char)msgSend , " ");
|
||||||
}
|
}
|
||||||
client.Send(msgSend);
|
|
||||||
|
if( msgSend != "exit")
|
||||||
|
{
|
||||||
|
if(msgSend.length() < 1)
|
||||||
|
{
|
||||||
|
//memcpy(msgSend, "ERROR" , 5);
|
||||||
|
msgSend = "ERROR!";
|
||||||
|
//strcpy_s(msgSend, "ERROR");
|
||||||
|
}
|
||||||
|
|
||||||
|
test.packageType = package_type_test;
|
||||||
|
test.size = msgSend.length();
|
||||||
|
test.textMessage = msgSend;
|
||||||
|
|
||||||
|
unsigned char *message = t->Translate(test);
|
||||||
|
|
||||||
|
client.Send(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
|
|
@ -59,7 +59,7 @@ 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");
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue