Reputation: 905
I have 2 android devices, one acts as a server that is in tethering mode, and accepting connections to a port. The other device acts as a client that connects to the server hotspot and establishes a connection to the server via a port. (Everything is done without a router in the middle).
The server code looks like this: (When the android server executes the code, the device is in tethering mode)
ServerSocket server = new ServerSocket(PORT);
while (true) {
Socket client;
client = server.accept();
ClientThread com = new ClientThread(client, this);
Thread t = new Thread(com);
t.start();
}
The client code looks like this: (when the android client executes this code, the device is connected to access point)
Socket client = new Socket();
int serverIP = wifiManager.getDhcpInfo().serverAddress;
String stringIP = android.text.format.Formatter.formatIpAddress(serverIP);
InetAddress address = InetAddress.getByName(stringIP);
SocketAddress socketAddress = new InetSocketAddress(address, PORT);
client.connect(socketAddress);
I get the following error on the client
java.net.ConnectException: failed to connect to /192.168.43.1 (port 12345): connect failed: ENETUNREACH (Network is unreachable)
at libcore.io.IoBridge.connect(IoBridge.java:114)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:842)
at java.net.Socket.connect(Socket.java:785)
at com.alternatewifimeshmessaging.HostManager.clientConnect(HostManager.java:283)
at com.alternatewifimeshmessaging.HostManager.client(HostManager.java:189)
at com.alternatewifimeshmessaging.HostManager.run(HostManager.java:59)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
at libcore.io.Posix.connect(Native Method)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
... 8 more
The server gives no errors.
I have the following permission set in the android manifest file:
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
Ideally I would need to transfer some data from server to the client.
What am I doing wrong?
Upvotes: 31
Views: 8452
Reputation: 2312
In a tethered wifi connection, connection provider can not work as a client. So we need to implement a bidirectional tcp socket connection. Use a server socket with a port number in device which is in tethering mode. This device act as a server. In client device use socket to access the client port with IP address.
Upvotes: 1
Reputation: 1747
Server Side
class ServerThread implements Runnable {
public void run() {
socket = new Socket();
try {
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
socket = serverSocket.accept();
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Client Side
class ClientThread implements Runnable {
@Override
public void run() {
try {
socket = new Socket();
InetSocketAddress socketAddress = new InetSocketAddress(SERVER_IP, SERVERPORT);
socket.connect(socketAddress);
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Upvotes: 2
Reputation: 905
By testing it turns out the connection cannot be established on the tethering device.
But if we reverse the process and open a serversocket on the connected client. And connect to it from the tethering device, it will work.
So reversing the communication is the answer.
Upvotes: 17