Heisenberg
Heisenberg

Reputation: 1

Android Java Server Socket does not connect

I am writing an app which needs to receive a string from a server. The following code works if the IP Adress connected to is "127.0.0.1" (The Client and the server are on the same phone, just for testing purpose), but not if it is the "real" IP Adress of the phone.

Server:

ServerSocket echoServer = null;
        String line;
        DataInputStream is;
        PrintStream os;
        Socket clientSocket = null;

        // Try to open a server socket on port 9999
        try {
            echoServer = new ServerSocket(1109);
        } catch (IOException e) {
            System.out.println(e);
        }
        // Create a socket object from the ServerSocket to listen and
        // accept
        // connections.
        // Open input and output streams

        try {
            clientSocket = echoServer.accept();
            is = new DataInputStream(clientSocket.getInputStream());
            os = new PrintStream(clientSocket.getOutputStream());

            // As long as we receive data, echo that data back to the
            // client.

                os.println("Das ist ein Test immernoch");
                publish("Fertig");
        } catch (IOException e) {
            publish("Fertig");
        } catch (Exception e) {
            publish("Fertig");
        }

Client:

Socket smtpSocket = null;
    DataOutputStream os = null;
    DataInputStream is = null;

    try {
        smtpSocket = new Socket();
        smtpSocket.connect(new InetSocketAddress("46.114.153.58", 1109), 10000); //That is the critcal line, if the IP is "127.0.0.1" everything works perfectly fine
        os = new DataOutputStream(smtpSocket.getOutputStream());
        is = new DataInputStream(smtpSocket.getInputStream());
    } catch (UnknownHostException e) {
        return "Fehler";
    } catch (IOException e) {
        return "Fehler";
    }

    if (smtpSocket != null && os != null && is != null) {
        try {

            os.writeBytes("HELO\n");
            String s = is.readLine();
            os.close();
            is.close();
            smtpSocket.close();
            return s;
        } catch (UnknownHostException e) {
            //System.err.println("Trying to connect to unknown host: " + e);
        } catch (IOException e) {
            //System.err.println("IOException:  " + e);
        }
    }
    return "Fehler";
}

EDIT: Hence this is an app for a mobile device, there is no router I can configure.

Upvotes: 0

Views: 947

Answers (3)

Praveen
Praveen

Reputation: 406

If this is the ip address of your router and you are connecting your android mobile to the router through wifi , then you have to port forward the port 1109 in your router to your mobile.

If this is the ip address of your android mobile connected through data connection , then there will be some restrictions on your data provider blocking ports for security .

46.114.153.58 is this a static ip address or dynamic ?

If its a dynamic ip address , first check the availability of the ip address by pinging it.

Upvotes: 0

Don Chakkappan
Don Chakkappan

Reputation: 7560

Add this to your code

if (Build.VERSION.SDK_INT >= 9) {  
                StrictMode.ThreadPolicy policy = new   StrictMode.ThreadPolicy.Builder()
                        .permitAll().build();
                StrictMode.setThreadPolicy(policy);

            }

Upvotes: 1

zeroRooter
zeroRooter

Reputation: 100

You might have to forward the port you are using on your router if you are planning on using ServerSocket with external connections.

Upvotes: 0

Related Questions