Search code examples
androidsocketsserversocket

android Handle multiple clients connected to a server using sockets


I have an Android app that needs to let multiple socket communication. here is the basic work around:

My application( Say Server) is a service which will run as a sever socket. other application can connect to it on predefined port. There can be a multiple connection say client 1, client 2.

So when client 1 send hello, server will process this and should send reply back to client 1.

Currently I am able to connect server for client 1 and client 2. I have created input , output buffer for same. But I am not sure how server will detect from which it has received "hello".

I was referring this example , but still not clear how to use that solution:

How can I handle multiple clients connected to a server using sockets?


Solution

  • First I tried to check thread ID whenever A new connection is established, but it was same as it is Server Id. Whenever the request received from a specific thread it has a ID. you will get it by

    Thread.currentThread().getId();

    This is unique for each client. So I save this and use this to detect to which thread I should reply back.

    Thanks all for your support.