网络编程

TCP网络编程

使用TCP连接进行通信的客户端和服务端

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
@Test  // 客户端
public void client() {
// 1.创建Socket对象,指明服务器端的IP和端口号
Socket socket = null;
OutputStream os = null;
try {
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 9999);
// 2.创建输出流
os = socket.getOutputStream();
// 3.向外写数据
os.write("hello? hello? hello?".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
// 4.关闭输入流
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}

try {
if (socket != null) {
// 5.关闭Socket
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
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
@Test  // 服务端
public void server() {
Socket socket = null;
InputStream is = null;
ByteOutputStream byteOutputStream = null;
ServerSocket serverSocket = null;
try {
// 1.创建服务器端的ServerSocket,指明端口号
serverSocket = new ServerSocket(9999);
// 2.接受来自客户端的Socket
socket = serverSocket.accept();
// 3.创建输入流
is = socket.getInputStream();
byteOutputStream = new ByteOutputStream();
byte[] buffer = new byte[5];
int len;
// 4.读取数据
while ((len = is.read(buffer)) != -1) {
// 将buffer数组中的数据写到ByteOutputStream类的一个数组中
byteOutputStream.write(buffer, 0, len);
}
System.out.println("收到消息,来自:"+socket.getInetAddress());
System.out.println(byteOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {// 5.关闭流和socket
if (byteOutputStream != null) {
try {
byteOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

使用TCP实现文件传输

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
  @Test
public void client() throws IOException {
// 创建socket并绑定IP和端口号
Socket socket = new Socket("127.0.0.1",9999);
// 创建输出流,发送数据
OutputStream outputStream = socket.getOutputStream();
// 创建文件输入流,外部读取文件内容
FileInputStream fi = new FileInputStream(new File("../java_08/pic2.png"));
byte[] buffer = new byte[20];
int len;
while ((len = fi.read(buffer))!=-1) {
outputStream.write(buffer,0,len);
}
// 关闭数据输出
socket.shutdownOutput();

// 客户端接收服务端的反馈信息
InputStream inputStream = socket.getInputStream();
ByteOutputStream byteOutputStream = new ByteOutputStream();
byte[] buffer1 = new byte[20];
int len1;
while((len=inputStream.read(buffer1))!=-1){
byteOutputStream.write(buffer1,0,len);
}
System.out.println(byteOutputStream.toString());
byteOutputStream.close();
inputStream.close();

outputStream.close();
fi.close();
socket.close();
}

@Test
public void server() throws IOException {
// 创建服务端ServerSocket并绑定端口
ServerSocket serverSocket = new ServerSocket(9999);
// 接收客户端socket
Socket socket = serverSocket.accept();
// 创建输入流,接收数据
InputStream inputStream = socket.getInputStream();
// 创建文件输出流,用于保存接收到的文件
FileOutputStream fo = new FileOutputStream(new File("rec1.png"));
byte[] buffer = new byte[20];
int len;
while((len = inputStream.read(buffer))!=-1){
fo.write(buffer,0,len);
}

// 服务器反馈消息给客户端
OutputStream outputStream = socket.getOutputStream();
outputStream.write("图片已接收到!".getBytes());
outputStream.close();

fo.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}

UDP网络编程

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
@Test
public void sender() throws IOException {
// 1.创建套接字
DatagramSocket socket = new DatagramSocket();
InetAddress inet = InetAddress.getByName("127.0.0.1");
String str = "哈哈哈哈哈哈哈~";
byte[] data = str.getBytes();
// 2.初始化数据包,绑定IP,和端口号,确定数据的字节长度
DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9999);
// 3.发送数据包
socket.send(packet);
// 4.关闭Socket
socket.close();
}

@Test
public void receiver() throws IOException {
// 1.创建套接字
DatagramSocket socket = new DatagramSocket(9999);
// 2.创建用于接收数据的字节数组
byte[] buffer = new byte[1024];
// 3.创建数据包
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
// 4.接收数据
socket.receive(packet);
// 5.将接收的数据打印到控制台
System.out.println(new String(packet.getData(),0,packet.getLength()));
// 6.关闭socket
socket.close();
}

URL编程

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
@Test
public void test() {
HttpURLConnection con = null;
InputStream is = null;
FileOutputStream fo = null;
try {
// 创建URL对象
URL url = new URL("http://localhost:8888/index.html");
// 创建连接对象
con = (HttpURLConnection) url.openConnection();
// 创建连接
con.connect();
// 创建输入流,用于读取数据
is = con.getInputStream();
// 创建文件输出流,将读取的数据写到本地
fo = new FileOutputStream(new File("index.html"));
byte[] buffer = new byte[20];
int len;
while ((len = is.read(buffer)) != -1) {
fo.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
if (fo != null) {
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}

if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}

}
}