Echo Server UDP Example Java

Echo Server UDP Example Java


Code Snippets

Following code snippets are very useful for the programmer in coding.

1. Finding the IP Address of the system where the program is running

InetAddress add = InetAddress.getLocalHost();
System.out.println ("The system address: " + add.getHostAddress());

Getting URN from URL (name to address)

InetAddress add = InetAddress.getByName("125.252.226.27");
System.out.println ("My system name: " + add.getHostName());

2. Getting URL from URN (address to name)

InetAddress add = InetAddress.getByName("www.rediff.com");
System.out.println ("Yahoo address in number format : " + add.getHostAddress());

3. To find who is accessing the system

a) When TCP/IP protocol is used

ServerSocket sersock = new ServerSocket(4000);
Socket sock = sersock.accept();
InetAddress add = sock.getInetAddress();
System.out.println(add.getHostAddress() + " on port number " + sock.getPort());

b) When UDP protocol is used

DatagramSocket dsock = null;
DatagramPacket dpack = null;
dsock.receive(dpack);
InetAddress add = dpack.getAddress();
System.out.println(add.getHostAddress() + " on port number " + dpack.getPort());

Java comes with many classes grouped as categories. A group gives common properties. A few groups are given below.

1. Wrapper classes
2. Listeners
3. Adapters
4. Layout managers
5. Byte streams
6. Character streams
7. Print streams
8. Filter streams
9. Wrapper streams
10. Components
11. Containers
12. Thread groups
13. Exceptions

Leave a Reply

Your email address will not be published. Required fields are marked *