powered by Jive Software

Reading In message and sending

Hi

Im have the following code, in which im trying to send a message to the user defined in the code. But as I enter the text in the command line and hit enter the message does not appear in the other user. I understand that this may be a Java problem but Im not sure, so please excuse my poor Java if im not reading in the text properly!

Thanks

import java.io.*;

import org.jivesoftware.smack.*;

import org.jivesoftware.smack.packet.Packet;

import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.filter.*;

public class JabberClient extends Thread implements PacketListener {

public static void main(String [] args) throws Exception {

XMPPConnection connection = new XMPPConnection(“localhost”);

connection.login(“pateldw”, “intel1”);

Chat newChat = connection.createChat(“dilip1@localhost”);

System.out.print("Text: ");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String text = null;

try {

text= br.readLine();

} catch (IOException ioe) {

System.out.println(“IO error trying to read !”);

newChat.sendMessage(“works” + text);

}

//Message message = chat.createMessage();

//message.setBody(messageText);

//chat.sendMessage(message);

// newChat.sendMessage(“This is a Test”);

PacketFilter filter = new PacketTypeFilter(Message.class);

JabberClient listener = new JabberClient();

listener.start();

connection.addPacketListener(listener, filter);

}

public void processPacket(Packet packet) {

Message message = (Message)packet;

System.out.println("From: " + packet.getFrom());

System.out.println("Body: " + message.getBody());

System.out.println("----


");

}

public void run() {

while (true) {

try {

Thread.sleep(10);

}

catch (Exception e) { }

}

}

}

Dilip

This is not a java programming forum, but here goes

Check the code and locate

ttry {
text= br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read !");
newChat.sendMessage("works" + text);
}

Change this to

ttry {
  text= br.readLine();
  newChat.sendMessage("works" + text);
}
catch (IOException ioe)
{
  System.out.println("IO error trying to read !");
}

Your sendMessage is within the catch block and may never be executed.

-Rajesh