Create a small command line program for chatting with Gtalk based on open-source software

Source: Internet
Author: User

Because Gtalk is developed based on the jabber XMPP protocol, we only need to download a Java XMPP protocol class library to chat with Gtalk, in fact, I hope all im vendors can communicate with each other, so that I can install only one client and chat with everyone. Here we download the open-source smack Java XMPP package provided by igniterealtime, which is:

Http://www.igniterealtime.org/downloads/index.jsp#smack, and then click to download smack_3_0_4.zip. After downloading, you will get a zip file, decompress it, and get the class library file:

Includes documents and class libraries. Here we only need smack. jar and smackx. jar. Now, let's use eclipse or other tools to create a Java project and add these two jar files. For example, eclipse is used here:

Then writeCodeTested. The following is the test code:

Import org. jivesoftware. Smack. Chat;
Import org. jivesoftware. Smack. messagelistener;
Import org. jivesoftware. Smack. xmppconnection;
Import org. jivesoftware. Smack. packet. message;
Import java. Io .*;

Public class chattest {

Public static void main (string [] ARGs) throws exception {
xmppconnection. debug_enabled = true;

Xmppconnection connection = new xmppconnection ("gmail.com ");
Connection. Connect ();
Connection. login ("myeclipsejava", "password ******");
Chat chat = connection. getchatmanager (). createchat ("beansoft@gmail.com", new messagelistener (){
Public void processmessage (chat, message ){
System. Out. println (message. getfrom () + "" + new java. util. Date (). tolocalestring () + "said:" + message. getbody ());
}

});
Bufferedreader cmdin = new bufferedreader (New inputstreamreader (system. In ));
For (;;){
Try {
String cmd = cmdin. Readline ();
If ("! Q ". equalsignorecase (CMD )){
Break;
}
Chat. sendmessage (CMD );
} Catch (exception ex ){
}
}
Connection. Disconnect ();
System. Exit (0 );
}
}

The code structure is very simple, that is, to connect to the Chat Server of gmail.com, and then log on with the account myeclipsejava. The password is kept confidential and harmonious. The createchat () method connects to the object you want to talk to, and then creates a message listener.

The following code cyclically reads information from the command line and sends it to the target object until you enter it! Q can quit the chat. This sectionProgramThe disadvantage is probably that you cannot change the Target chat object. However, it is very useful to set it as an alarm reminder, such as server errors. If all IM groups are interconnected, this will be more useful.

When the code is running, a debugging dialog box is displayed to view the received and sent messages. You can removeXmppconnection. debug_enabled = true;The debugging dialog box will not be displayed again.

Okay, run the Code:

Enter the message and press Enter. Then Gtalk will receive the message:

Haha, there is indeed a fight with Google's introduction interface. If a SWT or swing interface is added, an extremely simple text chat client will be born.

Of course, now we can also build a chat server to test it, and replace all gmail.com in the Code with the IP address of the chat server, for example, localhost, for more information, see kill qq msn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.