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.