/
SocketConnectTest.java
165 lines (79 loc) · 3.46 KB
/
SocketConnectTest.java
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
package com.chulm.websocket.test;
import javax.security.cert.CertificateException;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.security.cert.CertificateEncodingException;
import java.util.Base64;
import java.util.UUID;
public class SocketConnectTest {
private final static String charName = "UTF-8";
/**
* @param args
* @throws IOException
* @throws CertificateEncodingException
* @throws CertificateException
* @throws URISyntaxException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, CertificateEncodingException, CertificateException, URISyntaxException, InterruptedException {
/************************************************************************/
// request
Socket sock = new Socket();
sock.connect(new InetSocketAddress("localhost", 8082));
OutputStream os = sock.getOutputStream();
os.write(("GET /ws-handler/info?t=" + System.currentTimeMillis() + " HTTP/1.1" + "\n").getBytes(charName));
os.write(("Connection: KeepAlive\n").getBytes(charName));
os.write("\n\n".getBytes(charName));
os.flush();
String line = "";
String result = "";
// response
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream(), charName));
while ((line = br.readLine()) != null)
result += line + "\n";
System.out.println("result = " + result);
byte[] encode = Base64.getEncoder().encode(toByteArray(UUID.randomUUID()));
String key = new String(encode);
System.out.println("Request Sec-WebSocket-Key = " + key);
// disconnect
os.close();
br.close();
sock.close();
/************************************************************************/
//handshake request
Socket sock2 = new Socket();
sock2.connect(new InetSocketAddress("localhost", 8082));
os = sock2.getOutputStream();
///ws/733/a1f0yayx/websocket
os.write(("GET /ws-handler/111/" + System.currentTimeMillis()+"/websocket" + " HTTP/1.1" + "\n").getBytes("UTF-8"));
os.write(("Connection: Upgrade\n").getBytes("UTF-8"));
os.write(("Sec-WebSocket-Version: 13\n").getBytes("UTF-8"));
os.write(("Sec-WebSocket-Key:" + key + "\n").getBytes("UTF-8"));
os.write(("Upgrade: websocket" + "\n").getBytes("UTF-8"));
os.write(("\n\n").getBytes("UTF-8"));
os.flush();
result = "";
//handshake response
BufferedInputStream bis = new BufferedInputStream(sock2.getInputStream());
while (true) {
bis.mark(9999999);
int offset = bis.read();
byte[] readByte = new byte[offset];
bis.reset();
bis.read(readByte);
String data = new String(readByte, "UTF-8");
System.out.println("data: " + data);
}
}
private static byte[] toByteArray(UUID uuid) {
byte[] byteArray = new byte[(Long.SIZE / Byte.SIZE) * 2];
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
LongBuffer longBuffer = buffer.asLongBuffer();
longBuffer.put(new long[]{uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()});
return byteArray;
}
}