blob: 53a498b34e95a9bccf075aeac828bd5c90ed0b15 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/* A class to test my client TCP socket implementation */
import java.net.*;
import java.io.*;
public class ClientSocket extends Object
{
public static void
main(String[] argv) throws IOException
{
System.out.println("Starting client stream socket test");
/* Simple connection and read test */
System.out.println("Test 1: Connection to daytime port on local host");
try
{
InetAddress addr = InetAddress.getByName("127.0.0.1");
Socket s = new Socket(addr, 13);
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
for (String str = br.readLine(); ; str = br.readLine())
{
if (str == null)
break;
System.out.println(str);
}
s.close();
System.out.println("PASSED: daytime test");
}
catch(IOException e)
{
System.out.println("FAILED: daytime test: " + e);
}
/* Simple connection refused test */
System.out.println("Test 2: Connection refused test");
try
{
InetAddress addr = InetAddress.getByName("127.0.0.1");
Socket s = new Socket(addr, 47);
s.close();
System.out.print("WARNING: Cannot perform connection refused test");
System.out.println(" because someone is listening on localhost:47");
}
catch(IOException e)
{
System.out.println("PASSED: connection refused test: " + e.getMessage());
}
/* Socket attributes test */
System.out.println("Test 3: Connection attributes");
try
{
Socket s = new Socket("www.netscape.com", 80);
String laddr = s.getLocalAddress().getHostName();
int lport = s.getLocalPort();
String raddr = s.getInetAddress().getHostName();
int rport = s.getPort();
System.out.println("Local Address is: " + laddr);
System.out.println("Local Port is: " + lport);
System.out.println("Remote Address is: " + raddr);
System.out.println("Remote Port is: " + rport);
System.out.println("Socket.toString is: " + s);
if ( (laddr == null) ||
((lport < 0) || (lport > 65535)) ||
(raddr.indexOf("netscape.com") == -1) ||
(rport != 80))
System.out.println("FAILED: connection attribute test");
else
System.out.println("PASSED: connection attribute test");
s.close();
}
catch(IOException e)
{
System.out.println("FAILED: connection attributes test: " + e.getMessage());
}
/* Socket options test */
System.out.println("Test 4: Socket options");
Socket s = new Socket("127.0.0.1", 23);
try
{
// SO_TIMEOUT
System.out.println("SO_TIMEOUT = " + s.getSoTimeout());
System.out.println("Setting SO_TIMEOUT to 142");
s.setSoTimeout(142);
System.out.println("SO_TIMEOUT = " + s.getSoTimeout());
System.out.println("Setting SO_TIMEOUT to 0");
s.setSoTimeout(0);
System.out.println("SO_TIMEOUT = " + s.getSoTimeout());
}
catch (IOException e)
{
System.out.println("WARNING: SO_TIMEOUT problem: " + e.getMessage());
System.out.println("This is ok on Linux");
}
try
{
// Try TCP_NODELAY
System.out.println("TCP_NODELAY = " + s.getTcpNoDelay());
System.out.println("Setting TCP_NODELAY to true");
s.setTcpNoDelay(true);
System.out.println("TCP_NODELAY = " + s.getTcpNoDelay());
System.out.println("Setting TCP_NODELAY to false");
s.setTcpNoDelay(false);
System.out.println("TCP_NODELAY = " + s.getTcpNoDelay());
// Try SO_LINGER
System.out.println("SO_LINGER = " + s.getSoLinger());
System.out.println("Setting SO_LINGER to 100");
s.setSoLinger(true, 100);
System.out.println("SO_LINGER = " + s.getSoLinger());
System.out.println("Setting SO_LINGER to off");
s.setSoLinger(false, 0);
System.out.println("SO_LINGER = " + s.getSoLinger());
System.out.println("PASSED: socket options test");
}
catch(IOException e)
{
System.out.println("FAILED: socket options test: " + e.getMessage());
}
s.close();
/* Simple read/write test */
System.out.println("Test 5: Simple read/write test");
try
{
System.out.println("Downloading the Transmeta homepage");
s = new Socket("www.transmeta.com", 80);
BufferedReader in = new BufferedReader(new
InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(new
OutputStreamWriter(s.getOutputStream()));
out.print("GET /\r\n");
out.flush();
for (String str = in.readLine(); ; str = in.readLine())
{
if (str == null)
break;
System.out.println(str);
}
s.close();
System.out.println("PASSED: simple read/write test");
}
catch(IOException e)
{
System.out.println("FAILED: simple read/write test: " + e.getMessage());
}
/* Connect to our server socket */
System.out.println("Test 6: Connect to ServerSocket");
try
{
s = new Socket("localhost", 9999);
PrintWriter out = new PrintWriter(new
OutputStreamWriter(s.getOutputStream()));
out.println("Hello, there server socket");
out.print("I'm dun");
out.flush();
s.close();
System.out.println("PASSED: connect to server socket");
}
catch(Exception e)
{
System.out.println("FAILED: connect to server socket: " + e);
}
System.out.println("Client stream socket test complete");
}
}
|