Try to search your question here, if you can't find : Ask Any Question Now ?

Cannot establish Soket Connection when more devices are connected

HomeCategory: stackoverflowCannot establish Soket Connection when more devices are connected
Avatarcraig asked 5 months ago

I have 6 devices running my app on local network. Each devices knows the IP address and port address of all other devices. Each device send some data to all these 6 devices simultaneously using AsyncTask as shown below

public class Client extends AsyncTask<Void, Void, Void> {
...
...
protected Void doInBackground(Void... arg0) {
    Socket socket = null;

    try {
        socket = new Socket();
        socket.connect(new InetSocketAddress(dstAddress, dstPort), 4000);
        OutputStream out = socket.getOutputStream();
        PrintStream printStream = new PrintStream(out);

        if(sendAsString)
            printStream.println(stringData);
        else
            printStream.println(dataToSend);
        out.flush();
    }
}

In main class i use Client to send to those 6 devices using loop where ip and port are array with ip and port address of sending device

for(i=0; i<6; i++){
    Client c = new Client(ip[i], port[i]);
    c.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

In server class i accept this message as shown below

private class SocketServerThread extends Thread {

    @Override
    public void run() {
        try {
            serverSocket = new ServerSocket(socketServerPORT);

            while (true) {
                Socket socket = serverSocket.accept();
                BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                response = input.readLine();
                Log.d(TAG, "doInBackground: reading input from client " + response);
            }
        }
    }
}

This code above work fine for 4 devices for communication between all devices. when the number of device is 5 communication between 2 device is blocked and the client connect line show this error.

failed to connect to /192.168.1.153 (port 8474) after 4000ms: isConnected failed: EHOSTUNREACH (No route to host)

when device count is more than 4 then connection between these devices are hard. these devices cannot communicate each other. all device showing same error connection failed

First i used same port for all the communication but later i assigned each device different port address but still the problem continues.

I don’t know if it is problem with AsyncTask or with socket communication. Is there any better way i can communicate with multiple device over WIFI on local network?

1 Answers
Best Answer
AvatarAmit answered 5 months ago
Your Answer

9 + 7 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com