FileDocCategorySizeDatePackage
ChatterClient.javaAPI DocExample4155Mon Apr 06 18:10:22 BST 1998None

ChatterClient

public class ChatterClient extends Thread

Fields Summary
private DatagramSocket
s
private InetAddress
hostAddress
private byte[]
buf
private DatagramPacket
dp
private int
id
Constructors Summary
public ChatterClient(int identifier)


     
    id = identifier;
    try {
      // Auto-assign port number:
      s = new DatagramSocket();
      hostAddress = 
        InetAddress.getByName("localhost");
    } catch(UnknownHostException e) {
      System.err.println("Cannot find host");
      System.exit(1);
    } catch(SocketException e) {
      System.err.println("Can't open socket");
      e.printStackTrace();
      System.exit(1);
    } 
    System.out.println("ChatterClient starting");
  
Methods Summary
public static voidmain(java.lang.String[] args)

    for(int i = 0; i < 10; i++)
      new ChatterClient(i).start();
  
public voidrun()

    try {
      for(int i = 0; i < 25; i++) {
        String outMessage = "Client #" +
          id + ", message #" + i;
        // Make and send a datagram:
        s.send(Dgram.toDatagram(outMessage,
          hostAddress, 
          ChatterServer.INPORT));
        // Block until it echoes back:
        s.receive(dp);
        // Print out the echoed contents:
        String rcvd = "Client #" + id +
          ", rcvd from " + 
          dp.getAddress() + ", " + 
          dp.getPort() + ": " +
          Dgram.toString(dp);
        System.out.println(rcvd);
      }
    } catch(IOException e) {
      e.printStackTrace();
      System.exit(1);
    }