| | |
| | |
|
| | |
|
| | | import com.hdl.sdk.common.utils.ByteUtils;
|
| | | import com.hdl.sdk.common.utils.LogUtils;
|
| | | import com.hdl.sdk.common.utils.ThreadToolUtils;
|
| | | import com.hdl.sdk.socket.SocketBoot;
|
| | | import com.hdl.sdk.socket.SocketOptions;
|
| | |
| | | import java.io.OutputStream;
|
| | | import java.net.InetSocketAddress;
|
| | | import java.net.Socket;
|
| | | import java.net.SocketAddress;
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | |
|
| | |
| | |
|
| | | @Override
|
| | | public void connect() throws Exception {
|
| | |
|
| | | mSocket = getSocket();
|
| | | SocketOptions options = getOptions();
|
| | | mSocket.connect(new InetSocketAddress(ip, port));
|
| | | // SocketOptions options = getOptions();
|
| | | mSocket.connect(new InetSocketAddress(ip, port), 3 * 1000);
|
| | | mSocket.setTcpNoDelay(true);
|
| | | mSocket.setReuseAddress(true);
|
| | | mSocket.setKeepAlive(true);
|
| | | mSocket.setSoTimeout(20 * 1000);
|
| | |
|
| | | tcpClientList.add(this);
|
| | | }
|
| | |
| | | public void onHandleResponse() throws Exception {
|
| | | final InputStream stream = getInputStream();
|
| | | if (stream != null && getOptions() != null) {
|
| | | int len=0;
|
| | | while ( (len=getInputStream().read(readBuffer)) != -1) {
|
| | | while ( true) {
|
| | | int len=getInputStream().read(readBuffer);
|
| | | if(len<=0){
|
| | | throw new Exception("接收异常,接收数据长度len="+len);
|
| | | }
|
| | |
|
| | | IHandleMessage handleMessage = getOptions().getHandleMessage();
|
| | | if (handleMessage != null) {
|
| | | byte []bytes = new byte[len];
|
| | | System.arraycopy(readBuffer,0,bytes,0,len);
|
| | | LogUtils.i("收到TCP数据",new String(bytes));
|
| | | //完整的数据才回调
|
| | | handleMessage.read(bytes);
|
| | | }
|
| | |
| | | public void sendMsg(byte[] msg) throws Exception {
|
| | | final OutputStream outputStream = getOutStream();
|
| | | if (outputStream != null && getOptions() != null) {
|
| | | try {
|
| | | IHandleMessage handleMessage = getOptions().getHandleMessage();
|
| | | handleMessage.write(handleMessage.write(msg));
|
| | | getOutStream().write(msg);
|
| | |
|
| | | } finally {
|
| | | outputStream.write(msg);
|
| | | outputStream.flush();
|
| | | }
|
| | | }
|
| | | }
|
| | |
|