Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - hammad khan

Pages: [1]
1
Network / Re: problem with multiple threads in sfml
« on: October 09, 2013, 11:54:35 am »
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();
      }
   }
}

2
Network / problem with multiple threads in sfml
« on: October 09, 2013, 10:40:07 am »
i'm working on a chat massenger, in my program i've written two threads as follows. i'm facing problem with there working. here is the main function of the program

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;  
}
 
sometimes both threads work correctly but sometimes they don't work properly. i don't face any error. is there any problem regarding Operating system?

3
Network / running of multiplae thtreads in sfml simultaneously
« on: September 18, 2013, 09:28:54 am »
i'm doing work on chat massenger and facing difficulty to run multiple threads at the same time.

here is the sample code.

void menu();
void groupChat();
void personalChat();

int main()
{
    sf::Thread thread(&menu());
    thread.launch();
}

void menu()
{
   do
   {
      cout<<"P for group chat"<<endl;
      cout<<"p for group chat"<<endl;
      cout<<"Enter choice";

      switch( choice )
     {
      case 'p':
           sf::Thread thread(&gtoupChat);
           thread.launch();
      case 'g':
           sf::Thread thread(&personalChat);
           thread.launch();
     }
   }while( choice != 'p' choice != 'g' );
}

void groupChat()
{

};

void personalChat()
{

};

in my programm
after menu when he selects a choice, next display of menu wait the termination of the selected thread.
while i want to show menu right after when a menu is selected.
plz reply soon.

4
Network / nonCopyable class error
« on: September 16, 2013, 09:37:59 am »
and when i do this i got this i get the following error.

sf::Thread thread( &sf::TcpSocket::receive, socket);
thread.launch();

error on the first argument is the unknown type error.

and when i do this

void send( sf::TcpSocket socket )

and create a thread like this

sf::Thread thread( &send, socket);
thread.launch();
 get the following error

Error   1   error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'   c:\users\destraction\desktop\laurentgomila-sfml-ef78b6d\include\sfml\network\socket.hpp   178   1   sfml 3.0

5
Network / threads function
« on: September 15, 2013, 12:09:50 pm »
template<typename F >
sf::Thread::Thread    (    F     function   )

Construct the thread from a functor with no argument.

This constructor works for function objects, as well as free function.

Use this constructor for this kind of function:
void function();
// --- or ----
struct Functor
{
void operator()();
};

Note: this does not run the thread, use Launch().
what does the last line mean?

6
Network / blocking mode of recveive function
« on: September 14, 2013, 09:31:50 pm »
Status sf::TcpSocket::receive    (    void *     data,  std::size_t     size,   std::size_t &  received )

Receive raw data from the remote peer.

In blocking mode, this function will wait until some bytes are actually received. This function will fail if the socket is not connected.

what does blocking mode mean, and how i can use this function without blocking mode?

7
Network / Re: mini massenger working
« on: September 14, 2013, 08:31:29 pm »
this function also used to get the ip by computer name, and also by host name.

8
Network / mini massenger working
« on: September 14, 2013, 09:42:57 am »
i'm working on a massenger and i'm facing the following difficulties

first i'm trying to get the IP of a client computer by the name by the following function

sf::IpAddress ip("computer name");

this function works sometimes, and sometimes it don't work, while the client name is present on the LAN.

and second please elaborate the receive function.
how it works.
and if it don't receive anything than what it'll be return.

Pages: [1]
anything