Hi @ll.
At first I want to mention, that my english isn’'t the best so I hope that all of you will understand my problem.
I want to code a simple communication between UserA and UserB.
UserA should be able to send a message to UserB, while UserB is waiting for the message.
So really simple…
But it doesnt work !
I searched for about 5 hours in the forum but there wasnt any good solution.
It would be very nice if someone could post code so that other beginners can start
easily…
Thanks
My Code:
UserA:
import java.io.IOException;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import com.sun.jmx.snmp.Timestamp;
public class My_Test
{
public static void main(String argc[]) throws IOException
{
My_Test obj = new My_Test();
//Parameter
String host = “rsiegert-ws”;
int port = 5222;
String username = “USERNAME”;
String password = “PASSWORD”;
Timestamp tms = new Timestamp();
long l = tms.getDateTime();
System.out.println(l);
//BENUTZERNAME anmelden
XMPPConnection con = obj.Connect(host,port,username,password);
//BENUTZERNAME schickt “test”
obj.SendMessage(con,“test”);
con.close();
System.out.println(“Ende !”);
}
/**
-
@param host
-
@param port
-
@param username
-
@param password
-
@return
*/
XMPPConnection Connect(String host,int port,String username,String password)
{
try
{
XMPPConnection con = new XMPPConnection(host,port);
con.login(username,password);
System.out.println(“Connected !”);
return con;
}
catch(XMPPException XMPPe)
{
System.out.println("Fehler : "+XMPPe);
return null;
}
}
/**
-
@param con
-
@param message
*/
void SendMessage(XMPPConnection con,String message)
{
try
{
Chat newChat = con.createChat(“1@rsiegert-ws”);
newChat.sendMessage(message);
System.out.println(“Message sent !”);
}
catch(Exception e){System.out.println(e);}
}
}
/code
UserB:
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
public class Wartender
{
public static void main(String argc[]) throws XMPPException
{
Wartender wart = new Wartender();
wart.StartWartender();
}
void StartWartender()
{
String host = “rsiegert-ws”;
int port = 5222;
String username = “1”;
String password = “1”;
XMPPConnection con = Connect(host,port,username,password);
Listen(con);
System.out.println(“Ende”);
}
/**
-
@param host
-
@param port
-
@param username
-
@param password
-
@return
*/
XMPPConnection Connect(String host,int port,String username,String password)
{
try
{
XMPPConnection con = new XMPPConnection(host,port);
con.login(username,password);
System.out.println(“Connected !”);
return con;
}
catch(XMPPException XMPPe)
{
System.out.println("Fehler : "+XMPPe);
return null;
}
}
/**
-
Wartet auf Timestamp vom “Client”
-
@param con
-
@throws XMPPException
*/
void Listen(XMPPConnection conn2)
{
Chat newChat = conn2.createChat(“USERNAME@rsiegert-ws”);
while (true) {
// Wait for the next message the user types to us.
System.out.println(“Waiting…”);
Message message = newChat.nextMessage();
System.out.println("Got it ! : "+message.getBody());
// Send back the same text the other user sent us.
}
}
}
/code
Message was edited by: Khaled83