Reputation: 77
I'm trying to transfer a message between two android phones over the local network. I read sockets where a good way to do this. (I cant use bluetooth) (I cant use NFC either)
I have built a server and client application.
One app has a server that listens for a connection.
The other app has a client that tries to connect when a button is pressed.
Both the manifest files contain the correct permissions. (with the html tags)
uses-permission android:name="android.permission.INTERNET" /
uses-permission >android:name="android.permission.ACCESS_NETWORK_STATE"/
I put the server online first:
ServerSocket myServerSocket = new ServerSocket(27024);
System.out.println("Server is waiting for incoming connection on host=" + InetAddress.getLocalHost().getCanonicalHostName() + ", port=" + myServerSocket.getLocalPort());
Socket socket = myServerSocket.accept();
Then try and connect with the client.
String host = "localhost";
int port = 27024;
try{
System.out.println("Client attempting to connect to server at host: " + host + ", port: " + port);
Socket socket = new Socket(host, port);
//This below line never gets called :(
System.out.println("Client socked created! Now trying to send data to server");
}
In my console:
Client attempting to connect to server at host: localhost, port: 27024
The "Client socked created!" line never gets output.
Both hosts are set to "localhost" and the port number is the same.
I've tried various ports, but nothing happens.
Upvotes: 1
Views: 2590
Reputation: 28639
String host = "localhost";
int port = 27024;
You realize that your client needs to be given the IP address of the server, right?
Note you will most likely not be able to, or ever should, bind an app on a non WiFi interface.
Upvotes: 1