import java.util.objects;import org.jivesoftware.smack.abstractxmppconnection;import org.jivesoftware.smack.connectionconfiguration.securitymode;import org.jivesoftware.smack.chat.chat; import org.jivesoftware.smack.chat.chatmanager;import org.jivesoftware.smack.chat.chatmanagerlistener;import org.jivesoftware.smack.chat.chatmessagelistener; import org.jivesoftware.smack.packet.message;import org.jivesoftware.smack.tcp.xmpptcpconnection; import org.jivesoftware.smack.tcp.xmpptcpconnectionconfiguration;public class smackclient { public static void main (String[] args) { try { String serviceName = "XXXXXX"; String host = "192.168.0.1"; int port = 5222; String myUsername = "user1"; String myPassword = "123456"; String friendUsername = "User2"; XMPPTCPConnectionConfiguration.Builder Builder = xmpptcpconnectionconfiguration.builder (); builder.setusernameandpassword (Myusername, mypassword); builder.setservicename (ServiceName); builder.sethost (host); &nBsp; builder.setport (port); Builder.setsecuritymode (securitymode.disabled); builder.setdebuggerenabled (False); xmpptcpconnectionconfiguration config = builder.build (); AbstractXMPPConnection c = new Xmpptcpconnection (config); c.connect (); c.login (); system.out.println ("authenticated = " + C.isauthenticated ()); chatmanager Chatmanager = chatmanager.getinstAncefor (c); chat newchat = chatmanager.createchat (friendusername + "@" + servicename); final chatmessagelistener messagelistener = new chatmessagelistener () { @Override public void processmessage (Chat arg0, message message) { string messagebody = message.getbody (); if (Objects.nonnull ( MessageBody))  { &NBsp; try { Arg0.sendmessage ("What you just said:" + messagebody); } catch ( Exception e) { } } } }; ChatManagerListener chatManagerListener = new chatmanagerlistener () { @Override public void chatcreated (CHAT CHAT, BOOLEAN ARG1) { chat.addmessagelistener (MessageListener); } }; chatmanager.addchatlistener ( Chatmanagerlistener); try { newchat.sendmessage ("Here I Come"); } catch (exception e) { system.out.println (" Error delivering block "); } while (True) ; } catch (exception e) { e.printstacktrace (); } }}
The attachments are all the class library jar packages that are required.
This article is from the "zl1030 Records" blog, so be sure to keep this source http://zl1030.blog.51cto.com/274507/1846938
Java is simple to chat with other people through openfire with smack implementation