Client estimates the speed of the network connection to the server : Server « Network Protocol « Java






Client estimates the speed of the network connection to the server

  
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Calendar;

public class UdpEchoClient {
  static final String testString = "Greeks bearing gifts";

  public static void main(String[] args) {
    InetAddress address;
    try {
      address = InetAddress.getByName(args[0]);
    } catch (UnknownHostException host) {
      System.out.println(host);
      return;
    }
    DatagramPacket pack = new DatagramPacket(testString.getBytes(),
        testString.length(), address, 7);
    DatagramPacket incoming = new DatagramPacket(new byte[256], 256);
    DatagramSocket sock = null;
    try {
      Calendar start, end;
      sock = new DatagramSocket();
      start = Calendar.getInstance();
      sock.send(pack);
      sock.setSoTimeout(5000);
      sock.receive(incoming);
      end = Calendar.getInstance();
      String reply = new String(incoming.getData());
      reply = reply.substring(0, testString.length());
      if (reply.equals(testString)) {
        System.out.println("Success");
        System.out.println("Time = "
            + (end.getTime().getTime() - start.getTime().getTime())
            + "mS");
      } else
        System.out.println("Reply data did not match");
    } catch (SocketException socke) {
      System.out.println(socke);
    } catch (IOException ioe) {
      System.out.println(ioe);
    } finally {
      sock.close();
    }
  }
}
           
         
    
  








Related examples in the same category

1.A generic framework for a flexible, multi-threaded server
2.Server allows connections on socket 6123Server allows connections on socket 6123
3.This server displays messages to a single clientThis server displays messages to a single client
4.The client can specify information to control the output of a serverThe client can specify information to control the output of a server
5.A server can use specialized streams to deliver typed dataA server can use specialized streams to deliver typed data
6.Serve entire objects using ObjectOutputStreamServe entire objects using ObjectOutputStream
7.A multithreaded serverA multithreaded server
8.Base class to build multithreaded servers easily
9.Manage a pool of threads for clients
10.This server retrieves the time using the RFC867 protocol.This server retrieves the time using the RFC867 protocol.
11.Quote Server
12.Logging Server based on SocketServer
13.Client and Server Demo
14.Reflector
15.Simple Http Server