質問

I have a problem with my XMPP Java client.

I have a XMPP server (based on Ejabberd, 10.30.16.147) and two Pidgin users: test1@localhost (on My Windows machine, 10.30.16.199) and test2@locahost (on the Linux server machine, 10.30.16.147).

When I use the two Pidgin clients, the chat works fine. Now I am trying to create a Java client just to emulate one of the two Pidgin client. The code is as below (I have imported the Smack 3.4.1 library in my Eclipse project):

package test2XMPP;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.MessageListener;

public class Sender {

    public static void main(String a[]) throws XMPPException, InterruptedException {
        ConnectionConfiguration config = new ConnectionConfiguration("10.30.16.147", 5222);
        XMPPConnection connection = new XMPPConnection(config);
        connection.connect();
        connection.login("test1@locahost", "test1");

        Chat chat = connection.getChatManager().createChat("test2@localhost", new MessageListener() {

            public void processMessage(Chat chat, Message message) {
                System.out.println("Received message: " + message);
            }
        });

        chat.sendMessage("Howdy test1!");

        while (true) {
            Thread.sleep(50);
        }
    }
}

I get the following console output:

    apr 18, 2014 10:19:37 AM org.jivesoftware.smack.provider.UrlProviderFileInitializer initialize
    INFO: Loading providers for file [classpath:META-INF/core.providers]
    apr 18, 2014 10:19:37 AM org.jivesoftware.smack.provider.UrlProviderFileInitializer initialize
    INFO: Loading providers for file [classpath:META-INF/extension.providers]
    Exception in thread "main" java.lang.IllegalStateException: Not connected to server.
        at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:230)
        at org.jivesoftware.smack.Connection.login(Connection.java:368)
        at test2XMPP.Sender.main(Sender.java:18)

I get these error also if I'm already disconnected with my Pidgin test1@localhost client.

Do you have any suggestion? Thanks

役に立ちましたか?

解決

Use new ConnectionConfiguration("10.30.16.147", 5222, "localhost"); and connection.login("test1", "test1");

他のヒント

Had a similar experience, issue was with Hostname & Domain name being different. I was using single argument constructor of ConnectionConfiguration class.

ConnectionConfiguration config = new ConnectionConfiguration(hostname);

Just needed to identify Hostname of the machine where Ejabberd was installed & made sure that both of them were the same for the client's to communicate across.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top