2
votes

I am trying to implement a simple Android application that sends and recieves packets to and from a plugin written for Openfire server. The plugin is meant to recieve packets from a client for further processing. So it is not a chat. The following code snippet shows my way of sending packets to the server:

ConnectionConfiguration configuration = new ConnectionConfiguration(
        HOST, PORT);
Connection connection = new XMPPConnection(configuration);
try {
    connection.connect();
} catch (XMPPException e) {
    e.printStackTrace();
}
if (connection.isConnected()) {
    Packet packet = new Message();
    packet.setFrom("123456789@localhost");
    packet.setTo("[email protected]");
    connection.sendPacket(packet);
    connection.disconnect();
}

HOST and PORT are predefined constants.

I tried to use code in if clause inside the plugin and it worked perfectlly - component recieves packets and works with them. However, in my Android application this code does not work - packets do not reach the component.

So, guys, if you have any suggestions I will be greatful for your help. Maybe I use wrong technique somewhere - I am new to XMPP and Openfire.


Update

There are all needed permissions in application's manifest. And HOST is equal to a static IP address of the PC running Openfire server.

private static final String HOST = "192.168.1.100";
private static final int PORT = 5222;
2
Not sure how this could work on the pc if you don't call connection.login().Flow
You know this is actually a very bright idea. However, I used connection.loginAnonymously(). Thank you, Flow, you've made my day.Slava
Glad that I could help. If this did the trick, I would also suggest using iq type stanzas instead of message type stanzas. If not, try enabling the Smack debug facilities and/or attach an debugger to find out what's happening.Flow

2 Answers

0
votes

In order to send packets to a server you should login to it using login() or loginAnonymously() methods of org.jivesoftware.smack.Connection class.

Thanks mr. Flow for the hint.

0
votes

Connect and Disconnect

// Create the configuration for this new connection
ConnectionConfiguration config = new ConnectionConfiguration("jabber.org", 5222);
config.setCompressionEnabled(true);
config.setSASLAuthenticationEnabled(true);

Connection connection = new XMPPConnection(config);
// Connect to the server
connection.connect();
// Log into the server
connection.login("username", "password", "SomeResource");
....
// Disconnect from the server
connection.disconnect();
</code>

you should login first, here is the guide to manage connections http://www.igniterealtime.org/builds/smack/docs/latest/documentation/connections.html