Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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
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);
Behrouz Fallahi
 
Comment and Contribute

 

 

 

 

 


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

 

 

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