开发者

How to get ip of sender in TCP in Java?

How to get ip of sender in TCP communicat开发者_如何学Goion in Android.

any one have idea?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜