java Socket 長串連 心跳包 用戶端 資訊收發 demo

來源:互聯網
上載者:User

標籤:

今天寫了個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

java Socket 長串連 心跳包 用戶端 資訊收發 demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.