创建方法:
new Socket(“IP地址”,端口号)
注意:一旦创建了Socket对象,就自动连接对方计算机
常用方法:
-
getInputStream() 获得输入流,读取对方发来的数据
-
getOutputStream() 获得输出流,给对方发数据
-
close() 关闭
注意:一旦输入流或输出流关闭,Socket连接会自动关闭。
服务器端Socket,Socket的子类,用于接受客户端并和客户端通信
创建:
new ServerSocket(端口号)
注意:一旦创建ServerSocket对象,会不断侦听该端口,判断是否有客户端连接
主要方法:
- Socket accept() 用于获得连接过来的客户端Socket对象
服务器端实现步骤:
-
创建ServerSocket对象
-
循环调用accept方法获得连接
-
调用Socket对象的IO流来读取、发送数据。
public class Server {
public static final int PORT = 8888;
public void start(){
System.out.println(“启动服务器。。。”);
//创建ServerSocket对象
try {
ServerSocket server = new ServerSocket(PORT);
//循环获得客户端连接
while(true){
Socket client = server.accept();
System.out.println(client.getInetAddress()+“连接了”);
//获得客户端的输入流和输出流
try(DataInputStream dis = new DataInputStream(client.getInputStream());
DataOutputStream dos = new DataOutputStream(client.getOutputStream())){
//读取客户端的消息
System.out.println(“客户端”+client.getInetAddress()+“说:”+dis.readUTF());
//给客户端发消息
dos.writeUTF(“我是服务器端,客户端你好啊~~~~~~~~~~~~~~~~”);
}catch(IOException ex){
ex.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server().start();
}
}
客户端实现步骤:
-
创建Socket对象
-
调用Socket对象的getInputStream来读取数据。
-
调用Socket对象的getOutputStream来发送数据。
public class Client {
public void sendMessage(String ip,int port,String msg){
//创建Socket对象,连接服务器端
try {
Socket socket = new Socket(ip,port);
//获得输出流和输入流
try(DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
DataInputStream dis = new DataInputStream(socket.g
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
etInputStream())){
//发送数据给服务器端
dos.writeUTF(msg);
//读取服务器端的消息
System.out.println(“服务器端说:”+dis.readUTF());
}catch(Exception ex){
ex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Client().sendMessage(“192.168.53.5”, 8888,
“你好!!!服务器端”);
}
}
文件的上传的步骤
服务器端:
-
创建ServerSocket
-
调用accept获得客户端Socket
-
定义字节数组
-
创建文件输出流,获得客户端输入流
-
循环读取输入流的字节,写入到文件输出流
客户端:
-
创建Socket
-
获得socket对象输出流
-
创建文件输入流
-
循环读取文件输入流字节,写入到输出流
public class FileServer {
public static final int PORT = 8888;
public static final String DIR = “C:\upload\”;
public void start(){
System.out.println(“start…”);
//创建服务器端对象
try (ServerSocket server = new ServerSocket(PORT)😉{
//调用accept接受客户端连接
while(true){
Socket socket = server.accept();
//创建文件输出流和网络输入流
try(ObjectInputStream in = new ObjectInputStream(
socket.getInputStream());
//读取客户端发来的文件名,创建文件输出流
OutputStream out =
new FileOutputStream(DIR+in.readUTF())😉{
//从网络中读取数据,写入到本地磁盘
int len = 0;
byte[] buffer = new byte[1024];
while((len = in.read(buffer)) != -1){
out.write(buffer, 0, len);
}
System.out.println(“服务器保存完毕”);
}catch(IOException ex){
ex.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new FileServer().start();
}
}
public class FileClient {
/**
- 发送文件
*/
public void sendFile(String ip,int port,String path){
File file = new File(path);
//创建连接,创建文件输入流,网络输出流
try(Socket socket = new Socket(ip,port);
InputStream in = new FileInputStream(path);
ObjectOutputStream out = new ObjectOutputStream(
socket.getOutputStream())){
//先发送文件名给服务器
out.writeUTF(file.getName());
out.flush();
//读取本地文件数据,写入到网络输出流中
int len = 0;
byte[] buffer = new byte[1024];
while((len = in.read(buffer)) != -1){
out.write(buffer, 0, len);
}
System.out.println(“客户端发送完毕”);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new FileClient().sendFile(“127.0.0.1”, 8888,
“D:\java_code\Test10.java”);
}
}
HTML服务器的简单工作原理:
-
用户在浏览器输入URL地址
-
浏览器发送请求给服务器
-
服务器从请求头中解析信息:请求方法、URL等
-
从URL中解析到资源名称,就到服务器上查找HTML文件
-
服务器发送响应状态给浏览器
-
服务器发送响应头给浏览器
-
服务器开始发送HTML文件的内容
-
浏览器接收到HTML内容渲染出来
/**
- 模拟服务器
*/
public class MyTomcat {