Java基础23 Socket手撸应用服务器,上海大厂Java面试经历

创建方法:

new Socket(“IP地址”,端口号)

注意:一旦创建了Socket对象,就自动连接对方计算机

常用方法:

  • getInputStream() 获得输入流,读取对方发来的数据

  • getOutputStream() 获得输出流,给对方发数据

  • close() 关闭

注意:一旦输入流或输出流关闭,Socket连接会自动关闭。

ServerSocket


服务器端Socket,Socket的子类,用于接受客户端并和客户端通信

创建:

new ServerSocket(端口号)

注意:一旦创建ServerSocket对象,会不断侦听该端口,判断是否有客户端连接

主要方法:

  • Socket accept() 用于获得连接过来的客户端Socket对象

服务器端和客户端的通信


服务器端实现步骤:

  1. 创建ServerSocket对象

  2. 循环调用accept方法获得连接

  3. 调用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();

}

}

客户端实现步骤:

  1. 创建Socket对象

  2. 调用Socket对象的getInputStream来读取数据。

  3. 调用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,

“你好!!!服务器端”);

}

}

Socket实现文件上传


文件的上传的步骤

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IimwXFK3-1611022265375)(Socket.assets/clipboard-1610675065080.png)]

服务器端:

  1. 创建ServerSocket

  2. 调用accept获得客户端Socket

  3. 定义字节数组

  4. 创建文件输出流,获得客户端输入流

  5. 循环读取输入流的字节,写入到文件输出流

客户端:

  1. 创建Socket

  2. 获得socket对象输出流

  3. 创建文件输入流

  4. 循环读取文件输入流字节,写入到输出流

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”);

}

}

Socket实现模拟服务器


在这里插入图片描述

HTML服务器的简单工作原理:

  1. 用户在浏览器输入URL地址

  2. 浏览器发送请求给服务器

  3. 服务器从请求头中解析信息:请求方法、URL等

  4. 从URL中解析到资源名称,就到服务器上查找HTML文件

  5. 服务器发送响应状态给浏览器

  6. 服务器发送响应头给浏览器

  7. 服务器开始发送HTML文件的内容

  8. 浏览器接收到HTML内容渲染出来

/**

  • 模拟服务器

*/

public class MyTomcat {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值