XMPP,JABBER to communicate with Gtalk..

Hi all, Happy New year to all bloggers, readers and followers. Here is my first post of 2013.Extensible Messaging and Presence Protocol (XMPP) is a communications protocol for message oriented middle ware based on xml (Extensible Markup Language).The protocol was originally named Jabber, and was developed by the Jabber open-source community in 1999 for near-real time instant messaging (IM), presence information , and contact list maintenance. Designed to be extensible, the protocol has also been used for publish-subscribe systems; signalling for VoIP, video, and file transfer.

from this post i am adding Syntax highlighter for better formatting of code.

/**
 *
 */
package com.spark.communications;

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

/**
 * @author Mantha Pavan Kumar
 *
 */
public class GtalkClient {

	/**
	 * @param args
	 */
	public static void main(String args[]) {
		// ConnectionConfiguration connConfig = new
		// ConnectionConfiguration("localhost", 5222);
		// connConfig.setSASLAuthenticationEnabled(false);
		// ConnectionConfiguration connConfig = new
		// ConnectionConfiguration("localhost", 5222);
		ConnectionConfiguration connConfig = new ConnectionConfiguration(
				"talk.google.com", 5222, "gmail.com");
		XMPPConnection connection = new XMPPConnection(connConfig);

		try {
			connection.connect();
			System.out.println("Connected to " + connection.getHost());
		} catch (XMPPException ex) {
			// ex.printStackTrace();
			System.out.println("Failed to connect to " + connection.getHost());
			System.exit(1);
		}
		try {
			connection.login("user@gmail.com", "password");
			System.out.println("Logged in as " + connection.getUser());

			Presence presence = new Presence(Presence.Type.available);
			 // Set the status message
		    presence.setStatus("Online, Programmatically!");

		    // Set the highest priority
		    presence.setPriority(24);

		    // Set available presence mode
		    presence.setMode(Presence.Mode.available);

		    // Send the presence packet through the connection
			connection.sendPacket(presence);
			try {
				Thread.sleep(20000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (XMPPException ex) {
			// ex.printStackTrace();
			System.out.println("Failed to log in as " + connection.getUser());
			System.exit(1);
		}

		ChatManager chatmanager = connection.getChatManager();
		Chat newChat = chatmanager.createChat("recevier@gmail.com",
				new MessageListener() {
					public void processMessage(Chat chat, Message message) {
						System.out.println("Received message: " + message.getBody());
					}
				});

		try {
			newChat.sendMessage("Howdy!");
			System.out.println("Message Sent...");
			try {
				Thread.sleep(20000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (XMPPException e) {
			System.out.println("Error Delivering block");
		}
	}

}

please download smack API latest version and add the libs of smack to run this program.
Happy Coding 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s