程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Java socket字節流傳輸的示例,javasocket

Java socket字節流傳輸的示例,javasocket

編輯:JAVA綜合教程

Java socket字節流傳輸的示例,javasocket


服務端server端:

 

package com.yuan.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by YUAN on 2016-09-17.
 */
public class TalkServer4Byte {
    
    private ServerSocket server;
    private int port = 5020;

    public TalkServer4Byte() {
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
        }
    }

    public void talk() {
        System.out.println("監控端口:" + port);
        Socket socket = null;
        while (true) {
            try {
                // 阻塞等待,每接收到一個請求就創建一個新的連接實例
                socket = server.accept();
                System.out.println("連接客戶端地址:" + socket.getRemoteSocketAddress());

                // 裝飾流BufferedReader封裝輸入流(接收客戶端的流)
                BufferedInputStream bis = new BufferedInputStream(
                        socket.getInputStream());

                DataInputStream dis = new DataInputStream(bis);
                byte[] bytes = new byte[1]; // 一次讀取一個byte
                String ret = "";
                while (dis.read(bytes) != -1) {
                    ret += bytesToHexString(bytes) + " ";
                    if (dis.available() == 0) { //一個請求
                        doSomething(ret);
                    }
                }

            } catch (IOException e) {
                System.out.println(e.getMessage());
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }

    }
    
    public static void doSomething(String ret) {
        System.out.println(ret);
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String BytesHexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }

    public static void main(String[] args) {
        TalkServer4Byte server = new TalkServer4Byte();
        server.talk();
    }
}

 

 

 

客戶端client代碼:

 

package com.yuan.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

/**
 * Created by YUAN on 2016-09-17.
 */
public class TalkClient4Byte {
    
    private Socket socket;
    private SocketAddress address;

    public TalkClient4Byte() {
        try {
            socket = new Socket();
            address = new InetSocketAddress("127.0.0.1", 5020);
            socket.connect(address, 1000);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void talk() {

        try {

            //使用DataInputStream封裝輸入流
            InputStream os = new DataInputStream(System.in);
            
            byte [] b = new byte[1];
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            while (-1 != os.read(b)) {
                dos.write(b); // 發送給客戶端
            }
            
            dos.flush();
            dos.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {

            }
        }
    }

    public static void main(String[] args) {
        TalkClient4Byte client = new TalkClient4Byte();
        client.talk();
    }

}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved