编写Java代码编写一个TCP Socket的客户端和服务器端程序,实现如下功能: (1) 客户端程序向服务器端程序发出问候信息,内容自定 (2) 服务器端接收信息并显示信息,同时向客户端回送信息,回送内容自定 (3) 分别在DOS命令提示符界面中运行服务器和客户端程序,查看实验结果 (4) 在DOS界面中运行服务器程序,使用浏览器访问该服务器,观察服务器端获得的信息,查看浏览器中“开发者模式”network标签中显示的客户端请求信息,对比二者的内容,并予以解释说明。
时间: 2023-05-28 11:02:10 浏览: 115
服务器端代码:
```java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
//创建ServerSocket对象,监听端口号为8888
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待连接...");
//监听客户端连接请求
Socket socket = serverSocket.accept();
//获取输入流,接收客户端发送的消息
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String msgFromClient = bufferedReader.readLine();
System.out.println("客户端发来问候:" + msgFromClient);
//获取输出流,向客户端发送消息
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
String msgToClient = "你好,客户端";
bufferedWriter.write(msgToClient);
bufferedWriter.newLine();
bufferedWriter.flush();
System.out.println("已向客户端发送消息:" + msgToClient);
//关闭连接
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
客户端代码:
```java
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
//创建Socket对象,指定要连接的服务器和端口号
Socket socket = new Socket("127.0.0.1", 8888);
//获取输出流,向服务器发送消息
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
String msgToServer = "你好,服务器";
bufferedWriter.write(msgToServer);
bufferedWriter.newLine();
bufferedWriter.flush();
System.out.println("已向服务器发送消息:" + msgToServer);
//获取输入流,接收服务器发送的消息
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String msgFromServer = bufferedReader.readLine();
System.out.println("服务器回复消息:" + msgFromServer);
//关闭连接
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
运行结果:
在DOS命令提示符中运行服务器和客户端程序,先运行服务器端程序,等待客户端连接:
```
服务器已启动,等待连接...
```
再运行客户端程序,向服务器发送消息,并接收服务器回送的消息:
```
已向服务器发送消息:你好,服务器
服务器回复消息:你好,客户端
```
在DOS界面中运行服务器程序,使用浏览器访问该服务器,观察服务器端获得的信息:
```
GET / HTTP/1.1
Host: 127.0.0.1:8888
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: Idea-384168b9=07d53916-bcb2-42f3-b3dd-f3f7a13a62fa; Webstorm-f9d816bf=60ef5356-392e-4988-a895-bb1f8877e812
```
对比浏览器中“开发者模式”network标签中显示的客户端请求信息:
```
GET / HTTP/1.1
Host: 127.0.0.1:8888
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="92"
sec-ch-ua-mobile: ?0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: Idea-384168b9=07d53916-bcb2-42f3-b3dd-f3f7a13a62fa; Webstorm-f9d816bf=60ef5356-392e-4988-a895-bb1f8877e812
```
可以看到,浏览器发送的请求头信息比较复杂,其中包括了一些浏览器自身的需要的信息,而客户端程序只发送了一条简单的消息。此外,浏览器发送的请求头中包含了Cookie信息,可以用来辨认用户身份等信息。
阅读全文