0

Im making an Android app that communicates with a server. When the Android-phone receives a SMS I want it to send it thru a socket to my server.

public class Server {

public static final int PORT = 8080;
public static void main(String[] args) throws IOException {
    ServerSocket s = new ServerSocket(PORT);
    try {
        Socket socket = s.accept();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String str = in.readLine();
            System.out.println(str);
        } 
        finally {
            socket.close();
        }
    } 
    finally {
        s.close();
    }
} 

}

This is my client:

public Client(String message){

    try {
        Socket socket = new Socket("127.0.0.1", 8080);
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
        out.println(message);
        socket.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

Im not getting any connection when Im calling the Client class from the android app. Anyone know how to fix this problem?

The class 'Client' is called from the onReceive(Context context, Intent intent){ method when a message is received on the phone.

Renato Gama
  • 16,431
  • 12
  • 58
  • 92
rtc11
  • 747
  • 8
  • 23
  • What is the error that you get? please post stack trace. Also, are you testing it on a simulator or real device? – MByD Nov 29 '11 at 20:23
  • There is no stack trace because the server is waiting for a respond. Im running a simulator and waiting for a respond on a program run in java application. – rtc11 Nov 29 '11 at 20:27
  • Can you test your server socket using a simple telnet ? – Emmanuel Sys Nov 29 '11 at 20:29
  • I could try, just have to learn how to do so first. – rtc11 Nov 29 '11 at 20:34
  • When I start the Server.java an type telnet 127.0.0.1 8080 in cmd there is only a black screen. I assume that there is a connection – rtc11 Nov 29 '11 at 20:45
  • 127.0.0.1 is your 'home' ip address, it is not routable from outside of the device itself. – MrZander Nov 29 '11 at 22:11

2 Answers2

2

Your Android Client is trying to create a socket connecting to the IP 127.0.0.1 which is the loopback address. You are essentially trying to connect to yourself

Change

Socket socket = new Socket("127.0.0.1", 8080);

to

Socket socket = new Socket("your server's IP", 8080);

Please don't literally change it to "your server's IP" :) make it 192.168.1.104 or whatever the servers address is on the network.

dymmeh
  • 22,247
  • 5
  • 53
  • 60
  • Im using the IPV4-address now. When I test it with telnet I get a respond, so I know there is a connection. But still it wont interfere with the android-application. – rtc11 Nov 29 '11 at 23:45
  • Are you using an external IP address or an IP address local to your network? – dymmeh Nov 30 '11 at 00:23
  • It is a local address, Im running the server application and the android simulator on my laptop. – rtc11 Nov 30 '11 at 09:20
  • Socket socket = new Socket("78.91.18.55", 8080); //this works from other computers, but not from this app – rtc11 Nov 30 '11 at 14:42
1

I solved it, forgot to add this to my androidmanifest.xlm

< uses-permission android:name="android.permission.INTERNET">< /uses-permission>
serg10
  • 31,923
  • 16
  • 73
  • 94
rtc11
  • 747
  • 8
  • 23