JavaSE 进阶(二十二)

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
package ml.guest997;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class TCPClient {
public static void main(String[] args) {
Socket socket = null;
OutputStream outputStream = null;
try {
//发送信息
socket = new Socket(InetAddress.getByName("localhost"), 9999);
outputStream = socket.getOutputStream();
outputStream.write("客户端发送信息...".getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
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
57
58
59
60
61
62
63
package ml.guest997;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//创建连接
serverSocket = new ServerSocket(9999);
//等待客户端请求
accept = serverSocket.accept();
//读取用户信息
inputStream = accept.getInputStream();
//管道流接收和打印
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, len);
}
System.out.println(byteArrayOutputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (accept != null) {
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException 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
package ml.guest997;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class FileClient {
public static void main(String[] args) {
try {
Socket socket = new Socket(InetAddress.getByName("localhost"), 9999);
OutputStream outputStream = socket.getOutputStream();
FileInputStream fileInputStream = new FileInputStream("pc.png");
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
//通知服务器文件已发送完毕
socket.shutdownOutput();
//确定服务端接收完毕,才断开连接。
InputStream inputStream = socket.getInputStream();
byte[] buffer2 = new byte[1024];
int len2;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((len2 = inputStream.read(buffer2)) != -1) {
byteArrayOutputStream.write(buffer2, 0, len2);
}
System.out.println(byteArrayOutputStream);
System.out.println("客户端断开连接...");
byteArrayOutputStream.close();
inputStream.close();
outputStream.close();
fileInputStream.close();
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
package ml.guest997;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class FileServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9999);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("receive.png");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
OutputStream outputStream = accept.getOutputStream();
outputStream.write("服务端接收成功...".getBytes(StandardCharsets.UTF_8));
outputStream.close();
fileOutputStream.close();
inputStream.close();
accept.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

UDP 发送消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package ml.guest997;

import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;

public class UDPSend {
public static void main(String[] args) {
try {
DatagramSocket datagramSocket = new DatagramSocket();
String msg = "发送消息...";
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(StandardCharsets.UTF_8), 0, msg.getBytes(StandardCharsets.UTF_8).length, InetAddress.getByName("127.0.0.1"), 9999);
datagramSocket.send(datagramPacket);
datagramSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package ml.guest997;

import java.io.IOException;
import java.net.*;

public class UDPReceive {
public static void main(String[] args) {
try {
DatagramSocket datagramSocket = new DatagramSocket(9999, InetAddress.getByName("127.0.0.1"));
byte[] buffer = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(buffer, 0, buffer.length);
datagramSocket.receive(datagramPacket);
System.out.println(new String(buffer, 0, datagramPacket.getLength())); //不能直接使用 datagramPacket.getData(),因为未赋值的位数全是0。
} catch (IOException e) {
e.printStackTrace();
}
}
}

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package ml.guest997;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.nio.charset.StandardCharsets;

//发送消息
public class ChatSend implements Runnable {
DatagramSocket datagramSocket;
BufferedReader reader;

private int fromPort;
private int toPort;

public ChatSend(int fromPort, int toPort) {
this.fromPort = fromPort;
this.toPort = toPort;
try {
this.datagramSocket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}

@Override
public void run() {
while (true) {
try {
String s = reader.readLine();
byte[] data = s.getBytes(StandardCharsets.UTF_8);
DatagramPacket datagramPacket = new DatagramPacket(data, 0, data.length, new InetSocketAddress("localhost", this.toPort));
datagramSocket.send(datagramPacket);
if ("bye".equals(s)) {
break;
}
} 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
package ml.guest997;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

//接收消息
public class ChatReceive implements Runnable {
DatagramSocket datagramSocket;

private String from;
private int port;

public ChatReceive(String from, int port) {
this.from = from;
this.port = port;
try {
this.datagramSocket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}

@Override
public void run() {
while (true) {
try {
byte[] cache = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(cache, 0, cache.length);
datagramSocket.receive(datagramPacket);
String receiveData = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
System.out.println(from + ":" + receiveData);
if ("bye".equals(receiveData)) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1
2
3
4
5
6
7
8
package ml.guest997;

public class ChatServer {
public static void main(String[] args) {
new Thread(new ChatSend(6666, 7777)).start();
new Thread(new ChatReceive("客户端", 8888)).start();
}
}
1
2
3
4
5
6
7
8
package ml.guest997;

public class ChatClient {
public static void main(String[] args) {
new Thread(new ChatSend(9999, 8888)).start();
new Thread(new ChatReceive("服务端", 7777)).start();
}
}

下载网络资源

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
package ml.guest997;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Download {
public static void main(String[] args) {
try {
URL url = new URL("https://guest997.ml/wp-content/themes/Sakurairo-2.1.1.1/homepage.mp4");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("homepage.mp4");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}