CoDe
CoDe

Reputation: 11156

How to get ip of sender in TCP in Java?

How to get ip of sender in TCP communication in Android.

any one have idea?

Upvotes: 0

Views: 1268

Answers (1)

Don Roby
Don Roby

Reputation: 41135

In order to receive tcp communication in android, you'll need to have done something like this:

    ServerSocket serverSocket = new ServerSocket(port);
    Socket clientSocket = serverSocket.accept();

You can then get the address of the client from it's socket by

    InetAddress clientAddress = clientSocket.getInetAddress()

To get it in text form, you can then use

    String clientAddressString = clientAddress.getHostAddress()

Of course most of these things can throw exceptions, so you'll need to handle them.

Upvotes: 3

Related Questions