Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: All
Aug 14, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Use DatagramSocket to See Who's Calling Your Server

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
      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: " +
Behrouz Fallahi
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date