通过确认从java中的tcp套接字读取数据包

时间:2015-01-31 11:19:26

标签: java sockets tcp

我们正在创建一个java监听器来读取在特定服务器ip和port.Device上配置的多个设备数据,遵循以下规则。

  1. 设备发送登录数据包。
  2. 服务器将返回ack数据包作为响应。
  3. 收到ack设备后会发送信息包。
  4. 服务器读取该数据。
  5. 在最后一步我们卡住了,我们发送了ack但是无法从设备获取信息包(虽然我们通过开源软件检查生成的ack)。对于ref我们附加代码。(如果我们删除while(true)比获取登录数据包但在此之后套接字连接将关闭,设备将再次发送登录数据包,但如果我们保留它,那么我们不会得到任何数据包)

    // -------------- Main class ----------------------------- -------------

    public class Main {

    public static void main(String[] args) {
        Server server = new Server(listen_port, pool_size, pm);
        new Thread(server).start();
        logger.info("Server Started .....");
    }
    

    } // ------------------------------------------------ --------------

    public class Server实现Runnable {

    private ServerSocket serverSocket = null;
    public void run()
    

    {     this.m_stop = false;

    while (!this.m_stop)
      try {
        this.m_pool.execute(new Handler(this.serverSocket.accept()));
      } catch (IOException e) {
        LOGGER.debug("Unable to accept connection ", e);
      }
    

    } }

    // --------------------------------------------- -----------------

    public class Handler实现了Runnable {

    private Socket m_clientSocket;
    private String imei;
    
    public Handler(Socket socket) {
        this.m_clientSocket = socket;
    }
    
    public void run() {
        DataOutputStream clientDataOS = null;
        DataInputStream clientDataIS = null;
        try {
            logger.info("data is coming");
            m_clientSocket.setSoTimeout(300000);
            clientDataIS = new DataInputStream(this.m_clientSocket.getInputStream());
            clientDataOS = new DataOutputStream(this.m_clientSocket.getOutputStream());
            while (true) {
                String pkt = "";
                logger.info("Waiting for input strem");
                byte[] byte_pkt = IOUtils.toByteArray(clientDataIS);
                logger.info("Got input stream");
                for (byte b : byte_pkt) {
                    pkt += String.format("%02X ", b);
                }
                logger.info(pkt);
                if (byte_pkt.length > 0) {
                    logger.info("");
                    if (Byte.valueOf(byte_pkt[3]) == 1) {
                        imei = "xyz";
                        logger.info("login packet");
                        byte[] rep_pkt = Utils.getReceptionPacket(byte_pkt);//send back to device
                        clientDataOS.write(rep_pkt);
                        clientDataOS.flush();
                    } else if (Byte.valueOf(byte_pkt[3]) == 34) {
                        logger.info("information packet");
                        Utils.processPackets(byte_pkt);
                    } else {
                        logger.info("Unkown packet format");
                    }
                    logger.info(imei);
                } else {
                    logger.info("InputStream is empty.");
                }
            }
        } catch (SocketException se) {
            logger.error("Failure on reading data", se);
        } catch (IOException e) {
            logger.error("Failure on reading data", e);
        } catch (Exception e) {
            logger.error("Error while processing data", e);
        } finally {
            try {
                IOUtils.closeQuietly(clientDataOS);
                IOUtils.closeQuietly(clientDataIS);
                this.m_clientSocket.close();
            } catch (IOException e) {
                logger.debug("Error when sending out response ::", e);
            }
        }
    }
    

    }

0 个答案:

没有答案