You can use java.net.DatagramSocket class to identify a remote machine that is accessing your server. The class java.net.DatagramSocket represents a socket for sending and receiving datagram packets. A datagram socket is the sending or receiving point for a connectionless packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Every packet will have the address and port from which it was sent. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.
In Java, a datagram packet is represented by java.net.DatagramPacket. The following demonstrates how to
use DatagramSocket to identify a remote machine calling your server:
DatagramPacket aPacket = null;
// get the next datagram packet
aDatagramSocket.receive (aPacket);
/*
aDatagramSocket is an instance of java.net.DatagramSocket that
you've
bound to a port on the local machine
*/
String remoteIp = aPacket.getAddress().getHostAddress();
String port = aPacket.getPort();
System.out.println ("Packet received from IP: " + remoteIP + " at port: " +
port);