当前位置:网站首页>Mina的长连接和短连接
Mina的长连接和短连接
2022-08-05 05:21:00 【洒家肉山大魔王】
1. 长连接
通信双方长期的保持一个连接状态不断开。
比如:登录QQ,将去连接腾讯服务器,一旦建立连接后,就不断开,除非发生异常,这样的方式就是长连接。
对于长连接比较消耗IO资源。
2. 短连接
通信双方不是保持一个长期的连接状态,一次请求-应答结束后,连接自动断开。
比如:http协议,客户端发起http请求,服务器处理http请求,当服务器处理完成后,返回客户端数据后就断开连接,对于下一次的连接请求需要重新发起。
3. 短连接栗子
还是参考上一节《Mina入门》的栗子,修改Mina服务端的处理器MyServerHandler.java
package com.hl.magic.mina.mina1;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
/**
* 自定义Mina服务端会话适配器
*
* @Author HL
* @Date 2021/10/10 21:06
*/
public class MyServerHandler extends IoHandlerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(MyServerHandler.class);
/**
* 创建会话
*
* @param session 会话对象
*/
@Override
public void sessionCreated(IoSession session) throws Exception {
LOGGER.debug("sessionCreated");
}
/**
* 打开会话
*
* @param session 会话对象
*/
@Override
public void sessionOpened(IoSession session) throws Exception {
LOGGER.debug("sessionOpened");
}
/**
* 关闭会话
*
* @param session 会话对象
*/
@Override
public void sessionClosed(IoSession session) throws Exception {
LOGGER.debug("sessionClosed");
}
/**
* 会话空闲
*
* @param session 会话对象
* @param status 会话状态
*/
@Override
public void sessionIdle(IoSession session, IdleStatus status) {
LOGGER.debug("IDLE: [{}]", session.getIdleCount(status));
}
/**
* 连接异常时
*
* @param session 会话对象
*/
@Override
public void exceptionCaught(IoSession session, Throwable cause) {
LOGGER.debug("exceptionCaught");
}
/**
* 接收消息
*
* @param session 会话对象
* @param message 消息体
*/
@Override
public void messageReceived(IoSession session, Object message) {
String msg = (String) message;
LOGGER.debug("服务端接收到数据: [{}]", msg);
//长连接模式监听,如果要改为短连接,注释下边连接断开模式,在messageSent方法中断开连接
// if (msg.trim().equalsIgnoreCase("quit")) {
// session.closeNow();
// return;
// }
Date date = new Date();
session.write("消息已收到,时间:" + date);
}
/**
* 发生消息
*
* @param session 会话对象
* @param message 消息体
*/
@Override
public void messageSent(IoSession session, Object message) {
LOGGER.debug("messageSent...");
// 短连接方式,发送完消息后自动断开连接
session.closeNow();
}
}
运行Mina服务端程序MyServer.java类

运行CMD,窗口输入telnet 127.0.0.1 54321 与服务端建立连接

然后发送数据 "ww",可以看到服务端收到数据并给出应答后,自动断开连接。

边栏推荐
猜你喜欢

IP packet format (ICMP protocol and ARP protocol)

错误类型:反射。ReflectionException:无法设置属性“xxx”的“类”xxx”与价值“xxx”

What are some things that you only know when you do operation and maintenance?
![[Day6] File system permission management, file special permissions, hidden attributes](/img/ec/7fb3fa671fac8abf389844c0f4fbe7.png)
[Day6] File system permission management, file special permissions, hidden attributes

线上问题排查流程

Remembering my first CCF-A conference paper | After six rejections, my paper is finally accepted, yay!

网络层协议介绍

The problem come from line screening process

Hard Disk Partitioning and Permanent Mounting

Mongodb查询分析器解析
随机推荐
Remembering my first CCF-A conference paper | After six rejections, my paper is finally accepted, yay!
千亿IT运维市场,产品要凭实力说话
The problem come from line screening process
入门文档11 自动添加版本号
User and user group management, file permission management
[Day8] Commands involved in using LVM to expand
markdown编辑器模板
spark source code - task submission process - 5-CoarseGrainedExecutorBackend
spark operator-parallelize operator
VRRP principle and command
ACL 和NAT
spark operator-wholeTextFiles operator
Getting Started 03 Distinguish between development and production environments ("hot update" is performed only in the production environment)
Small example of regular expression--validate email address
The problem of redirecting to the home page when visiting a new page in dsf5.0
King power volume LinkSLA, realize operations engineer is happy fishing
实力卷王LinkSLA,实现运维工程师快乐摸鱼
入门文档05-2 使用return指示当前任务已完成
NAT实验
TCP/IP four-layer model