百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
X

XAndroidSocket

> 编程语言
开源

Socket封装,支持TCP/UDP客户端和服务端,支持自定义粘包处理、验证处理、解析处理。

193 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Socket封装,支持TCP/UDP客户端和服务端,支持自定义粘包处理、验证处理、解析处理。

XAndroidSocket

Socket封装,支持TCP/UDP客户端和服务端,支持自定义粘包处理、验证处理、解析处理。 ##使用 详见目录TCP/UDP使用demo 下的例子,使用简单。 其中只有TCP才支持支持粘包处理、验证处理、解析处理。

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}

Step 2. Add the dependency

dependencies {
        compile 'com.github.Blankeer:XAndroidSocket:1.0.0'
}

##粘包处理 提供的粘包处理有

  • 不处理(默认)
  • 首尾特殊字符处理
  • 固定长度处理
  • 动态长度处理

支持自定义粘包处理,只需要实现AbsStickPackageHelper接口,

java
/**
 * 接受消息,粘包处理的helper,通过inputstream,返回最终的数据,需手动处理粘包,返回的byte[]是我们预期的完整数据
 * note:这个方法会反复调用,直到解析到一条完整的数据。该方法是同步的,尽量不要做耗时操作,否则会阻塞读取数据
 */
public interface AbsStickPackageHelper {
    byte[] execute(InputStream is);
}

把接收消息的InputStream给你,你返回一个完整包的byte[]给我。 ##验证处理 提供的验证处理是 不处理,也是默认的。 自定义验证处理需要实现AbsValidationHelper:

java
public interface AbsValidationHelper {
    boolean execute(byte[] msg);
}

把完整的数据包给你,你需要返回是否验证通过,一般的自定义协议里都会有MD5验证,可以在这里验证。 ##解析处理 提供的解析处理是 不处理,也是默认的。 自定义解析处理需要实现AbsDecodeHelper

java
/**
 * 解析消息的处理
 */
public interface AbsDecodeHelper {
    /**
     *
     * @param data  完整的数据包
     * @param targetInfo    对方的信息(ip/port)
     * @param tcpConnConfig    tcp连接配置,可自定义
     * @return
     */
    byte[][] execute(byte[] data, TargetInfo targetInfo, TcpConnConfig tcpConnConfig);
}

设计思路:一般自定义协议会设计好多个字段组成,比如:dataLen+data+type+md5,数据长度+数据+类型+MD5,解析处理就是把这4个字段解析出来,返回byte[4][],便于后续处理。

Log

日志开关:XSocketLog.debug(true)

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Java

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言