Client program – ClientEcho.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import java.net.*;
import java.util.*;
public class ClientEcho
{
public static void main( String args[] ) throws Exception
{
InetAddress add = InetAddress.getByName("snrao");
DatagramSocket dsock = new DatagramSocket( );
String message1 = "This is client calling";
byte arr[] = message1.getBytes( );
DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
dsock.send(dpack); // send the packet
Date sendTime = new Date(); // note the time of sending the message
dsock.receive(dpack); // receive the packet
String message2 = new String(dpack.getData( ));
Date receiveTime = new Date( ); // note the time of receiving the message
System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for " + message2);
}
}
|
Client program – ServerEcho.java