Hi,
I was working on an application but i'm stuck at a point now..
I'm able to send the chat messages but unable to receive any...
I have two threads, 1 for sending message and another for receiving but receiving part isn't working!
I think the problem is with multithreading! can anyone help me out??

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java_chat.Java_chat.chat;
import static java_chat.Java_chat.connection;
import static java_chat.Java_chat.free;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;  
import org.jivesoftware.smack.XMPPConnection; 
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.XMPPException;  
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Packet;

public class Java_chat {  
   private static String username = "some username";  
   private static String password = "some password";  
   public static String id = "";
   ConnectionConfiguration connConfig;  
   public static XMPPConnection connection;
   public static Chat chat;
   public static boolean free = false;

   public Java_chat() throws XMPPException {  
     connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");  
     connection = new XMPPConnection(connConfig);  
     connection.connect();  
     connection.login(username, password);  
   } 


   public static void main(String[] args) throws XMPPException,IOException, InterruptedException {  
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     Java_chat gtalkChat = new Java_chat();  
     System.out.print("Enter the id : ");
     id = br.readLine();
     chat = connection.getChatManager().createChat(id, null);
     misc m = new misc();
     sender send = new sender(m);
     receive rcv = new receive(m);
   }  
 } 

class sender extends Java_chat implements Runnable {
    misc m;
    public sender(misc m) throws XMPPException {
        this.m = m;
        new Thread(this,"sender").start();
    }

    @Override
     synchronized public void run() {
       while(true) {
           m.send();
       }
    }
}

class receive extends Java_chat implements Runnable {
    misc m;
    public receive(misc m) throws XMPPException {
        this.m = m;
        new Thread(this,"receive").start();
    }
    @Override
   synchronized public void run() {
       while (true) {
           m.receiver();
       }

    }

}

//class with both sending and recieving functions.

class misc extends Java_chat {
    public misc() throws XMPPException {
    }
    PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class));
    PacketCollector collector = connection.createPacketCollector(filter);
    public String msg = "";
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));

    synchronized public void receiver() {
        while(!free) {
              try {
                System.out.println("receive de-active");
                wait();
              } catch (InterruptedException ex) {
               System.out.println(ex);
           } 
           }
            System.out.println("receive active");
                System.out.println("loop");
                Packet packet = collector.nextResult();
                if (packet instanceof Message) {
                    Message message = (Message) packet;
                        if (message != null && message.getBody() != null)
                            System.out.println("Received message from "
                                    + packet.getFrom() + " : "
                                    + (message != null ? message.getBody() : "NULL"));
            }   

            free = false;
            notify();
    }

    synchronized public void send() {
         while(free) {
           try {
               System.out.println("sender de-active");
               wait();                

           } catch (InterruptedException ex) {
               System.out.println(ex);
           } 
           }  
            System.out.println("sender active");
           try {
               System.out.print("Me: ");
               msg = buff.readLine();
               chat.sendMessage(msg);
               System.out.println();
               free = true;
               notify();
           } catch (   XMPPException | IOException ex) {
               System.out.println(ex);
           }
    }
} 

The sample code provided for Smack API works fine with gTalk credentials, run that code and then modify that code according to your rerquirement.

This article has been dead for over six months. Start a new discussion instead.