OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libjava / classpath / test / java.net / ClientDatagram.java
1 /* Class to test Datagrams from a client perspective */
2
3 import java.io.*;
4 import java.net.*;
5
6 public class ClientDatagram
7 {
8
9 public static void
10 main(String[] argv) throws IOException
11 {
12   System.out.println("Starting datagram tests");
13
14   byte[] buf = new byte[2048];
15   DatagramPacket p = new DatagramPacket(buf, buf.length);
16   InetAddress addr = InetAddress.getByName("localhost");
17
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
22     {
23       
24       DatagramSocket s = new DatagramSocket();
25
26       System.out.println("Socket bound to " + s.getLocalAddress() +
27                          ":" + s.getLocalPort());
28
29       byte[] sbuf = { 'H', 'I' };
30       DatagramPacket spack = new DatagramPacket(sbuf, sbuf.length, addr, 13);
31
32       s.send(spack);
33       s.receive(p);
34
35       System.out.println("Received " + p.getLength() + " bytes from " +
36                          p.getAddress() + ":" + p.getPort());
37       
38       byte[] tmp = new byte[p.getLength()];
39       for (int i = 0; i < p.getLength(); i++)
40         tmp[i] = buf[i];
41
42       System.out.print("Data: " + new String(tmp));
43
44       s.close();
45       System.out.println("PASSED simple datagram test");
46     }
47   catch(Exception e)
48     {
49       System.out.println("FAILED simple datagram test: " + e);
50     }
51
52   System.out.println("Test 2: Specific host/port binding");
53   try
54     {
55       DatagramSocket s = new DatagramSocket(8765, addr);
56       if (s.getLocalPort() != 8765) 
57         throw new IOException("Bound to wrong port: " + s.getLocalPort());
58
59       if (!s.getLocalAddress().equals(addr))
60         throw new IOException("Bound to wrong host:" + s.getLocalAddress());
61
62       s.close();
63       System.out.println("PASSED specific host/port binding test");
64     }
65   catch (Exception e)
66     {
67       System.out.println("FAILED specific host/port binding: " + e);
68     }
69  
70   System.out.println("Test 3: Socket Options test");
71   try
72     {
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();
82     }
83   catch(Exception e)
84     {
85       System.out.println("WARNING: Problem with SO_TIMEOUT test: " + e.getMessage());
86       System.out.println("This is ok on Linux");
87     }
88
89   System.out.println("Test 4: Max values test");
90   try
91     {
92 //      ServerDatagram sd = new ServerDatagram(37900);
93 //      sd.run();
94
95       DatagramSocket s = new DatagramSocket();
96       byte[] sbuf = new byte[65332];
97       DatagramPacket spack = new DatagramPacket(sbuf, sbuf.length, 
98                                                 addr, 37900);
99
100       s.send(spack);
101       s.close();
102     }
103   catch (Exception e)
104     {
105       System.out.println("FAILED max values test: " + e);
106     }
107
108   System.out.println("Datagram testing complete");
109 }
110
111 }
112