java 網路(socket)

來源:互聯網
上載者:User

標籤:

本文梳理一個基礎的java TCP訊息通訊,構造一個簡單的Packet進行傳輸,代碼如下:

  • Packet
public class Packet {    private String attribute;        public Packet(String attr){        this.attribute = attr;    }        public String getAttribute() {        return attribute;    }    public void setAttribute(String attribute) {        this.attribute = attribute;    }}
  • 伺服器端代碼
public class App {        private static final int PORT = 4000;    public static void main(String[] args) throws IOException {                ServerSocket listen = new ServerSocket(PORT);        Socket          client = null;        while(true){            client = listen.accept();            new Thread(new ServerThread(client)).start();        }    }}
public class ServerThread implements Runnable{        private static final int BUFSIZE  =1024;    private Socket client = null;        public ServerThread(Socket client){        this.client = client;    }        @Override    public void run() {        try {            InputStream in      = client.getInputStream();            OutputStream out = client.getOutputStream();                         byte[] buffer = new byte[BUFSIZE];                               while (in.read(buffer) != -1) {                //判斷包頭                if(buffer[0] == 0x01 && buffer[1] == 0x02){                    byte[] tmp = new byte[BUFSIZE];                    int index = 0;                    for(int i = 2; i < buffer.length; i++){                        tmp[index++] = buffer[i];                    }                                        Packet packet = new Packet(new String(tmp,"GB2312"));                                        System.out.println(packet.getAttribute());                }else{                    System.out.println("訊息格式不正確");                }            }                        out.close();            in.close();                    } catch (IOException e) {            e.printStackTrace();        }        }}
  • 用戶端代碼
public class App {        private static final int PORT = 4000;    private static final int BUFSIZE  =1024;    public static void main(String[] args) throws IOException {                Socket client = new Socket("127.0.0.1", PORT);                InputStream in      = client.getInputStream();        OutputStream out = client.getOutputStream();                Packet packet = new Packet("型別參數2345");                    byte[] output = new byte[BUFSIZE];        output[0] = 0x01;        output[1] = 0x02;                int index = 2;        //若增加包長度欄位,則可實現packet的分包、組包        byte[] tmp1 = packet.getAttribute().getBytes();        for(int i= 0; i < tmp1.length; i++){            output[index++] = tmp1[i];        }                                            out.write(output);                byte[] buffer = new byte[BUFSIZE];        while (in.read(buffer) != -1) {              System.out.println(new String(buffer, "GB2312"));        }                out.close();        in.close();        client.close();                }}

java 網路(socket)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.