Linux server with Android client: connection timed out

245 views Asked by At

I am working on an android app that is connected to a server programmed in Java. The server and the client are communicating as programmed when I test the server application on Windows.

Now I have moved the experiment to Linux Ubuntu OS. The Android app (client) is able to send strings but when the server on Linux tries to send string to android it fails. It takes longer time and gives this error:

Testing.NewJFrame$1 run
SEVERE: null
java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at Testing.NewJFrame$1.run(NewJFrame.java:178)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Here is the code at server side:

Socket clientSocket = null;
ServerSocket serverSocket = null;
String clientIP;
int port = 4447;

and based on some condition the following will be triggerred:

System.out.println("Attempting connect to "+clientIP+" through "+ port);
Socket socket = new Socket(clientIP, port);
System.out.println("Connection made");
OutputStream output = socket.getOutputStream();
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output)), true);
out.println(appName + " has been installed and launched in Background");
System.out.println("Message sent...");

But it stops at the line "Attempting to connect......"

Here is the code at client side:

public void listenToServer(){
    new Thread (new Runnable(){
        @Override
        public void run() {
            try {
                serverSocket = new ServerSocket(4447);
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (true) {
                try {
                    client = serverSocket.accept();
                    Scanner in = new Scanner(client.getInputStream());
.
.
.
.
.

Please take note that these two codes both work fine in Windows but it is not working on Linux Ubuntu!

1

There are 1 answers

0
Saeed On BEST ANSWER

I would like to share the answer that I found couple of weeks ago in case anyone is interested.

I found that the reason behind that error in the question is the network configuration that I am using. I work from my office in the University and the apply some level of security by allowing this kind of p2p communication to send messages to local computers but not the other way around. So I am able to send messages from android to the server which is basically my desktop. The latter is connected through an isolated network to ensure some safety level.

Sorry for getting back late and thanks for you guys who shared their views