1

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("987654321@component.localhost");
    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;
Slava
  • 474
  • 1
  • 4
  • 11
  • Not sure how this could work on the pc if you don't call `connection.login()`. – Flow Aug 11 '12 at 09:24
  • You know this is actually a very bright idea. However, I used connection.loginAnonymously(). Thank you, Flow, you've made my day. – Slava Aug 11 '12 at 11:13
  • 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](http://www.igniterealtime.org/builds/smack/docs/latest/documentation/debugging.html) and/or attach an debugger to find out what's happening. – Flow Aug 11 '12 at 11:57

2 Answers2

0

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.

Slava
  • 474
  • 1
  • 4
  • 11
0

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

lynn8570
  • 1,685
  • 1
  • 14
  • 24