Reputation: 788
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.
Upvotes: 0
Views: 2499
Reputation: 788
I solved it, forgot to add this to my androidmanifest.xlm
< uses-permission android:name="android.permission.INTERNET">< /uses-permission>
Upvotes: 1
Reputation: 22306
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.
Upvotes: 2