Thursday, March 3, 2011

chat program using java

Chatting program using Google account


Before using this program you need to download the following jar

files and include this jar files in to your application libraries.

here we are making a sample chat program for Google.
with this exmaple you can chat with the person who is online.

1.smack.jar
2.smackx-debug.jar
3.smackx-jingle.jar
4.smackx.jar

 
package ChatTest;

import java.util.*;
import java.io.*;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

public class ChatTest implements MessageListener {

XMPPConnection connection;
public String[] ids;

public void login(String userName, String password) throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
config.setSASLAuthenticationEnabled(false);
connection = new XMPPConnection(config);
try {
connection.connect();
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection.login(userName, password);
System.out.println(connection.isAuthenticated());
} catch (Exception e) {
System.out.println(e);
System.exit(0);

}
}

public void sendMessage(String message, String to) throws XMPPException {
Chat chat = connection.getChatManager().createChat(to, this);
chat.sendMessage(message);
}

public void displayBuddyList() {
Roster roster = connection.getRoster();
Collection entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for (RosterEntry r : entries) {

System.out.println(r.getUser());
}
}

public void disconnect() {
connection.disconnect();
}

public void processMessage(Chat chat, Message message) {
if (message.getType() == Message.Type.chat) {
System.out.println(chat.getParticipant() + " says: " + message.getBody());
}
}

public static void main(String args[]) throws XMPPException, IOException {
// declare variables
ChatTest c = new ChatTest();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg;
DataInputStream input = new DataInputStream(System.in);
// turn on the enhanced debugger
XMPPConnection.DEBUG_ENABLED = true;
try {
// Enter your login information here
System.out.println("Enter Your gmail id");
String userId = input.readLine();
System.out.println("Password :");
String password = input.readLine();
userId = userId.replaceAll("@gmail.com", "");
c.login(userId, password);
System.out.println("-----");
c.displayBuddyList();

System.out.println("-----");

System.out.println("Who do you want to talk to? - Type contacts full email address:");
String talkTo = br.readLine();

System.out.println("-----");

System.out.println("All messages will be sent to " + talkTo);
System.out.println("Enter your message in the console:");
System.out.println("-----\n");

while (!(msg = br.readLine()).equals("bye")) {
c.sendMessage(msg, talkTo);
}

c.disconnect();
System.exit(0);
} catch (Exception e) {
System.out.println("Sorry Failed");
System.exit(0);
}
}
}

using this chat program.you can chat with one person at a time.

Sending mail using java program

Chatting program using Google talk account



Before using this program you need to download the following jar files

and include this jar files in to your application libraries.

here we are making a sample chat program for Google.

with this exmaple you can chat with the person who is online.


1.smack.jar
2.smackx-debug.jar
3.smackx-jingle.jar
4.smackx.jar



package ChatTest;

import java.util.*;

import java.io.*;


import org.jivesoftware.smack.Chat;

import org.jivesoftware.smack.ConnectionConfiguration;

import org.jivesoftware.smack.MessageListener;

import org.jivesoftware.smack.Roster;

import org.jivesoftware.smack.RosterEntry;

import org.jivesoftware.smack.SASLAuthentication;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.packet.Message;


public class ChatTest implements MessageListener {


XMPPConnection connection;

public String[] ids;


public void login(String userName, String password) throws XMPPException {

ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");

config.setSASLAuthenticationEnabled(false);

connection = new XMPPConnection(config);

try {

connection.connect();

SASLAuthentication.supportSASLMechanism("PLAIN", 0);

connection.login(userName, password);

System.out.println(connection.isAuthenticated());

} catch (Exception e) {

System.out.println(e);

System.exit(0);


}

}


public void sendMessage(String message, String to) throws XMPPException {

Chat chat = connection.getChatManager().createChat(to, this);

chat.sendMessage(message);

}


public void displayBuddyList() {

Roster roster = connection.getRoster();

Collection entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for (RosterEntry r : entries) {

System.out.println(r.getUser());
}
}

public void disconnect() {
connection.disconnect();
}

public void processMessage(Chat chat, Message message) {
if (message.getType() == Message.Type.chat) {
System.out.println(chat.getParticipant() + " says: " + message.getBody());
}
}

public static void main(String args[]) throws XMPPException, IOException {
// declare variables
ChatTest c = new ChatTest();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg;
DataInputStream input = new DataInputStream(System.in);
// turn on the enhanced debugger
XMPPConnection.DEBUG_ENABLED = true;
try {
// Enter your login information here
System.out.println("Enter Your gmail id");
String userId = input.readLine();
System.out.println("Password :");
String password = input.readLine();
userId = userId.replaceAll("@gmail.com", "");
c.login(userId, password);
System.out.println("-----");
c.displayBuddyList();

System.out.println("-----");

System.out.println("Who do you want to talk to? - Type contacts full email address:");
String talkTo = br.readLine();

System.out.println("-----");

System.out.println("All messages will be sent to " + talkTo);
System.out.println("Enter your message in the console:");
System.out.println("-----\n");

while (!(msg = br.readLine()).equals("bye")) {
c.sendMessage(msg, talkTo);
}

c.disconnect();
System.exit(0);
} catch (Exception e) {
System.out.println("Sorry Failed");
System.exit(0);
}
}
}

using this chat program.you can chat with one person at a time.