package com.myapp;
import android.content.ContextWrapper;
import android.content.Intent;
import android.util.Log;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Timer;
import java.util.TimerTask;
/*数据解析帮助类*/
class DataParserHelper extends Thread{
static final String TAG = "DataParserHelper";
/*解析命令*/
int getCmd(byte []data) {
if(data.length < 4) {
return ConstantData.ERROR;
}
byte[] tempArray = new byte[4];
System.arraycopy(data, 0, tempArray, 0, 4);
int cmd = ByteAndInt.byteArray2Int(tempArray);
return cmd;
}
/*解析类型*/
int getType(byte []data) {
if(data.length - 4 < 4) {
return ConstantData.ERROR;
}
byte[] tempArray = new byte[4];
System.arraycopy(data, 4, tempArray, 0, 4);
int type = ByteAndInt.byteArray2Int(tempArray);
return type;
}
/*解析整形返回结果*/
int getIntResult(byte []data) {
if(data.length - 8 < 4) {
return ConstantData.ERROR;
}
byte[] tempArray = new byte[4];
System.arraycopy(data, 8, tempArray, 0, 4);
int result = ByteAndInt.byteArray2Int(tempArray);
return result;
}
/*解析字串内容*/
String getContent(byte []data) {
if(data.length - 8 < 4) {
return null;
}
byte[] tempArray = new byte[4];
System.arraycopy(data, 8, tempArray, 0, 4);
int len = ByteAndInt.byteArray2Int(tempArray);
if(data.length - 12 < len) {
return null;
}
tempArray = new byte[len];
System.arraycopy(data, 12, tempArray, 0, len);
try {
return new String(tempArray, 0, len, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
/*解析用户名*/
String getUserName(String userInfo) {
int userNameIndex = userInfo.indexOf("USER:");
int userNameEndIndex = userInfo.indexOf("$", userNameIndex + 5);
if(userNameIndex == -1 || userNameEndIndex == -1) {
return null;
}else{
String userName = userInfo.subSequence(userNameIndex + 5, userNameEndIndex).toString();
return userName;
}
}
/*解析密码*/
String getPwd(String userInfo) {
int pwdIndex = userInfo.indexOf("PWD:");
int pwdEndIndex = userInfo.indexOf("$", pwdIndex + 4);
if(pwdIndex == -1 || pwdEndIndex ==-1) {
return null;
}else{
String pwd = userInfo.subSequence(pwdIndex + 4, pwdEndIndex).toString();
return pwd;
}
}
/*解析地址*/
String getAddr(String userInfo) {
int pwdIndex = userInfo.indexOf("ADDR:");
int pwdEndIndex = userInfo.indexOf("$", pwdIndex + 5);
if(pwdIndex == -1 || pwdEndIndex ==-1) {
return null;
}else{
String pwd = userInfo.subSequence(pwdIndex + 5, pwdEndIndex).toString();
return pwd;
}
}
/*解析用户名*/
String getMsgContent(String conent) {
int userNameEndIndex = conent.indexOf("$", 0);
if(userNameEndIndex == -1) {
return null;
}else{
String msgContent = conent.subSequence(userNameEndIndex + 1, conent.length()).toString();
return msgContent;
}
}
/*发送整形结果值*/
synchronized int sendData(Socket socket, int cmd, int type, int resultCode) {
int errCode = -1;
if(socket == null) {
return errCode;
}
try {
byte []buff = new byte[12];
System.arraycopy(ByteAndInt.int2ByteArray(cmd), 0, buff, 0, 4);
System.arraycopy(ByteAndInt.int2ByteArray(type), 0, buff, 4, 4);
System.arraycopy(ByteAndInt.int2ByteArray(resultCode), 0, buff, 8, 4);
Log.v(TAG, "sendMsg resultCode: length="+buff.length);
socket.getOutputStream().write(buff);
errCode = 0;
}catch (IOException e) {
e.printStackTrace();
}finally {
}
return errCode;
}
/*发送字串形结果值*/
synchronized int sendData(Socket socket, int cmd, int type, String content) {
int errCode = -1;
if(socket == null) {
return errCode;
}
try {
byte[] tempContent = content.getBytes("UTF-8");
byte []buff = new byte[12 + tempContent.length];
System.arraycopy(ByteAndInt.int2ByteArray(cmd), 0, buff, 0, 4);
System.arraycopy(ByteAndInt.int2ByteArray(type), 0, buff, 4, 4);
System.arraycopy(ByteAndInt.int2ByteArray(tempContent.length), 0, buff, 8, 4);
System.arraycopy(tempContent, 0, buff, 12, tempContent.length);
Log.v(TAG, "sendMsg content destAddr:"+"empty"+" port:"+"empty"+" length="+buff.length);
socket.getOutputStream().write(buff);
errCode = 0;
}catch (IOException e) {
e.printStackTrace();
}finally {
}
return errCode;
}
int checkValidCmdOrType(byte []data, int cmdortype) {
return ByteAndInt.compareBytesData(data, ByteAndInt.int2ByteArray(cmdortype), 4);
}
}
/*数据解析*/
public class DataParser {
static final String TAG = "DataParser";
DataParserHelper dataParserHelper;
ContextWrapper mContextWrapper;
HashMap<String, String> mOnlineList = new HashMap<String, String>();
HashMap<String, String> mPendingMessageList = new LinkedHashMap<String, String>();
String mMyOwnUserName = null;
Socket mSocket = null;
DataReceiverThread mRecvThread = null;
boolean rspRecevied = false;
int mCurrentState = ConstantData.STATE_IDLE;
DataParser (ContextWrapper contextWrapper) {
mContextWrapper = contextWrapper;//用于发出广播消息
dataParserHelper = new DataParserHelper();
}
String getMyOwnUserName() {
return mMyOwnUserName;
}
HashMap<String, String> getOnlineList() {
return mOnlineList;
}
HashMap<String, String> getPendingMessageList() {
return mPendingMessageList;
}
void addOnlineUserInfo(String userName, String addr) {
mOnlineList.put(userName, addr);
}
void removeOnlineUserInfo(String userName) {
mOnlineList.remove(userName);
}
void addPendingMessage(String userName, String msg) {
String str = msg;
if(mPendingMessageList.containsKey(userName)) {
str = mPendingMessageList.get(userName) + str;
}
mPendingMessageList.put(userName, str);
}
void removePendingMessage(String userName) {
mPendingMessageList.remove(userName);
}
void setCurrentState(int state) {
mCurrentState = state;
}
int getCurrentState() {
return mCurrentState;
}
voi
没有合适的资源?快使用搜索试试~ 我知道了~
基于Android的tcp聊天工具
共109个文件
png:42个
class:40个
java:11个
5星 · 超过95%的资源 需积分: 10 97 下载量 26 浏览量
2012-06-05
21:16:27
上传
评论 3
收藏 381KB RAR 举报
温馨提示
1)实现了基本上的注册,登录,聊天功能。并加入了发送超时以及用户是否在线的心跳测试机制。 2)NetMsgServer运行于PC,NetMsg运行在Android手机或者模拟器上。wifi以及3G网络都验证成功。 3)配置文件在ConstantData.java中,请将SERVER_ADDR修改为你自己的主机地址,如果你的主机地址是路由器的内网地址,请登录路由器配置虚拟服务器,将你的局域网地址和4567端口做一下映射。 4)本人联系方式:QQ54476167
资源推荐
资源详情
资源评论
收起资源包目录
基于Android的tcp聊天工具 (109个子文件)
resources.ap_ 98KB
NetMsg.apk 117KB
proguard.cfg 1KB
DataParser.class 11KB
MainActivity.class 7KB
NetMsgServer$DataParser.class 6KB
DataInterface.class 5KB
DataParserHelper.class 4KB
ChatActivity.class 4KB
LoginActivity.class 3KB
NetMsgServer$UserInfoManager.class 3KB
ChatActivity$MyBroadcastReceiver.class 3KB
LoginActivity$MyBroadcastReceiver.class 2KB
MainActivity$MyBroadcastReceiver.class 2KB
MainActivity$MyOnItemClickListener.class 2KB
DataInterface.class 2KB
ConstantData.class 2KB
MainActivity$BtnNewMsgOnClickListener.class 2KB
DataParser$DataReceiverThread.class 2KB
ChatActivity$1.class 2KB
MainService.class 2KB
LoginActivity$BtnRegisterListener.class 2KB
LoginActivity$BtnLoginListener.class 2KB
ChatActivity$BtnSendListener.class 2KB
MainActivity$1.class 2KB
DataParser$MsgTimer.class 1KB
LoginActivity$1.class 1KB
DataParser$HeartBeatTestTimer.class 1KB
NetMsgServer.class 1KB
DataParser$1.class 1KB
NetMsgServer$DataParser$MyTimer.class 1KB
R$string.class 993B
ByteAndInt.class 845B
R$id.class 830B
LoginActivity$BtnExitListener.class 733B
NetMsgServer$DataParser$1.class 707B
NetMsgServer$DataParser$MyTimer$MyTimerTask.class 701B
DataParser$MsgTimer$MyTimerTask.class 648B
R$drawable.class 611B
MainService$MyBinder.class 555B
R.class 468B
R$layout.class 460B
R$attr.class 313B
.classpath 364B
.classpath 301B
classes.dex 35KB
DataParser.java 23KB
NetMsgServer.java 15KB
MainActivity.java 9KB
LoginActivity.java 7KB
DataInterface.java 7KB
ChatActivity.java 6KB
R.java 3KB
ConstantData.java 2KB
DataInterface.java 2KB
MainService.java 1KB
ByteAndInt.java 827B
netmsg.png 19KB
ic_launcher.png 19KB
ic_launcher.png 19KB
ic_launcher.png 17KB
ic_launcher.png 17KB
netmsg.png 17KB
ic_launcher1.png 4KB
green_pig.png 4KB
green_pig.png 4KB
green_pig.png 4KB
ic_launcher1.png 4KB
red_bird.png 4KB
red_bird.png 4KB
red_bird.png 4KB
yellow_bird.png 3KB
yellow_bird.png 3KB
yellow_bird.png 3KB
ic_launcher1.png 3KB
green_pig.png 2KB
green_pig.png 2KB
green_pig.png 2KB
pig_egg.png 2KB
pig_egg.png 2KB
pig_egg.png 2KB
green_bird.png 2KB
green_bird.png 2KB
green_bird.png 2KB
ic_launcher1.png 2KB
red_bird.png 2KB
red_bird.png 2KB
red_bird.png 2KB
pig_egg.png 2KB
pig_egg.png 2KB
pig_egg.png 2KB
yellow_bird.png 2KB
yellow_bird.png 2KB
yellow_bird.png 2KB
ic_launcher.png 2KB
ic_launcher.png 2KB
green_bird.png 1KB
green_bird.png 1KB
green_bird.png 1KB
org.eclipse.jdt.core.prefs 629B
共 109 条
- 1
- 2
qylandy
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 南京邮电大学数学实验:熟练掌握 Matlab 软件的基本命令和操作
- 2017校招真题校园招聘真题算法题(37道)Python源码.zip
- 基于单片机protues仿真的多功能自动饮水机系统设计(仿真图、源代码、演示视频)
- 二叉树7-1-1.cpp
- android 9.0 原生模拟器 签名文件
- 技术面试最后反问面试官的话 校招面试非技术问题有哪些 非技术问题如何回答.png
- NB-IOT-BC26全网通模块Altium+ CADENCE +PADS三种格式(原理图SCH+PCB封装库)文件.zip
- 基于微信小程序开发的校园失物招领系统源码毕业设计(优质项目源码).zip
- 词向量是一种将自然语言中的单词转换为数值向量的技术,它能够捕捉词义和上下文信息
- nmap与masscan的简单使用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页