PR target/27599
[official-gcc.git] / libjava / classpath / test / java.net / ClientDatagram.java
blob6ec98258e56632b5ccda25e78ef8917da0da0660
1 /* Class to test Datagrams from a client perspective */
3 import java.io.*;
4 import java.net.*;
6 public class ClientDatagram
9 public static void
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");
21 try
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);
32 s.send(spack);
33 s.receive(p);
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++)
40 tmp[i] = buf[i];
42 System.out.print("Data: " + new String(tmp));
44 s.close();
45 System.out.println("PASSED simple datagram test");
47 catch(Exception e)
49 System.out.println("FAILED simple datagram test: " + e);
52 System.out.println("Test 2: Specific host/port binding");
53 try
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());
62 s.close();
63 System.out.println("PASSED specific host/port binding test");
65 catch (Exception e)
67 System.out.println("FAILED specific host/port binding: " + e);
70 System.out.println("Test 3: Socket Options test");
71 try
73 DatagramSocket s = new DatagramSocket();
74 System.out.println("SO_TIMEOUT = " + s.getSoTimeout());
75 System.out.println("Setting SO_TIMEOUT to 170");
76 s.setSoTimeout(170);
77 System.out.println("SO_TIMEOUT = " + s.getSoTimeout());
78 System.out.println("Setting SO_TIMEOUT to 0");
79 s.setSoTimeout(0);
80 System.out.println("SO_TIMEOUT = " + s.getSoTimeout());
81 s.close();
83 catch(Exception e)
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");
90 try
92 // ServerDatagram sd = new ServerDatagram(37900);
93 // sd.run();
95 DatagramSocket s = new DatagramSocket();
96 byte[] sbuf = new byte[65332];
97 DatagramPacket spack = new DatagramPacket(sbuf, sbuf.length,
98 addr, 37900);
100 s.send(spack);
101 s.close();
103 catch (Exception e)
105 System.out.println("FAILED max values test: " + e);
108 System.out.println("Datagram testing complete");