0

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.

1
  • Code that depends on the correct outcome of a try block should be inside that try block. Not after the corresponding catch block. 'os' and 'is' cannot possibly be null at the point you're testing them. Don't write code like this. Commented Apr 17, 2014 at 9:29

3 Answers 3

1

Add this to your code

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

            }
Sign up to request clarification or add additional context in comments.

2 Comments

and check your firewall status too
How can I check my Firewall on an Android Phone? And where should I put this code? I put it on the top of both client and server, but it still does not work...
0

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

1 Comment

How can I do this when I am on my android phone?
0

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.

10 Comments

The server is on my mobile, so it is dynamic. I wanted to use the "NO-IP" service, but I thought I try it with the original IP first. Which ports are blocked?
So can you ping your ip address ? I tried but i was not able to ping. I don't think ping will be blocked by providers . So if you are able to ping your ip then you can try port more than 10000
@Heisenberg what is the exception you are getting ?
@Heisenberg are you able to ping your ip address ?
@Heisenberg your client socket is in blocking mode . If this channel is in blocking mode then an invocation of connect method will block until the connection is established or an I/O error occurs. If your IP address is non-reachable , then connect will block until tcp send 5 syn packets (in android default /proc/sys/net/ipv4/tcp_syn_retries is 5) i.e a time of approximately 1 min and will return error . So only I'm asking you to check the availability of your ip address .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.