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.

  • 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. – user207421 Apr 17 '14 at 09:29

3 Answers3

1

Add this to your code

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

            }
Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
0

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

zeroRooter
  • 100
  • 1
  • 6
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.

Praveen
  • 366
  • 3
  • 12
  • 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? – Heisenberg Apr 17 '14 at 12:05
  • 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 – Praveen Apr 17 '14 at 12:26
  • @Heisenberg what is the exception you are getting ? – Praveen Apr 17 '14 at 12:52
  • @Heisenberg are you able to ping your ip address ? – Praveen Apr 18 '14 at 06:39
  • @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 . – Praveen Apr 18 '14 at 07:19
  • @Praven I used [this code](http://stackoverflow.com/questions/14576710/ping-application-in-android/) to ping and there is 100% packet loss. What is going wrong? – Heisenberg Apr 18 '14 at 08:26
  • @Praven I use [this site](wieistmeineip.de) to get the IP-Adress. – Heisenberg Apr 18 '14 at 08:38
  • @Heisenberg who is data provider ? May be your data provider might have blocked ping port and your IP address might change dynamically . Just curious , you are enabling the data connection, checking the ip address and then using the same IP in your client side , am i right? – Praveen Apr 18 '14 at 10:17
  • @Praven and I already tried other ports. Yes, you are right. I am checking the IP, compile the app with this IP and run it. – Heisenberg Apr 18 '14 at 10:50
  • @Heisenberg I don't know about your data provider . But some data providers will block incoming connections . If you have a router can you connect your mobile to the router and port forward to your mobile and check the connection. – Praveen Apr 18 '14 at 11:46