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
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPClientDemo1 {

public static void main(String[] args) throws UnknownHostException {
//要连接服务器需要知道服务器的ip和port
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = null;
OutputStream os = null;

//需要创建套接字用来连接
try {
socket = new Socket(ip, port);
//发送信息的IO流
os = socket.getOutputStream();
os.write("hello world!".getBytes());


} catch (IOException e) {
e.printStackTrace();
}finally {
if(os != null){
try {
os.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
64
65
66
67
68
69
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServerDemo1 {
public static void main(String[] args){
//提升一下作用域
ServerSocket serverSocket = null;
Socket accept = null;
InputStream is = null;
ByteArrayOutputStream byteArrayOutputStream = null;

//需要提供套接字(插座)给客户端
try {
serverSocket = new ServerSocket(9999);
//等待客户端连接过来,下面的accept就是和客户端的socket配对的套接字

//多次接收
while(true) {
accept = serverSocket.accept();
//接收信息
is = accept.getInputStream();
//管道流
byteArrayOutputStream = new ByteArrayOutputStream(); //该流可以将信息写入到内存
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}

System.out.println(byteArrayOutputStream.toString());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流
if(byteArrayOutputStream != null){
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.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();
}
}
}
}
}

UDP消息发送

客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.IOException;
import java.net.*;

public class UDPClientDemo1 {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket();

//包的信息如下
String msg = "hello world!";
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 9000;

DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, ip, port);

datagramSocket.send(packet);
}
}

服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPServerDemo1 {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(9000);

//接收包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

datagramSocket.receive(packet);

System.out.println(new String(packet.getData()));
}
}

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;


public class UDPClientDemo2 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();

//使用控制台读取数据
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

while (true) {
String dataString = reader.readLine();
byte[] data = dataString.getBytes();
DatagramPacket packet = new DatagramPacket(data, 0, data.length, new InetSocketAddress("127.0.0.1", 8888));

socket.send(packet);
if(dataString.equals("bye")){
break;
}

}
socket.close();
}
}

服务端

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
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPServerDemo2 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);

while (true) {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);

//对读取到的数据进行判断方便结束
String receiveData = new String(packet.getData(), 0, packet.getLength());
//假如使用packet.getData().length,那么获得的长度会是1024

System.out.println(receiveData);

if(receiveData.equals("bye")){
break;
}
}
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
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLDemo {
public static void main(String[] args) throws IOException {
URL url = new URL("https://m701.music.126.net/20201013155815/4716d8336da1c8f4c84ef7db472c593b/jdyyaac/0f0c/5509/5309/8fd58bdf65bc94fd7e89dc7dbd4df522.m4a");

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

InputStream inputStream = urlConnection.getInputStream();

FileOutputStream fileOutputStream = new FileOutputStream("op.m4a");
byte[] buffer = new byte[1024];
int len;
while((len = inputStream.read(buffer)) != -1){
fileOutputStream.write(buffer, 0, len);
}

//close stream
fileOutputStream.close();
inputStream.close();
urlConnection.disconnect();
}
}