-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClienttcp2.java
More file actions
39 lines (30 loc) · 1002 Bytes
/
Clienttcp2.java
File metadata and controls
39 lines (30 loc) · 1002 Bytes
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
package com.cpucode.java;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Clienttcp2 {
public static void main(String[] args) throws Exception {
System.out.println("客户端 发送数据");
// 1.创建 Socket ( ip , port ) , 确定连接到哪里.
Socket client = new Socket("localhost", 666);
// 通过 Scoket, 获取输出流对象
OutputStream os = client.getOutputStream();
// 3.写出数据.
os.write("你好 cpucode".getBytes());
// ==============解析回写=========================
// 4. 通过 Scoket,获取 输入流对象
InputStream in = client.getInputStream();
//读取数据数据
byte[] b = new byte[100];
int len = in.read(b);
System.out.println(new String(b, 0, len));
// 关闭资源
in.close();
os.close();
client.close();
}
}
/*
客户端 发送数据
你好, 我很好
*/