ok this is the whole code a little bit longer...........
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include <string>
#include <cstring>
#include <iostream>
#include <conio.h>
#include <vector>
#include <fstream>
using namespace std;
void getIp( char * );
void menu( sf::TcpSocket * );
void personalChat( sf::TcpSocket * );
void chatOnRequest( sf::TcpSocket * );
void chat( sf::TcpSocket * );
bool isAnyRequest( sf::TcpSocket * );
void checkRequest( sf::TcpSocket * );
string id;
int main()
{
char ipAddress[20];
getIp(ipAddress);
sf::IpAddress ip(ipAddress);
sf::TcpSocket *socket = new sf::TcpSocket;
sf::Mutex mutex;
bool done = false;
if( socket->connect(ip, 2000) == 0 )
{
cout<<"Enter online id : ";
std::getline(cin, id);
sf::Packet packet;
packet<<id;
socket->send(packet);
sf::Thread thread( &checkRequest, socket);
thread.launch();
mutex.lock();
sf::Thread thread2( &menu, socket);
thread2.launch();
mutex.unlock();
}
else
{
cout<<"Couldn't connect to the server try again later"<<endl;
}
return 0;
}
void getIp( char * ip )
{
ifstream fout( "ipAddress.TXT");
if( !fout )
cout<<"File is not opened"<<endl;
else
fout.getline( ip, 20, '\n');
}
void menu( sf::TcpSocket * socket )
{
//sf::Thread thread1( &personalChat, socket);
char choice;
do
{
cout<<"P for personal chat."<<endl;
cout<<"Q for quit"<<endl;
cin>>choice;
if( toupper(choice) == 'P' )
{
personalChat( socket );
}
else if( toupper(choice) == 'Q' )
{
cout<<"Thanx for using messanger"<<endl;
exit(0);
}
else
{
cout<<"this choice is not available."<<endl;
cout<<"G for group chat."<<endl;
cout<<"P for personal chat."<<endl;
cout<<"Q for quit"<<endl;
cin>>choice;
}
}while( toupper(choice) != 'G' || toupper(choice) != 'P' );
}
void personalChat( sf::TcpSocket * socket )
{
socket->setBlocking(true);
string message;
string choice = "-10-1*0-01-p";
sf::Packet packet;
packet<<choice;
socket->send(packet);
packet<<id;
socket->send(packet);
packet.clear();
if( socket->receive(packet) == sf::Socket::Done )
{
if( packet.getDataSize() == 0 )
{
cout<<"None of the other clients is online"<<endl;
cout<<"wait...."<<endl;
}
else
{
while( !packet.endOfPacket() )
{
packet>>message;
cout<<message<<endl;
}
packet.clear();
string nameOfclient;
cin.ignore();
cout<<"With which you wanna personal chat ";
std::getline(cin, nameOfclient);
packet<<nameOfclient;
socket->send(packet);
packet>>nameOfclient;
cout<<nameOfclient.c_str()<<endl;
chat(socket);
}
}
}
void chat(sf::TcpSocket * socket )
{
sf::RenderWindow window(sf::VideoMode( 300, 400, 32), "chat");
sf::Packet packet1;
packet1<<id;
socket->send(packet1);
socket->setBlocking(false);
window.setTitle(id);
std::string text = "";
std::vector<sf::Text> chat;
sf::Font font;
if( !font.loadFromFile("Candarab.ttf") )
cout<<"can't find the file"<<endl;
while( window.isOpen() )
{
sf::Event event;
while( window.pollEvent( event ))
{
switch( event.type )
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Escape)
window.close();
else if(event.key.code == sf::Keyboard::Return)
{
sf::Packet packet;
packet<<id + ": " + text;
socket->send(packet);
sf::Text displayText( text, font, 20) ;
displayText.setColor(sf::Color::Green);
chat.push_back(displayText);
text = "";
}
break;
case sf::Event::TextEntered:
text += event.text.unicode;
}
}
sf::Packet packet1;
socket->receive(packet1);
std::string tempText;
if( packet1 >> tempText )
{
sf::Text displayText( tempText, font, 20) ;
displayText.setColor(sf::Color::Blue);
chat.push_back(displayText);
}
int i = 0;
for( i; i < chat.size(); i++ )
{
chat
.setPosition( 0, i*20.0 );
window.draw(chat);
}
sf::Text drawText( text, font, 20) ;
drawText.setColor(sf::Color::White);
drawText.setPosition( 0, i*20.0 );
window.draw(drawText);
window.display();
window.clear();
}
}
bool isAnyRequest( sf::TcpSocket * socket )
{
socket->setBlocking(false);
sf::Packet packet;
string request;
if( socket->receive(packet) )
{
packet>>request;
if( request == "-10-1*0-01-r" )
{
sf::RenderWindow window(sf::VideoMode( 300, 400, 32), "chat");
window.close();
return 1;
}
}
return 0;
}
void chatOnRequest( sf::TcpSocket * socket )
{
socket->setBlocking(true);
sf::RenderWindow window(sf::VideoMode( 300, 400, 32), "chat");
sf::Packet packet1;
window.setTitle(id);
std::string text = "";
std::vector<sf::Text> chat;
sf::Font font;
if( !font.loadFromFile("Candarab.ttf") )
cout<<"can't find the file"<<endl;
while( window.isOpen() )
{
sf::Event event;
while( window.pollEvent( event ))
{
switch( event.type )
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Escape)
window.close();
else if(event.key.code == sf::Keyboard::Return)
{
sf::Packet packet;
if( text == "y" && socket->isBlocking() )
{
packet<<text;
socket->send(packet);
socket->setBlocking(false);
sf::Text displayText( text, font, 20) ;
displayText.setColor(sf::Color::Green);
chat.push_back(displayText);
text = "";
socket->setBlocking(false);
packet.clear();
window.clear();
}
else
{
packet<<id + ": " + text;
sf::Text displayText( text, font, 20) ;
displayText.setColor(sf::Color::Green);
chat.push_back(displayText);
text = "";
}
}
break;
case sf::Event::TextEntered:
text += event.text.unicode;
}
}
sf::Packet packet1;
socket->receive(packet1);
std::string tempText;
if( packet1 >> tempText )
{
sf::Text displayText( tempText, font, 20) ;
displayText.setColor(sf::Color::Blue);
chat.push_back(displayText);
}
int i = 0;
for( i; i < chat.size(); i++ )
{
chat.setPosition( 0, i*20.0 );
window.draw(chat);
}
sf::Text drawText( text, font, 20) ;
drawText.setColor(sf::Color::White);
drawText.setPosition( 0, i*20.0 );
window.draw(drawText);
window.display();
window.clear();
}
}
void checkRequest( sf::TcpSocket* socket )
{
sf::Mutex mutex;
sf::Thread thread( &chat, socket );
while( true )
{
if( isAnyRequest( socket ) )
{
mutex.lock();
thread.launch();
mutex.unlock();
}
}
}