zoukankan      html  css  js  c++  java
  • java Socket 长连接 心跳包 客户端 信息收发 demo

    今天写了个socket的测试小程序,代码如下

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    /** 
     * @author 某家: 
     * @version 创建时间:2015年8月17日 下午3:04:14 
     * 类说明 
     */
    public class Connect {
        private static final ThreadLocal<Socket> threadConnect = new ThreadLocal<Socket>(); 
        
        private static final String HOST = "192.168.1.120";
    
        private static final int PORT = 8888;
        
        private static Socket client;
        
        private static OutputStream outStr = null;
        
        private static InputStream inStr = null;
        
        private static Thread tRecv = new Thread(new RecvThread());
        private static Thread tKeep = new Thread(new KeepThread());
    
        public static void connect() throws UnknownHostException, IOException  {
            client = threadConnect.get();
            if(client == null){
                client = new Socket(HOST, PORT);
                threadConnect.set(client);
                tKeep.start();
                System.out.println("========链接开始!========");
            }
            outStr = client.getOutputStream();
            inStr = client.getInputStream();
        }
        
        public static void disconnect() {
            try {
                outStr.close();
                inStr.close();
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
        private static class KeepThread implements Runnable {
            public void run() {
                try {
                    System.out.println("=====================开始发送心跳包==============");
                    while (true) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println("发送心跳数据包");
                        outStr.write("send heart beat data package !".getBytes());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
        
        private static class RecvThread implements Runnable {
            public void run() {
                try {
                    System.out.println("==============开始接收数据===============");
                    while (true) {
                        byte[] b = new byte[1024];
                        int r = inStr.read(b);
                        if(r>-1){
                            String str = new String(b);
                            System.out.println( str );
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
        
        public static void main(String[] args) {
            try {
                Connect.connect();
                tRecv.start();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    服务器端直接用的TCP/UDP Socket 调试工具

    调试工具下载地址

    链接: http://pan.baidu.com/s/1e7vIY 提取码: ipdt

  • 相关阅读:
    记录一下自己写PHP程序时走过的一些坑
    自己写了一个TCP攻击测压平台
    Centos 7x 安装 Telegram MTproxy代理【完美可用】
    "@阅后即焚"上线了!
    小白的机器学习坑3:粗大的安装CUDA
    小白的机器学习坑2:nvidia驱动的安装
    小白的机器学习坑1:ubuntu 18.04的安装
    小白的linux学习笔记9:安装nodejs和gitbook
    小白的linux笔记11:放弃gitbook,转战Sphinx
    小白的linux学习笔记10:安装nginx和第一个网页
  • 原文地址:https://www.cnblogs.com/someonehere/p/4737295.html
Copyright © 2011-2022 走看看