1

Im implementing a chat application using Jabber/XMPP and gloox framework which should send and receive messages concurrently in Ubuntu Linux.

My current code implementation is :

int main()
{
        ...
 int temp = pthread_create(&iSend, NULL, SendMessage, &b);
 int temp1 = pthread_create(&iRecv, NULL, ConnServer, &b);
}

void* ConnServer(void *athis)
{
 UserClient *t = (UserClient*)athis;
 t->ConnecttosServer();
}

bool UserClient::ConnecttosServer()
{
 //JID jid( "map@jo-hely.hu/gloox" );

 j = new Client( iUserJid, iUser.getPassword() );
 j->registerMessageHandler( this);
 j->registerConnectionListener( this );
 j->registerMessageSessionHandler(this);
 bool result = j->connect(false);
 if(result == true)
 {
  iConnected = true;
  ConnectionError er = ConnNoError;
  ConnectionError er1 = ConnNoError;
  while(er == ConnNoError || er1 == ConnNoError)
  {
   er = j->recv(500000);
   sleep(2);
  }
  delete j;
 }
...
}

void* SendMessage(void * athis )// JID *aDest)
{
 UserClient *t = (UserClient*)athis;
 //JID *t = (JID)dest;

 string ip ;
 cout << "enter here";
 cin >> ip;
 if(t->iConnected == true)
 {
  if(t->iMessageSession == NULL )
  {
   string aBody = "hello";

   MessageSession *session = new MessageSession(t->j, t->iDestJid);
   session->registerMessageHandler(t);
   session->send(aBody.c_str());
  } 
 }
}

The problem faced is both the threads are created and pthread_join( ) is called for both.

The iSend thread is scheduled first but gets suspended at cin. Once the recv( ) function is called, which runs in iRecv thread, the recv call back function handleMessage( ) is called. However the control never shifts back to the iSend thread which should call SendMessage( ) function.

Please help

CashCow
  • 30,981
  • 5
  • 61
  • 92
user562701
  • 11
  • 4
  • Quick question - you perform a connect call and send message simultaneously - can you do that or should you be calling them serially? Also, the cin suspends because it's waiting for a character from stdin. Can you type something in? – mj_ Jan 04 '11 at 14:58
  • Besides all your other technical issues here (e.g. memory leaks), you call `j->recv(500000)`. If memory serves, it will block until it receives the number of bytes requested in the buffer size (500000 in this case). Since I doubt you are sending that much data with this simple program, I'd suggest making it a much smaller buffer to start and only increase it if you see the need to do so. – Zac Howland Jan 04 '11 at 16:06
  • Send and Recv is not happening simultaneously. I did comment cin as well, but it didnt change the behaviour. The SendMessage function doesnt get scheduled at all.. – user562701 Jan 04 '11 at 16:54
  • j->recv( ) takes a time argument for how long to sleep, its not a std. socket receive call rather a call to gloox framework. – user562701 Jan 04 '11 at 16:56
  • some googling led me to this. http://camaya.net/glooxlist/dev/msg01657.html however Im not able to fully understand the implementation ie. GetJobFromMainThread – user562701 Jan 04 '11 at 17:01

2 Answers2

0

I cannot see in there how SendMessage ever sends more than one "hello" message.

There are various memory issues here of course, eg j won't get deleted at all if connect failed, and as its scope is function-only there is no real need to create it with new at all.

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • hi, thats my exact doubt. I need to get the user input using cin and more over the iSend thread should be scheduled parallely along with iRecv thread which handles the callback function. – user562701 Jan 04 '11 at 16:57
0

You cannot count on the iSend thread being scheduled first. That is completely up to the operating system.

Mike Lyons
  • 22,575
  • 2
  • 16
  • 19