详解Java的TCP/IP编程学习--基于定界符的成帧
作者:毕九生 时间:2023-11-23 03:02:32
一、定界符成帧
Framer接口
package framer;
import java.io.IOException;
import java.io.OutputStream;
public interface Framer {
/**
* 添加成帧信息并将指定消息输出到指定流
* @param message
* @param out
* @throws IOException
*/
void frameMsg(byte[] message, OutputStream out) throws IOException;
/**
* 扫描指定的流,从中抽取下一条信息
* @return
* @throws IOException
*/
byte[] nextMsg() throws IOException;
}
基于定界符的成帧
package framer;
import java.io.*;
/**
* @ClassName DelimFramer
* @Description TODO
* @Author Cays
* @Date 2019/3/16 22:04
* @Version 1.0
**/
public class DelimFramer implements Framer {
//输入流
private InputStream in;
//定界符为换行符
private static final byte DELIMITER='\n';
public DelimFramer(InputStream in) {
this.in = in;
}
@Override
public void frameMsg(byte[] message, OutputStream out) throws IOException {
//检测信息中是否包含换行符
for (byte b:message){
if (b==DELIMITER){
throw new IOException("Message contaions delimiter");
}
}
//将成帧的信息输出到流中
out.write(message);
//添加定界符
out.write(DELIMITER);
out.flush();
}
@Override
public byte[] nextMsg() throws IOException {
ByteArrayOutputStream messageBuffer=new ByteArrayOutputStream();
int nextByte;
//读取流中的每一个字节,直到遇到定界符
while ((nextByte=in.read())!=DELIMITER){
if (nextByte==-1){
//如果遇到定界符之前就到了流的终点
if (messageBuffer.size()==0){
return null;
}else {
//如果存入缓存区之前有数据,抛出异常
throw new EOFException("Non-empty message without delimiter");
}
}
//将无定界符的字节写入消息缓存区
messageBuffer.write(nextByte);
}
//将消息缓存区的内容以字节数组的形式返回
return messageBuffer.toByteArray();
}
}
以上所述是小编给大家介绍的Java的TCP/IP编程学习--基于定界符的成帧详解整合,希望对大家有所帮助.
来源:https://blog.csdn.net/qq_39400984/article/details/88697835
标签:Java,TCP/IP,成帧
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现输出数字三角形实例代码
2023-08-25 02:09:51
![](https://img.aspxhome.com/file/2023/4/58304_0s.png)
android TextView实现跑马灯效果
2023-07-27 16:35:31
举例说明Java设计模式编程中ISP接口隔离原则的使用
2023-11-20 11:07:40
![](https://img.aspxhome.com/file/2023/7/59517_0s.jpg)
剖析Java中阻塞队列的实现原理及应用场景
2023-09-01 17:33:07
Android消息机制Handler的工作过程详解
2023-07-31 13:49:03
![](https://img.aspxhome.com/file/2023/5/85965_0s.jpg)
Spring JDK动态 代理实现过程详解
2023-11-16 19:42:15
![](https://img.aspxhome.com/file/2023/2/59392_0s.png)
Java中的Struts2框架拦截 器之实例代码
2023-06-21 19:04:03
![](https://img.aspxhome.com/file/2023/8/78238_0s.jpg)
SpringBoot之Helloword 快速搭建一个web项目(图文)
2023-08-23 17:36:21
![](https://img.aspxhome.com/file/2023/8/58448_0s.jpg)
Android token过期刷新处理的方法示例
2023-11-23 14:11:40
Flutter加载图片的多样玩法汇总
2023-08-24 09:48:22
![](https://img.aspxhome.com/file/2023/1/108291_0s.png)
AndroidStudio接入Unity工程并实现相互跳转的示例代码
2023-08-06 23:34:51
![](https://img.aspxhome.com/file/2023/6/84736_0s.gif)
IDEA部署JavaWeb项目到Tomcat服务器的方法
2023-11-02 23:21:22
深入剖析Android消息机制原理
2023-09-30 01:57:11
![](https://img.aspxhome.com/file/2023/6/85966_0s.jpg)
c# 获取CookieContainer的所有cookies函数代码
2023-06-17 23:11:30
Eureka源码阅读之环境搭建及工程结构
2023-07-26 01:02:36
![](https://img.aspxhome.com/file/2023/6/57946_0s.png)
Java web访问localhost报404错误问题的解决方法
2023-07-27 05:28:55
![](https://img.aspxhome.com/file/2023/3/57913_0s.png)
Java @Deprecated注解的作用及传递性
2023-08-11 12:55:05
![](https://img.aspxhome.com/file/2023/7/58097_0s.png)
Spring Boot整合流控组件Sentinel的场景分析
2023-06-22 19:27:53
![](https://img.aspxhome.com/file/2023/0/57600_0s.jpg)
java 动态生成bean的案例
2023-08-09 02:20:05
C++实现的O(n)复杂度内查找第K大数算法示例
2023-06-30 15:51:13