├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── tjstudy │ │ └── tcpclientdemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── tjstudy │ │ │ └── tcpclientdemo │ │ │ ├── MainActivity.java │ │ │ ├── MyRecParse.java │ │ │ ├── RecData.java │ │ │ ├── adapter │ │ │ └── ChatAdapter.java │ │ │ └── bean │ │ │ └── ChatMess.java │ └── res │ │ ├── drawable │ │ └── shape_chat.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ └── item_rv_chat.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── tjstudy │ └── tcpclientdemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── tcplib ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml ├── java └── com │ └── tjstudy │ └── tcplib │ ├── AbsBaseCallback.java │ ├── BaseRecParse.java │ ├── RequestCallback.java │ ├── ResponseCallback.java │ ├── TCPClient.java │ ├── TCPClientManager.java │ └── utils │ ├── DigitalUtils.java │ └── LoopBuffer.java └── res └── values └── strings.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TCPClient 2 | --------- 3 | 4 | [![](https://jitpack.io/v/WhatWeCan/TCPClient.svg)](https://jitpack.io/#WhatWeCan/TCPClient) 5 | 6 | ---------- 7 | TCPClient android端的TCP封装 8 | 9 | > 最近手上的一个项目,使用TCP进行数据的访问,根据目前的学习状态做一个简单的封装。 10 | > github地址:https://github.com/WhatWeCan/TCPClient 11 | 12 | 怎么使用? 13 | ----- 14 | 15 | 1、导入 16 | ---- 17 | 18 | **Step 1**. Add the JitPack repository to your build file 19 | Add it in your root build.gradle at the end of repositories: 20 | 21 | ``` 22 | allprojects { 23 | repositories { 24 | ... 25 | maven { url 'https://jitpack.io' } 26 | } 27 | } 28 | ``` 29 | **Step 2**. Add the dependency 30 | 31 | ``` 32 | dependencies { 33 | compile 'com.github.WhatWeCan:TCPClient:v1.0.2' 34 | } 35 | 36 | ``` 37 | 38 | 2、使用方法 39 | ------ 40 | 41 | 接收数据 42 | ---- 43 | 44 | 接收数据:判断连接是否存在;连接存在 则不进行操作(默认连接存在就一直接收数据),连接不存在,尝试进行重连操作,连接成功接收数据 45 | 46 | **1、自定义接收数据处理方式** 47 | 48 | 自定义接收数据类型 49 | 50 | ``` 51 | /** 52 | * 接收数据的类型 53 | */ 54 | 55 | public class RecData { 56 | private byte[] head; 57 | private short len; 58 | private byte[] data; 59 | 60 | public RecData() { 61 | } 62 | 63 | public RecData(byte[] head, short len, byte[] data) { 64 | this.head = head; 65 | this.len = len; 66 | this.data = data; 67 | } 68 | //省略 getter setter 69 | } 70 | ``` 71 | 72 | 自定义接收数据规则 示例 73 | ``` 74 | /** 75 | * 自定义转换规则 76 | * 自定义接收数据的协议 77 | * 示例:接收到的数据必须以$$(2个字节)开头 数据的大小(short,byte 2个字节 数据大小=2+2+数据大小) 78 | */ 79 | 80 | public class MyRecParse extends BaseRecParse { 81 | private static final String TAG = "MyRecParse"; 82 | 83 | @Override 84 | public List parse() { 85 | ArrayList recDataList = new ArrayList<>(); 86 | byte[] baseData = getBaseData();//总数据 87 | 88 | int removeSize = 0; 89 | byte[] head = new byte[2]; 90 | 91 | for (int i = 0; i < baseData.length - 1; i++) { 92 | if (baseData[i] == '$' && baseData[i + 1] == '$') { 93 | //找到头了 进一步进行处理 94 | //然后两个字节是数据长度 95 | short dataLen = DigitalUtils.byte2Short(baseData, i + 2); 96 | if (baseData.length - i + 1 > dataLen) {//说明数据完整 97 | Log.e(TAG, "parse: 有完整的数据"); 98 | byte[] recData = new byte[dataLen]; 99 | System.arraycopy(baseData, i, recData, 0, dataLen); 100 | i += dataLen - 1; 101 | removeSize += dataLen; 102 | 103 | //头 104 | head[0] = baseData[i]; 105 | head[1] = baseData[i + 1]; 106 | //长度 dataLen 107 | //数据 bytes 108 | recDataList.add(new RecData(head, dataLen, recData)); 109 | } else { 110 | Log.e(TAG, "parse: nonono完整的数据"); 111 | break;//如果长度不足 就等着 112 | } 113 | } else { 114 | removeSize++; 115 | } 116 | } 117 | notifyLeftData(removeSize); 118 | return recDataList; 119 | } 120 | } 121 | ``` 122 | **2、开启接收数据** 123 | 124 | ``` 125 | TCPClient.build() 126 | .server(ipAddress, 8888) 127 | .breath("heart".getBytes(), 6 * 1000) 128 | .connTimeout(10 * 1000) 129 | .onResponse(responseCallback); 130 | ``` 131 | 132 | 发送数据 133 | ---- 134 | 发送数据:判断连接是否存在;连接存在 则直接发送数据,连接不存在,尝试进行重连操作,连接成功发送数据,并接收数据。 135 | 136 | ``` 137 | TCPClient.build() 138 | .server(ipAddress, 8888) 139 | .breath("heart".getBytes(), 6 * 1000) 140 | .connTimeout(10 * 1000) 141 | .request(sendMess.getBytes(), 8000, new RequestCallback() { 142 | @Override 143 | public void onTimeout() { 144 | Log.e(TAG, "onTimeout:请求超时,稍后重试 ,关闭连接 "); 145 | TCPClient.closeTcp(ipAddress, 8888); 146 | } 147 | 148 | @Override 149 | public void onFail(Throwable throwable) { 150 | handlerError(throwable); 151 | } 152 | }, responseCallback);//接收数据回调 153 | ``` 154 | 关闭连接 155 | ---- 156 | 157 | 关闭指定连接 158 | TCPClient.closeTcp(“192.168.5.75”, 8888); 159 | 160 | 简易聊天demo 演示 161 | ------- 162 | 163 | ![这里写图片描述](http://img.blog.csdn.net/20170704120552841?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjM5MTg3Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) 164 | 165 | 服务器端截图:略 166 | 167 | demo地址 168 | ------ 169 | 170 | app:https://github.com/WhatWeCan/TCPClient 171 | server:https://github.com/WhatWeCan/TCPServerDemo 172 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.2" 6 | defaultConfig { 7 | applicationId "com.tjstudy.tcpclientdemo" 8 | minSdkVersion 14 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:24.2.1' 28 | compile 'com.android.support:design:24.2.1' 29 | testCompile 'junit:junit:4.12' 30 | compile project(path: ':tcplib') 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/tjstudy/tcpclientdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.tjstudy.tcpclientdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.tjstudy.tcpclientdemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/tjstudy/tcpclientdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.tjstudy.tcpclientdemo; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.util.Log; 8 | import android.view.View; 9 | import android.widget.Button; 10 | import android.widget.EditText; 11 | 12 | import com.tjstudy.tcpclientdemo.adapter.ChatAdapter; 13 | import com.tjstudy.tcpclientdemo.bean.ChatMess; 14 | import com.tjstudy.tcplib.RequestCallback; 15 | import com.tjstudy.tcplib.ResponseCallback; 16 | import com.tjstudy.tcplib.TCPClient; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | public class MainActivity extends AppCompatActivity { 22 | private static final String TAG = "MainActivity"; 23 | private String ipAddress; 24 | private RecyclerView rvChat; 25 | private ChatAdapter chatAdapter; 26 | private EditText etInput; 27 | private Button btnSend; 28 | private TCPClient tcpClient; 29 | private ArrayList chatMessList; 30 | private LinearLayoutManager chatLayoutManager; 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_main); 36 | ipAddress = "192.168.5.69"; 37 | initNet(); 38 | initView(); 39 | } 40 | 41 | private void initNet() { 42 | tcpClient = TCPClient.build() 43 | .server(ipAddress, 8888) 44 | .breath("heart".getBytes(), 6 * 1000) 45 | .connTimeout(10 * 1000); 46 | } 47 | 48 | private void initView() { 49 | rvChat = (RecyclerView) findViewById(R.id.recycler_chat); 50 | chatLayoutManager = new LinearLayoutManager(this); 51 | rvChat.setLayoutManager(chatLayoutManager); 52 | chatMessList = new ArrayList<>(); 53 | chatAdapter = new ChatAdapter(this, chatMessList); 54 | rvChat.setAdapter(chatAdapter); 55 | 56 | etInput = (EditText) findViewById(R.id.et_input); 57 | btnSend = (Button) findViewById(R.id.btn_send); 58 | btnSend.setOnClickListener(new View.OnClickListener() { 59 | @Override 60 | public void onClick(View v) { 61 | final String sendMess = etInput.getText().toString(); 62 | ChatMess chatMess = new ChatMess(); 63 | chatMess.setType(2); 64 | chatMess.setMesg(sendMess); 65 | chatMessList.add(chatMess); 66 | chatAdapter.notifyDataSetChanged(); 67 | chatLayoutManager.scrollToPosition(chatMessList.size() - 1); 68 | 69 | tcpClient.request(sendMess.getBytes(), 8000, new RequestCallback() { 70 | @Override 71 | public void onTimeout() { 72 | Log.e(TAG, "onTimeout:请求超时,稍后重试 ,关闭连接 "); 73 | TCPClient.closeTcp(ipAddress, 8888); 74 | } 75 | 76 | @Override 77 | public void onFail(Throwable throwable) { 78 | handlerError(throwable); 79 | } 80 | }, responseCallback); 81 | } 82 | }); 83 | } 84 | 85 | private void handlerError(Throwable throwable) { 86 | Log.e(TAG, "handlerError: 网络访问失败:" + throwable.getMessage()); 87 | } 88 | 89 | @Override 90 | protected void onDestroy() { 91 | super.onDestroy(); 92 | TCPClient.closeTcp(ipAddress, 8888); 93 | } 94 | 95 | private ResponseCallback responseCallback = new ResponseCallback() { 96 | @Override 97 | public void onRec() { 98 | MyRecParse myRecParse = new MyRecParse(); 99 | List dataList = myRecParse.parse(); 100 | if (dataList.size() > 0) { 101 | for (RecData recData : 102 | dataList) { 103 | ChatMess chatMess = new ChatMess(); 104 | chatMess.setType(1); 105 | chatMess.setMesg(new String(recData.getData())); 106 | chatMessList.add(chatMess); 107 | chatAdapter.notifyDataSetChanged(); 108 | chatLayoutManager.scrollToPosition(chatMessList.size() - 1); 109 | } 110 | } 111 | } 112 | 113 | @Override 114 | public void onFail(Throwable throwable) { 115 | handlerError(throwable); 116 | } 117 | }; 118 | 119 | /** 120 | * 测试场景:不请求数据时,进行数据的接收 心跳时间间隔改短 6s 121 | * 122 | * @param view 123 | */ 124 | public void onRecHeartTest(View view) { 125 | tcpClient.onResponse(responseCallback); 126 | } 127 | } -------------------------------------------------------------------------------- /app/src/main/java/com/tjstudy/tcpclientdemo/MyRecParse.java: -------------------------------------------------------------------------------- 1 | package com.tjstudy.tcpclientdemo; 2 | 3 | import android.util.Log; 4 | 5 | import com.tjstudy.tcplib.BaseRecParse; 6 | import com.tjstudy.tcplib.utils.DigitalUtils; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | /** 12 | * 自定义转换规则 13 | * 自定义接收数据的协议 14 | * 示例:接收到的数据必须以$$(2个字节)开头 数据的大小(short,byte 2个字节 数据大小=2+2+数据大小) 15 | */ 16 | 17 | public class MyRecParse extends BaseRecParse { 18 | private static final String TAG = "MyRecdataFilter"; 19 | 20 | @Override 21 | public List parse() { 22 | ArrayList recDataList = new ArrayList<>(); 23 | byte[] baseData = getBaseData();//总数据 24 | 25 | int removeSize = 0; 26 | byte[] head = new byte[2]; 27 | 28 | for (int i = 0; i < baseData.length - 1; i++) { 29 | if (baseData[i] == '$' && baseData[i + 1] == '$') { 30 | //找到头了 进一步进行处理 31 | //然后两个字节是数据长度 32 | short dataLen = DigitalUtils.byte2Short(baseData, i + 2); 33 | if (baseData.length - i + 1 > dataLen) {//说明数据完整 34 | byte[] recData = new byte[dataLen]; 35 | System.arraycopy(baseData, i + 4, recData, 0, dataLen - 4); 36 | Log.e(TAG, "parse: 有完整的数据=" + new String(recData)); 37 | i += dataLen - 1; 38 | removeSize += dataLen; 39 | 40 | //头 41 | head[0] = recData[0]; 42 | head[1] = recData[1]; 43 | //长度 dataLen 44 | //数据 bytes 45 | recDataList.add(new RecData(head, dataLen, recData)); 46 | } else { 47 | Log.e(TAG, "parse: nonono完整的数据"); 48 | break;//如果长度不足 就等着 49 | } 50 | } else { 51 | removeSize++; 52 | } 53 | } 54 | notifyLeftData(removeSize); 55 | return recDataList; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/tjstudy/tcpclientdemo/RecData.java: -------------------------------------------------------------------------------- 1 | package com.tjstudy.tcpclientdemo; 2 | 3 | /** 4 | * 接收数据的类型 5 | */ 6 | 7 | public class RecData { 8 | private byte[] head; 9 | private short len; 10 | private byte[] data; 11 | 12 | public RecData() { 13 | } 14 | 15 | public RecData(byte[] head, short len, byte[] data) { 16 | this.head = head; 17 | this.len = len; 18 | this.data = data; 19 | } 20 | 21 | public byte[] getHead() { 22 | return head; 23 | } 24 | 25 | public void setHead(byte[] head) { 26 | this.head = head; 27 | } 28 | 29 | public short getLen() { 30 | return len; 31 | } 32 | 33 | public void setLen(short len) { 34 | this.len = len; 35 | } 36 | 37 | public byte[] getData() { 38 | return data; 39 | } 40 | 41 | public void setData(byte[] data) { 42 | this.data = data; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/tjstudy/tcpclientdemo/adapter/ChatAdapter.java: -------------------------------------------------------------------------------- 1 | package com.tjstudy.tcpclientdemo.adapter; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.TextView; 9 | 10 | import com.tjstudy.tcpclientdemo.R; 11 | import com.tjstudy.tcpclientdemo.bean.ChatMess; 12 | 13 | import java.util.List; 14 | 15 | /** 16 | * Chat adapter 17 | */ 18 | 19 | public class ChatAdapter extends RecyclerView.Adapter { 20 | List chatMessList; 21 | Context mContext; 22 | 23 | public ChatAdapter(Context mContext, List chatMessList) { 24 | this.chatMessList = chatMessList; 25 | this.mContext = mContext; 26 | } 27 | 28 | @Override 29 | public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) { 30 | return new ChatHolder(LayoutInflater.from(mContext).inflate(R.layout.item_rv_chat, parent, false)); 31 | } 32 | 33 | @Override 34 | public void onBindViewHolder(ChatHolder holder, int position) { 35 | ChatMess chatMess = chatMessList.get(position); 36 | if (chatMess.getType() == 1) { 37 | holder.tvSend.setVisibility(View.GONE); 38 | holder.tvRec.setVisibility(View.VISIBLE); 39 | holder.tvRec.setText(chatMess.getMesg()); 40 | } else { 41 | holder.tvRec.setVisibility(View.GONE); 42 | holder.tvSend.setVisibility(View.VISIBLE); 43 | holder.tvSend.setText(chatMess.getMesg()); 44 | } 45 | } 46 | 47 | @Override 48 | public int getItemCount() { 49 | return chatMessList.size(); 50 | } 51 | 52 | public class ChatHolder extends RecyclerView.ViewHolder { 53 | 54 | TextView tvSend; 55 | TextView tvRec; 56 | 57 | public ChatHolder(View itemView) { 58 | super(itemView); 59 | tvSend = (TextView) itemView.findViewById(R.id.tv_send); 60 | tvRec = (TextView) itemView.findViewById(R.id.tv_rec); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/com/tjstudy/tcpclientdemo/bean/ChatMess.java: -------------------------------------------------------------------------------- 1 | package com.tjstudy.tcpclientdemo.bean; 2 | 3 | /** 4 | * 聊天 消息实体 5 | */ 6 | 7 | public class ChatMess { 8 | private int type; 9 | private String mesg; 10 | 11 | public int getType() { 12 | return type; 13 | } 14 | 15 | public void setType(int type) { 16 | this.type = type; 17 | } 18 | 19 | public String getMesg() { 20 | return mesg; 21 | } 22 | 23 | public void setMesg(String mesg) { 24 | this.mesg = mesg; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_chat.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 |