I'm trying to do a server-less app for IM. I use apple bonjour protocol to discover xmpp services. But once I get those I can't connect to my host (linux computer using pidgin + bonjour).
Here is my code (taken from here) :
public class Xmpp extends AsyncTask<Void, Void, Void>
{
#Override
protected Void doInBackground(Void... arg0)
{
ConnectionConfiguration connConfig =
new ConnectionConfiguration("192.168.0.11", 5298, "bonjour");
XMPPConnection connection = new XMPPConnection(connConfig);
try
{
// Connect to the server
connection.connect();
// Most servers require you to login before performing other tasks.
connection.login("grea08", "mypass");
// Start a new conversation with John Doe and send him a message.
Chat chat = connection.getChatManager().createChat("grea09#192.168.0.11", new MessageListener() {
public void processMessage(Chat chat, Message message) {
// Print out any messages we get back to standard out.
Log.v(getClass().getName(), "Received message: " + message);
}
});
chat.sendMessage("Howdy!");
} catch (XMPPException e)
{
// TODO Auto-generated catch block
Log.e(getClass().getName(), "Xmpp error !", e);
}
// Disconnect from the server
connection.disconnect();
return null;
}
}
I've got an XmppException "No response from server". I think that the host is not an XMPP server and we must use the protocol this way.
Smack and aSmack has no support for XEP-0174 (aka. link-local or serverless messaging). Jonas patches never made it into the trunk. The corresponding issue to track is SMACK-262.
Gibberbot open source project supports XMPP and Bonjour serverless communication.
It also can be installed from Google Play.
Perhaps you can check out its sources and extract the relevant code for your app. :-)
Try creating your XMPPConnection with a configuration:
ConnectionConfiguration config = new ConnectionConfiguration("192.189.0.11", port);
//Set optional configurations on the config object.
Connection connection = new XMPPConnection(config);
Related
In my android application I want to use the autobahn library to use websockets.
I have done server side code using spring.
Simple socket is working when i am try pub sub then i get error.
My Code :-
final String wsuri = "ws://localhost:8080/ws";
try {
mConnection.connect(wsuri, new Wamp.ConnectionHandler()
#Override
public void onOpen() {
Log.d(TAG, "Status: Connected to " + wsuri);
mConnection.subscribe("ws://localhost8080/ws/onetoone",
MyEvent1.class, new Wamp.EventHandler() {
#Override
public void onEvent(String topicUri, Object event) {
Log.d(TAG, "Status: Connected to " + event);
}
});
}
#Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection lost." + " " + reason);
}
});
} catch (Exception e) {
Log.d(TAG, e.toString());
}
OutOut :-
de.tavendo.autobahn.WebSocketConnection: created
de.tavendo.autobahn.WebSocketReader: created
de.tavendo.autobahn.WampReader: created
de.tavendo.autobahn.WampConnection: reader created and started
de.tavendo.autobahn.WebSocketWriter: created
de.tavendo.autobahn.WampWriter: created
de.tavendo.autobahn.WampConnection: writer created and started
de.tavendo.autobahn.WebSocketReader: running
de.tavendo.autobahn.WebSocketReader: run() : WebSocketException
(de.tavendo.autobahn.WebSocketException: RSV != 0 and no extension negotiated)
de.tavendo.autobahn.WebSocketReader: ended
de.tavendo.autobahn.WebSocketConnection: opening handshake received
Status: Connected to ws://localhost:8080/Spring4WebSocket/add
de.tavendo.autobahn.WebSocketConnection: fail connection [code = 4, reason = WebSockets protocol violation
de.tavendo.autobahn.WebSocketReader: quit
de.tavendo.autobahn.WebSocketConnection: waiting for reader to finish
de.tavendo.autobahn.WebSocketConnection: readr thread done
de.tavendo.autobahn.WebSocketConnection: sending close message over socket
de.tavendo.autobahn.WebSocketWriter: ended
de.tavendo.autobahn.WebSocketConnection: waiting for writer to finish
de.tavendo.autobahn.WebSocketConnection: writer thread done
Connection lost. WebSockets protocol violation
I have pass ip address place on local host(my PC ip address).
Any have solution for how to solve web Sockets protocol violation in android autobahn.
Please help for above problem.
The error message:
RSV != 0 and no extension negotiated
implies that one or more of the reserved bits of a WebSocket frame which your WebSocket client received from your WebSocket server was not 0. If no WebSocket extension was negotiated (during the WebSocket opening handshake) as the error message claims, the reserved bits should be all zeros.
Check whether the endpoint of your WebSocket server is properly speaking the WebSocket protocol.
I'm writing an Android app using sockets for communication. In a class called sever I accept clients (android devices) and open sockets for them.
Server side:
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Socket socket = serverSocket.accept();
Client clientThread = new Client(socket);
System.out.println("New client: " + clientThread.getName());
new Thread(clientThread).start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
After a succesful connection the user may create a group, which is like a room for number of clients to connect and play together.
The group creation is done here:
Server side:
Client:
private void client_create() {
this.mGroup = new Group();
mGroup.joinPlayer(this);
System.out.println("New group for: " + name);
}
Group:
public Group(int nClients){
// Clients in this group
this.clients = new ArrayList<Client>();
}
public void joinPlayer(Client player){
clients.add(player);
}
Client Side:
Connection handling:
try {
socket = new Socket(hostName, portNumber);
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
Listener listener = new Listener();
new Thread(listener).start();
} catch (IOException e) {
e.printStackTrace();
}
I ran this programm on 2 android devices and on my localhost as the server. After the connection was made, I tried creating 2 independent different groups. While debugging it all seems legit until I reached to a point where I lost it due to the 2 different running threads.
The odd thing that happened is that after the first group was created with the first client (clients contains the first device client object), and then the second group with the second player (clients contains the second device client object), the first group clients array contains the second client object (from the second device).
Have you got any thoughts on that? Did I do something wrong?
Figured it out.
Clients was mistakly defined as static, so I guess when accessing to the clients array of the static object it received the last one who was created.
I try to create a Simple TCP Server on Android phone and waiting for client.
I only want to implement the connection between TCPServer and Client , it doesn't need to transmit any data.
I have the another application for client , It use to connect to this TCPServer.
The code of TCPServerthread is like the following.
private class TCPServerThread implements Runnable
{
#Override
public void run() {
// TODO Auto-generated method stub
try {
ServerSocket serverSocket = new ServerSocket(PORT);
//while loop
while (true) {
Log.i(TAG, "TCPServerThread...while loop");
try {
Socket socket = serverSocket.accept();
Log.i(TAG, "TCPServerThread...socket.getInetAddress() = " + socket.getInetAddress());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Log.i(TAG, "First IOException");
}
}
//while loop end
} catch (Exception e) {
// TODO: handle exception
//e.printStackTrace();
Log.i(TAG, "Second IOException");
}
}
}
But it seems stop at Socket socket = serverSocket.accept(); and doesn't show the log of TCPServerThread...socket.getInetAddress() = when the client try to connect to this Server.
DO I missing something for TCPServer ?
Is it mean the client doesn't connect to the Server when the code stop at Socket socket = serverSocket.accept(); ??
----------------------------EDIT----------------------------------------
Update the process.
The Server(Android Phone) open the WiFi-Hot-Spot, it also open the TCP-Server like the above code.
After Client connect to WiFi-Hot-Spot , the Client and the Server are in the same network.
The Client will get a IP address of gateway, and the Client try to connect to this IP address of gateway by TCP.
So the connection port and Server address seems correct for Client.
Your code is correct, but it seems that no one is connecting to your TCPserver.
To avoid this blocking situation on
Socket socket = serverSocket.accept();
you have to set the timeout option for your socket when you declare it
serverSocket.setSoTimeout(mTime);
;)
I'm new to asmack and openfire, looked a lot for a working answer to this but couldn't find it anywhere. How do I retrieve offline messages on Logging into my account on asmack?
I've used the following code:
configure(ProviderManager.getInstance()); //configuring providers before creating a connection
ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT);
connConfig.setSendPresence(false);
connection = new XMPPConnection (connConfig);
try {
connection.connect();
} catch (XMPPException ex) {
setConnection(null);
}
try {
connection.login(username, password);
try {
OfflineMessageManager offlineManager = new OfflineMessageManager(
connection);
Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager
.getMessages();
System.out.println(offlineManager.supportsFlexibleRetrieval());
System.out.println("Number of offline messages:: " + offlineManager.getMessageCount());
Map<String,ArrayList<Message>> offlineMsgs = new HashMap<String,ArrayList<Message>>();
while (it.hasNext()) {
org.jivesoftware.smack.packet.Message message = it.next();
System.out
.println("receive offline messages, the Received from [" + message.getFrom()
+ "] the message:" + message.getBody());
String fromUser = message.getFrom().split("/")[0];
if(offlineMsgs.containsKey(fromUser))
{
offlineMsgs.get(fromUser).add(message);
}else{
ArrayList<Message> temp = new ArrayList<Message>();
temp.add(message);
offlineMsgs.put(fromUser, temp);
}
}
// Deal with a collection of offline messages ...
offlineManager.deleteMessages();
} catch (Exception e) {
Log.e("CATCH","OFFLINE");
e.printStackTrace();
}
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
setConnection(connection);//Packet Listener
// Set the status to available
} catch (XMPPException ex) {
setConnection(null);
}
From what I've read, once a connection is established, Openfire automatically sends offline messages to the user. (if any) Which means that by just setting packet listeners after logging in, I should be able to retrieve the messages. However, this didn't work for me. Which's why I tried using OfflineMessageManager. It always shows 0 messages as the message count though. I even logged in to the mysql db which the server's using and checked the offline messages folder. The messages exist till the user logs in, which means that the messages are being sent but it isn't being retrieved by the app. I can't seem to find out how implement this. If anyone has a working solution, It'd be greatly appreciated.
i partially solved added a packet listener after login but the same problem persist when internet go down. in that case i intercept reconnectionSuccessful event and remove and add again the packet listener but the messages sent when the user was offline is lost .
Somebody have the best solution to solve it?
Send your presence afetr logging in to the XMPP server. You have forgot to add packetListener which listens for the upcoming offline messages.
Hope this works.
I m try to connect with Xmpp server,But i m getting exception
Login exception SASL authentication failed using mechanism DIGEST-MD5
i use this code ,can any one help me,or code
try {
if (xmppConnection == null) {
ConnectionConfiguration config = new ConnectionConfiguration(
SERVER_HOST, SERVER_PORT, SERVICE_NAME);
xmppConnection = new XMPPConnection(config);
System.out.println("xmppConnection"+xmppConnection);
}
if (!xmppConnection.isConnected()) {
xmppConnection.connect();
System.out.println("Connecting");
}
System.out.println("facebook id get xmpp "+username);
if (!xmppConnection.isAuthenticated()) {
xmppConnection.login(username, "123");
System.out.println("User is authenticated ");
}
Presence presence = new Presence(Presence.Type.available);
xmppConnection.sendPacket(presence);
} catch (Exception e) {
System.out.println("Login exception "+e);
e.printStackTrace();
}
In the Openfire configuration it is machinename.domain.com
This SASL mechanism uses also the Xmpp Domain name for authentication, not only username and password. This is why authentication fails.
mean your username & password must be like:
username: abc111#domain.com (whatever your domain name)
password: abcabc111
for more detail check this conversation.
I am using below code in my, its working fine in here..
try {
ConnectionConfiguration connConfig = new ConnectionConfiguration("HOST_IP", Integer.parseInt("PORT_NO"));
XMPPConnection connection = new XMPPConnection(connConfig);
connection.connect();
try {
// Login
connection.login("USER_NAME", "PASSWORD");
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
xmppClient.setConnection(connection);
} catch (XMPPException ex) {
Log.w("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
Log.w("XMPPClient", ex.toString());
xmppClient.setConnection(null);
}
} catch (XMPPException ex) {
Log.w("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
Log.w("XMPPClient", ex.toString());
xmppClient.setConnection(null);
}
And I have also added smack.jar file.
Please check below post, i think it might help you..
https://stackoverflow.com/a/6659403/1849482
And many users are getting this error in login. check below links for More Information..
http://community.igniterealtime.org/thread/44219
http://code.google.com/p/asmack/issues/detail?id=33
asmack uses Novell's Open LDAP DigestMD5SaslClient.java which does not support the escape of backslash ('\') and double-quote ('"') for SASL. According to XEP-0106, user name "user#company.com" should be encoded as "user\040company.com". Fully complying with SASL spec, it should be encoded as "user\\040company.com", but asmack didn't. If your XMPP server (e.g. Openfire) uses Java's SASL implementation, it will have a mismatch. Smack 3.x uses Java's SASL implementation, so it works fine. Smack 4.x is supposed to replace asmack, but I don't know if Smack 4.x uses Novell or Java SASL implementation.
BTW, this problem exists in iOS xmppframework as well.
If you are interested in my fix, I'll post it somewhere.
Here is the link to the revised DigestMD5SaslClient.java with the escape quoted string.