tcp通信协议

最近用到了tcp的通信协议,而我想想似乎又没啥好写的,就把tcp的当成这次博文吧。

tcp是面向连接的通信协议,所用你首先得知道目标服务器的ip地址跟端口号。才能跟服务器建立连接。但是有一点得注意一下,要服务器先启动,客户端才能与之建立连接的唷。

就讲这么多,接下来上代码:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
* Created by cjj on 2016/4/26.
*/
public class TcpClient {
  private String ADDRESS;
  private int PORT;

  private Socket mSocket;
  private DataOutputStream dout = null;
  private DataInputStream din = null;
  private static OnResponseListener mResponse;
  private byte[] sendData;
  private SendThread mSendThread;
  private ReciveThread mReciveThread;


  public TcpClient(String ip, int port) {
      this.ADDRESS = ip;
      this.PORT = port;
  }

  public void setResponseListener(OnResponseListener response) {
      mResponse = response;
  }

  public void send(final byte[] sendData) {
      this.sendData = sendData;
      new SendThread().start();
  }

  private class SendThread extends Thread {
      @Override
      public void run() {
          super.run();
          try {
              if (mSocket == null) mSocket = new Socket(ADDRESS, PORT);
              if (dout == null) dout = new DataOutputStream(mSocket.getOutputStream());
              dout.write(sendData);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }

  private class ReciveThread extends Thread {
      @Override
      public void run() {
          try {
              while (true) {
                  if (mSocket == null)
                      mSocket = new Socket(ADDRESS, PORT);
                  if (din == null)
                      din = new DataInputStream(mSocket.getInputStream());
                  byte[] b = new byte[1024];
                  int length;
                  while ((length = din.read(b)) > -1) {
                      String msgs = new String(b, 0, length, "UTF-8");
                      mResponse.onSuccess(msgs);
                  }
              }
          } catch (IOException e) {
              mResponse.onFailure(e);
          }
      }
  }

  public void onDestory() {
      try {
          if (dout != null) dout.close();
          if (din != null) din.close();
          if (mSocket != null) mSocket.close();
      } catch (IOException e) {
          e.printStackTrace();
      }

  }

  public void listen() {
      if (mReciveThread == null) mReciveThread = new ReciveThread();
      mReciveThread.start();
  }

  public interface OnResponseListener {
      void onSuccess(String resp);

      void onFailure(Exception e);
  }
从以上代码不难看出这是一个tcp客户端封装的一个类,调用起来也是蛮方便的,以面的话就是用法了:

TcpClient tcpClient = new TcpClient(IP,PORT);
tcpClient.listen();//开启接收数据的监听
tcpClient.setResponseListener(new TcpClient.OnResponseListener() {
      @Override
      public void onSuccess(String resp) {
            // TODO: 2016/4/26 对接收到的数据进行处理
       }

      @Override
      public void onFailure(Exception e) {

      }
});

一些细心的可能就注意到用法里面少了个东西,对你没看错就是少了一个发送数据的,其实你在你想要发送数据的地方调用tcpClient.send(byte[] b);就好了。

最后的话要调用一下tcpClient.onDestory();你懂的。

嗯就这么多了!


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值