简体   繁体   中英

How to get the clients ip address from the server?

I have problem where I can't get the clients Ip from the server to store it into a variable then into a dictionary. I've tried to use getAddress() but it's not working it give a message says " can't find symbol" .

try {
        DatagramSocket IN_socket = new DatagramSocket(3000);
        DatagramSocket OUT_socket = new DatagramSocket(8000);
        IN_socket.setSoTimeout(0);
        Inet4Address addrs = DatagramSocket.getAddress() ;
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

Object java.net.DatagramSocket doesn't have a getAddress() method, instead there is getInetAddress() :

public InetAddress getInetAddress()

Returns the address to which this socket is connected. Returns null if the socket is not connected. If the socket was connected prior to being closed, then this method will continue to return the connected address after the socket is closed.

Returns: the address to which this socket is connected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM