-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketClient.java
More file actions
31 lines (30 loc) · 1.1 KB
/
SocketClient.java
File metadata and controls
31 lines (30 loc) · 1.1 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
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class SocketClient {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
Socket client = new Socket("localhost", 6666);
while (true) {
OutputStream out = client.getOutputStream();
System.out.println("客户端发送数据");
String str = input.next();
out.write(str.getBytes());
if (str.equals("over")) {
System.out.println("客户端已关闭");
out.close();
client.close();
break;
}
//-------------------客户器端读取回复信息----------------------//
InputStream in = client.getInputStream();
//4.读取数据
byte[] b = new byte[1024];
int len = in.read(b);
String info = new String(b, 0, len);
System.out.println("这是从客户端接收的数据:" + info);
}
}
}