1 /* Class to test Datagrams from a client perspective */
6 public class ClientDatagram
10 main(String[] argv) throws IOException
12 System.out.println("Starting datagram tests");
14 byte[] buf = new byte[2048];
15 DatagramPacket p = new DatagramPacket(buf, buf.length);
16 InetAddress addr = InetAddress.getByName("localhost");
18 /* Execute the daytime UDP service on localhost. You may need to
19 enable this in inetd to make the test work */
20 System.out.println("Test 1: Simple daytime test");
24 DatagramSocket s = new DatagramSocket();
26 System.out.println("Socket bound to " + s.getLocalAddress() +
27 ":" + s.getLocalPort());
29 byte[] sbuf = { 'H', 'I' };
30 DatagramPacket spack = new DatagramPacket(sbuf, sbuf.length, addr, 13);
35 System.out.println("Received " + p.getLength() + " bytes from " +
36 p.getAddress() + ":" + p.getPort());
38 byte[] tmp = new byte[p.getLength()];
39 for (int i = 0; i < p.getLength(); i++)
42 System.out.print("Data: " + new String(tmp));
45 System.out.println("PASSED simple datagram test");
49 System.out.println("FAILED simple datagram test: " + e);
52 System.out.println("Test 2: Specific host/port binding");
55 DatagramSocket s = new DatagramSocket(8765, addr);
56 if (s.getLocalPort() != 8765)
57 throw new IOException("Bound to wrong port: " + s.getLocalPort());
59 if (!s.getLocalAddress().equals(addr))
60 throw new IOException("Bound to wrong host:" + s.getLocalAddress());
63 System.out.println("PASSED specific host/port binding test");
67 System.out.println("FAILED specific host/port binding: " + e);
70 System.out.println("Test 3: Socket Options test");
73 DatagramSocket s = new DatagramSocket();
74 System.out.println("SO_TIMEOUT = " + s.getSoTimeout());
75 System.out.println("Setting SO_TIMEOUT to 170");
77 System.out.println("SO_TIMEOUT = " + s.getSoTimeout());
78 System.out.println("Setting SO_TIMEOUT to 0");
80 System.out.println("SO_TIMEOUT = " + s.getSoTimeout());
85 System.out.println("WARNING: Problem with SO_TIMEOUT test: " + e.getMessage());
86 System.out.println("This is ok on Linux");
89 System.out.println("Test 4: Max values test");
92 // ServerDatagram sd = new ServerDatagram(37900);
95 DatagramSocket s = new DatagramSocket();
96 byte[] sbuf = new byte[65332];
97 DatagramPacket spack = new DatagramPacket(sbuf, sbuf.length,
105 System.out.println("FAILED max values test: " + e);
108 System.out.println("Datagram testing complete");