HI,
I’'m trying to write a simple Java based Jabber client to send and receive messages from a Jive Messenger server.
I can send a message fine but the connection always gets closed directly after I send the message. Any ideas why this might be the case? I get the following error from the Connection Listener:
java.io.EOFException - no more data available
Here is my code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet; public class chatApp {
static XMPPConnection con = null;
static Chat chat = null;
public static void main(String[] args) {
try{
connect();
if(con.isConnected()){
System.out.println("Connected");
}
System.out.println("Logging into Server");
con.login("john", "Atunas21");
if(con.isAuthenticated()){
System.out.println("authenticated");
}
Chat chat = con.createChat("andrea@monomoy.homelinux.com");
chat.addMessageListener(new PacketListener() {
public void processPacket(Packet arg0) {
System.out.println("Msg received: "+ arg0.toString());
}
});
Message newMessage = chat.createMessage();
newMessage.setBody("Howdy!");
chat.sendMessage(newMessage);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String input; System.out.println("Enter ''quit!'' to quit.");
boolean bWait = true;
while (bWait) { if ((input = in.readLine()).equals("quit!")) { System.out.println("Quitting"); bWait = false;
con.close();
con = null;
System.gc();
System.exit(0);
} else{
System.out.println("Sending Message");
chat.sendMessage(input);
}
// Wait for the next message the user types to us.
Message message = chat.nextMessage();
System.out.println(message.getBody()); }
}catch(XMPPException xe){
System.out.println(xe.toString()+ ": " + xe.getMessage());
xe.printStackTrace();
if(con != null && con.isConnected()){
con.close();
}
}catch(IOException xe){
System.out.println(xe.toString()+ ": " + xe.getMessage());
xe.printStackTrace();
if(con != null && con.isConnected()){
con.close();
}
}catch(Exception xe){
System.out.println(xe.toString()+ ": " + xe.getMessage());
xe.printStackTrace();
if(con != null && con.isConnected()){
con.close();
}
}finally{
if(con != null && con.isConnected()){
con.close();
}
}
}
public static void connect(){
try{
System.out.println("Connecting to Jive Server");
XMPPConnection.DEBUG_ENABLED = true;
con = new XMPPConnection("monomoy.homelinux.com");
con.addConnectionListener(new ConnectionListener(){
public void connectionClosed(){
System.out.println("Connection has been closed");
reconnect();
}
public void connectionClosedOnError(Exception ex){
System.out.println("Connection has been closed because of: " + ex.toString() + ": "+ ex.getMessage());
reconnect();
}
});
}catch(XMPPException xe){
System.out.println(xe.toString()+ ": " + xe.getMessage());
xe.printStackTrace();
if(con != null && con.isConnected()){
con.close();
}
}
}
public static void reconnect(){
try{
boolean bReconnect = false;
if(bReconnect){
System.out.println("Connecting to Jive Server");
XMPPConnection.DEBUG_ENABLED = true;
con = new XMPPConnection("monomoy.homelinux.com");
con.addConnectionListener(new ConnectionListener(){
public void connectionClosed(){
System.out.println("Connection has been closed");
reconnect();
}
public void connectionClosedOnError(Exception ex){
System.out.println("Connection has been closed because of: " + ex.toString() + ": "+ ex.getMessage());
reconnect();
}
});
}
}catch(XMPPException xe){
System.out.println(xe.toString()+ ": " + xe.getMessage());
xe.printStackTrace();
if(con != null && con.isConnected()){
con.close();
}
}
}
}