开发者

How to get ip of sender in TCP in Java?

开发者 https://www.devze.com 2023-03-17 07:43 出处:网络
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:

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

精彩评论

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