本文原创,欢迎随便匿名转载,你高兴就行。
背景:小的是做android开发的,项目中经常要和硬件打交道,所以会遇到好多做android开发不经常遇到的问题。这次项目中要和硬件之间用TCP通信,我是客户端,使用dataInputStream.readUTF()方法收不到硬件发过来的数据,但是和我自己写的服务端通信都正常。F&&&K!!!
google半天发现这句话 “readUTF()只能读取writeUTF方法写入的数据”,所以我只能直接使用输入输出流操作,DataInputStream和DataOutputStream都是Java中输入输出流的装饰类,用起来很方便,但其也有局限性。
自己封装了一个TcpClientConnector,有用的上的直接用就行。
TcpClientConnector
package com.hadisi.socket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* Created by hadisi on 2016/6/28.
*/
public class TcpClientConnector {
private static TcpClientConnector mTcpClientConnector;
private Socket mClient;
private ConnectLinstener mListener;
private Thread mConnectThread;
public interface ConnectLinstener {
void onReceiveData(String data);
}
public void setOnConnectLinstener(ConnectLinstener linstener) {
this.mListener = linstener;
}
public static TcpClientConnector getInstance() {
if (mTcpClientConnector == null)
mTcpClientConnector = new TcpClientConnector();
return mTcpClientConnector;
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 100:
if (mListener != null) {
mListener.onReceiveData(msg.getData().getString("data"));
}
break;
}
}
};
public void creatConnect(final String mSerIP, final int mSerPort) {
if (mConnectThread == null) {
mConnectThread = new Thread(new Runnable() {
@Override
public void run() {
try {
connect(mSerIP, mSerPort);
} catch (IOException e) {
e.printStackTrace();
}
}
});
mConnectThread.start();
}
}
/**
* 与服务端进行连接
*
* @throws IOException
*/
private void connect(String mSerIP, int mSerPort) throws IOException {
if (mClient == null) {
mClient = new Socket(mSerIP, mSerPort);
}
InputStream inputStream = mClient.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
String data = new String(buffer, 0, len);
Message message = new Message();
message.what = 100;
Bundle bundle = new Bundle();
bundle.putString("data", data);
message.setData(bundle);
mHandler.sendMessage(message);
}
}
/**
* 发送数据
*
* @param data 需要发送的内容
*/
public void send(String data) throws IOException {
OutputStream outputStream = mClient.getOutputStream();
outputStream.write(data.getBytes());
}
/**
* 断开连接
*
* @throws IOException
*/
public void disconnect() throws IOException {
if (mClient != null) {
mClient.close();
mClient = null;
}
}
}
代码很简单,用了单例,封装了发送数据send(String data)方法和断开连接disconnect()方法,还有一接收数据的接口OnConnectLinstener。
怎么用它?
- 获取TcpClientConnector单例对象
connector = TcpClientConnector.getInstance();
- 创建tcp连接creatConnect("服务端IP地址","服务端端口号");
connector.creatConnect("服务端IP地址","服务端端口号");
- 实现接收数据接口OnConnectLinstener,onReceiveData(String data)中的data就是服务端返回的数据;
connector.setOnConnectLinstener(new TcpClientConnector.ConnectLinstener() {
@Override
public void onReceiveData(String data) {
//do somethings.
}
});
- 在你想要断开连接的地方调用disconnect()方法,一般在onDestroy()方法中调用。
@Override
protected void onDestroy() {
super.onDestroy();
try {
connector.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
网友评论