-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
34 lines (32 loc) · 1.16 KB
/
Server.java
File metadata and controls
34 lines (32 loc) · 1.16 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
32
33
34
package JavaIO;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Reference
* - https://cloud.tencent.com/developer/article/1545422
*/
public class Server {
public static void main(String[] args) {
byte[] buffer = new byte[1024];
try{
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("服务器已启动并监听8080端口");
while(true) {
System.out.println();
System.out.println("服务器正在等待连接...");
Socket socket = serverSocket.accept();
System.out.println("服务器已接收到连接请求...");
System.out.println();
System.out.println("服务器正在等待数据...");
socket.getInputStream().read(buffer);
System.out.println("服务器已经接收到数据");
System.out.println();
String content = new String(buffer);
System.out.println("接收到的数据:"+ content);
}
} catch(IOException e) {
e.printStackTrace();
}
}
}