如何用一个Socket服务端去接收一个http请求
发布网友
发布时间:2022-04-22 06:07
我来回答
共1个回答
热心网友
时间:2024-01-20 17:11
Java code
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
ServerSocket serverSocket = null;
int port = 8888;
try
{
serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
} catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
while (!shutdown)
{
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try
{
/**
* 循环到此停止,在端口8888接收到一个HTTP请求才会继续
*/
socket = serverSocket.accept();
input = socket.getInputStream();
output = socket.getOutputStream();
} catch (Exception e)
{
e.printStackTrace();
continue;
}
}