-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClientTest.java
More file actions
80 lines (75 loc) · 2.94 KB
/
ClientTest.java
File metadata and controls
80 lines (75 loc) · 2.94 KB
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
package client;
import org.junit.Test;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class ClientTest {
// @Test
// public void testConnectToServer() throws IOException {
// Socket socket = new Socket("127.0.0.1", 8999);
// Scanner scanner = new Scanner(System.in);
// try {
// DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
// for (; ; ) {
// System.out.println("请输入:");
// String str = scanner.next();
// dataOutputStream.writeInt(str.getBytes(StandardCharsets.UTF_8).length);
// socket.getOutputStream().write(str.getBytes(StandardCharsets.UTF_8));
// System.out.println("发送完成");
// Thread.sleep(1000);
// }
// } catch (IOException e) {
// throw new RuntimeException(e);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }
//
// public static void main(String[] args) throws IOException {
// Socket socket = new Socket("127.0.0.1", 8800);
// Scanner scanner = new Scanner(System.in);
// try {
// DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
// Thread readThead = new Thread(
// () -> {
// try {
// DataInputStream inputStream = new DataInputStream(socket.getInputStream());
// for (; ; ) {
//
// if (inputStream.available() > 0) {
// int len = inputStream.readInt();
// byte[] data = new byte[len];
// inputStream.read(data);
// System.out.println("返回内容:" + new String(data));
// }
// }
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
//
// }
// );
// readThead.start();
// for (; ; ) {
// Thread.sleep(100);
// System.out.println("请输入:");
//
// String str = scanner.next();
// dataOutputStream.writeInt(str.getBytes(StandardCharsets.UTF_8).length);
// socket.getOutputStream().write(str.getBytes(StandardCharsets.UTF_8));
//
// }
// } catch (IOException e) {
// throw new RuntimeException(e);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }
@Test
public void testString() {
byte[] buf = new byte[10];
String s = new String(buf, StandardCharsets.UTF_8);
System.out.println(s);
}
}