chat test with server

created simple chat program for the client to be able to chat between
client through a server
This commit is contained in:
Sam Mario Svensson 2013-11-19 14:18:34 +01:00
parent ac75e178ce
commit 1df11becf7
2 changed files with 38 additions and 3 deletions

View File

@ -126,6 +126,8 @@ int Connection::Recieve(char message[])
return -1; return -1;
} }
message[nBytes] = NULL;
return 1; return 1;
} }

View File

@ -6,6 +6,7 @@ using namespace std;
void ShutdownSockets(); void ShutdownSockets();
bool InitSockets(); bool InitSockets();
void chat(Client client);
int main() int main()
{ {
@ -19,13 +20,16 @@ int main()
Client client; Client client;
//Connect to server //Connect to server
client.Connect(9876, "127.0.0.1"); client.Connect(9876, "10.0.0.3");
chat(client);
//Recieve message //Recieve message
client.Recv(msgRecv); //client.Recv(msgRecv);
//print message //print message
cout << msgRecv << endl; //cout << msgRecv << endl;
ShutdownSockets(); ShutdownSockets();
@ -43,3 +47,32 @@ void ShutdownSockets()
{ {
WSACleanup(); WSACleanup();
} }
void chat(Client client)
{
char msgRecv[255] = "\0";
char msgSend[255] = "\0";
bool chatDone = false;
while(!chatDone)
{
client.Recv(msgRecv);
cout<< "Server: " << msgRecv << endl;
cin.getline(msgSend , 255 , '\n');
if(msgSend != "exit")
{
client.Send(msgSend);
}
else
{
chatDone = true;
}
}
}