I am new to XMPP. For a whole day I am stuck in connecting to my XMPP server (Openfire version 3.9.3) from Java. I am using the Smack (version 4.0.7) library. Here is simple code...
ConnectionConfiguration config =new ConnectionConfiguration("servername",5223);
XMPPTCPConnection connection = new XMPPTCPConnection(config);
// Connect to the server
try {
connection.connect();
connection.login("username", "password");
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
}
But when I run this code this error is showing ...
org.jivesoftware.smack.SmackException$NoResponseException
at org.jivesoftware.smack.XMPPConnection.throwConnectionExceptionOrNoResponse(XMPPConnection.java:548)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.throwConnectionExceptionOrNoResponse(XMPPTCPConnection.java:867)
at org.jivesoftware.smack.tcp.PacketReader.startup(PacketReader.java:113)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.initConnection(XMPPTCPConnection.java:482)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:440)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:811)
at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)
at test.third.<init>(third.java:19)
at test.third.main(third.java:34)
There may be a silly mistake and easy solution. I googled but somehow I am not getting the right solution.
public void connectAndLogin( {
connect();
login();
}
private void connect() {
/**
* Set server configuration
*
* connect to server
*/
setConfiguration();
try {
getConnection().connect();
} catch (XMPPException e) {
e.printStackTrace();
setConnection(null);
}
}
private void setConfiguration() {
ConnectionConfiguration config = new ConnectionConfiguration(Constants.IP);
SmackConfiguration.setPacketReplyTimeout(Constants.PACKET_TIME_OUT);
System.out.println(SmackConfiguration.getVersion());
config.setRosterLoadedAtLogin(false);
// config.setCompressionEnabled(true);
config.setVerifyChainEnabled(false);
config.setReconnectionAllowed(true);
config.setSASLAuthenticationEnabled(false);
config.setSecurityMode(SecurityMode.disabled);
config.setDebuggerEnabled(false);
connection = new XMPPConnection(config);
}
private void login() {
if(getConnection()!=null){
String USER_NAME="";
String PASSWORD="";
try {
getConnection().login(USER_NAME,PASSWORD, Constants.RESOURCE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Try using port 5222 instead of 5223. That's the old SSL way which isn't typically used anymore.
Related
I'm trying to connect to my FTP server in Java SE 1.8. To do so I use this method :
private void connectFTP() {
String server = "ftp.XXXXXXXXXX.site";
int port = 21;
String user = "XXXX";
String pass = "XXXX";
if(!ftpConnexionSuccess.get())
{
client = new FTPClient();
client.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
try {
client.connect(server, port);
ftpConnexionSuccess.set(client.login(user, pass));
if (!ftpConnexionSuccess.get()) {
System.out.println("Could not login to the server");
return;
}
else
{
System.out.println("LOGGED IN SERVER");
client.changeWorkingDirectory("/crypto");
listenedFile = getListenedFile();
System.out.println(listenedFile.getName());
if(listenedFile != null)
{
baseFileTimeStamp.set(listenedFile.getTimestamp().getTimeInMillis());
}
System.out.println(baseFileTimeStamp);
}
} catch (Exception ex) {
System.err.println("FTP connection error : Sleeping for 5 seconds before trying again (" + ex.getMessage() + ")");
ex.printStackTrace();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {e.printStackTrace();}
try {
client.disconnect();
} catch (IOException e) {e.printStackTrace();}
connectFTP();
}
}
}
It works great when I'm on Eclipse and when I export the app on my Windows 10.
Nonetheless, when I try to launch the app on my AWS Webmachine I get a null pointer exception at "listenedFile". The method to listen to this file is the one below.
private FTPFile getListenedFile() {
FTPFile returnedFile = null;
try {
for(FTPFile file : client.listFiles())
{
if(file.getName().contains("filetolisten.txt"))
returnedFile = file;
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
try {
client.disconnect();
} catch (IOException e1) {e1.printStackTrace();}
connectFTP();
return getListenedFile();
}
return returnedFile;
}
I thought it was because of the line
client.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
I tried to delete the line, and to replace SYST_UNIX with SYST_NT, but nothing worked.
I tried to delete the line, and to replace SYST_UNIX with SYST_NT, but nothing worked. Also updated Java, updated the common-nets library. Nothing worked
I've created a java server app which opens connections with serverSocket on port 3000 and it works perfectly with java client app I created. But now I started developing client app in react native and I can't understand it's socket API, because socket.io in react native forces me to use WebSocket way.
Is there any other way?
public static final int PORT = 3000;
private ServerSocket mServerSocket = null;
#Override
public void run() {
try {
mServerSocket = new ServerSocket(PORT);
} catch (IOException e) {
close();
return;
}
while (isOpen()) {
try {
mSocket = mServerSocket.accept();
ClientThread clientThread = new ClientThread(new Client(mSocket, this));
mClients.add(clientThread);
clientThread.start();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
try {
mServerSocket.close();
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I try to connect to db2 from java, here is my code:
public class Db2Connection {
public static void main(String[] args) {
String jdbcClassName="com.ibm.db2.jcc.DB2Driver";
String url="jdbc:db2://localhost:50001/TEST";
String user="user1";
String password="pass";
System.out.println("before try-catch");
Connection connection = null;
try {
System.out.println("try");
//Load class into memory
Class.forName(jdbcClassName);
//Establish connection
System.out.println("before conn");
connection = DriverManager.getConnection(url, user, password);
System.out.println("after conn");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(connection!=null){
System.out.println("Connected successfully.");
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
To be clear, I add db2jcc.jar to my project and run code with valid url. Program cannot jump over line:
connection = DriverManager.getConnection(url, user, password);
I receive no errors or exception, application just not execute. I have no idea how deal with it, can anyone help me?
Try, after removing colon like :
String url="jdbc:db2//localhost:50001/TEST";
This is my class that I have created just to check whether SMTP Service is running or not on the specific port of the host. I am getting the IOException while trying to connect (I have mentioned that in the comment). Am I on the right track of doing this. Is there some other better methods to determine if the service is perfectly running or not in the host at specific port. I am using apache commons library for this.
import org.apache.commons.net.smtp.AuthenticatingSMTPClient;
import java.io.IOException;
import java.net.SocketException;
import java.security.NoSuchAlgorithmException;
public class SmtpServiceTest {
String hostaddress;
Integer port;
public SmtpServiceTest(String hostaddress, Integer port) {
this.hostaddress = hostaddress;
this.port = port;
}
public Boolean execute() {
AuthenticatingSMTPClient client = null;
try {
client = new AuthenticatingSMTPClient();
client.setDefaultTimeout(10 * 1000);
// I get IOException on this line while trying to connect
client.connect(hostaddress, port);
// I am not sure about this too
if (client.execTLS()) {
return true;
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(client != null && client.isConnected()) {
try {
client.logout();
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
}
I have created a client and a server, which both work fine and have no trouble connecting. Now I would like to be able to connect from my android phone to the server. I have imported the kryonet library and copy-pasted the client code into a new android project. The code gave me some errors, but I fixed those. When I tried testing my app, it didn't give me any errors, but my server wasn't getting any connections either. I looked a bit in the LogCat logs, and found out it keeps sticking at "Connecting..". I am totally stuck. This is my android code:
public void openSocket(View view){
Log.i(TAG, "test1");
EditText portText= (EditText)findViewById(R.id.port);
String parts[] = portText.getText().toString().split(":");
partIP = parts[0];
String partPort = parts[1];
port = Integer.parseInt(partPort);
try {
client = new Client();
client.start();
register();
pHandler = new PacketHandler();
connect();
} catch (Exception e) {
pHandler.checkException(e);
}
}
public static void register() {
client.getKryo().register(Packet.class);
client.getKryo().register(Packet1Connected.class);
client.getKryo().register(String.class);
client.getKryo().register(String[].class);
client.getKryo().register(Object[].class);
}
public static void connect() {
try {
String IP = "*MY IP*:25565";
String[] Ip = IP.split(":");
String ipFinal = Ip[0];
int port = Integer.parseInt(Ip[1]);
client.connect(5000, ipFinal, port, port);
} catch (IOException e) {
try {
Thread.sleep(15000);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
System.out.println("Trying to connect.");
connect();
}
Packet1Connected packet = new Packet1Connected();
packet.id = "Android";
client.sendTCP(packet);
}
And this is my normal client code:
public static void main(String[] args) {
try {
client = new Client();
client.start();
register();
pHandler = new PacketHandler();
new Main();
} catch (Exception e) {
pHandler.checkException(e);
}
}
public static void register() {
client.getKryo().register(Packet.class);
client.getKryo().register(Packet1Connected.class);
client.getKryo().register(String.class);
client.getKryo().register(String[].class);
client.getKryo().register(Object[].class);
}
public Main() {
try {
connect();
Thread t = new Thread(this);
t.start();
} catch (Exception e) {
pHandler.checkException(e);
}
}
public static void connect() {
try {
String IP = "*MY IP*:25565";
String[] Ip = IP.split(":");
String ipFinal = Ip[0];
int port = Integer.parseInt(Ip[1]);
client.connect(5000, ipFinal, port, port);
} catch (IOException e) {
try {
Thread.sleep(15000);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
System.out.println("Trying to connect.");
connect();
}
Packet1Connected packet = new Packet1Connected();
packet.id = System.getProperty("user.name");
client.sendTCP(packet);
}
#Override
public void run() {
while (true) {
try {
Thread.sleep(3000);
if (!client.isConnected())
connect();
} catch (InterruptedException e) {
pHandler.checkException(e);
}
}
}
}
So this:
client.connect(5000, ipFinal, port, port);
is the line it gets stuck on, on android. The normal client just connects normally.
Now my question is, is there something wrong with my android code, or is it just the device, or wifi connection?
If it helps, I tried testing it on my samsung galaxy nexus, which is rooted.
Thanks in advance.