bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

JAVA中怎么實現(xiàn)一個Socket操作工具類

JAVA中怎么實現(xiàn)一個Socket操作工具類,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

站在用戶的角度思考問題,與客戶深入溝通,找到湘東網(wǎng)站設(shè)計與湘東網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋湘東地區(qū)。

用java.net.Socket進(jìn)行Socket操作工具類
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author JL
 * @version V1.0
 * @Description 用java.net.Socket進(jìn)行Socket操作工具類
 */
public class SocketUtils {

    private static final String ENCODING = "UTF-8";

    public static void listen(final int port, String content, SocketFunction function) throws IOException{
        ServerSocket serverSocket = null ;
        Socket socket = null;
        try {
            serverSocket = createServer(port);
            socket = createServerSocket(serverSocket);
            //通常獲取到socket連接后,都是分配一個線程來處理客戶端
            WorkClass workClass = new WorkClass(socket, content, function);
            workClass.work();
        }catch(IOException ioe){
            ioe.printStackTrace();
            throw ioe;
        }finally{
            clientClose(socket);
            //關(guān)閉服務(wù)端
            serverClose(serverSocket);
        }
    }

    static class WorkClass{
        private Socket socket ;
        private String content;
        private SocketFunction function;
        public WorkClass(Socket socket, String content, SocketFunction function){
            this.socket = socket;
            this.content = content;
            this.function = function;
        }

        public void work() throws IOException {
            String msg = null;
            InputStream in = null;
            OutputStream out = null;
            try {
                in = socket.getInputStream();
                msg = input(in);
                out = socket.getOutputStream();
                output(out, content);
                function.callback(msg);
            }finally{
                //關(guān)閉輸入輸出流
                streamClose(in, out);
            }
        }
    }

    public static void send(String host, int port, String content, SocketFunction function) throws IOException{
        Socket socket = null;
        String msg = null;
        InputStream in = null;
        OutputStream out = null;
        try {
            socket = createClientSocket(host, port);
            out = socket.getOutputStream();
            output(out, content);
            socket.shutdownOutput();//輸出完后,需要關(guān)閉socket的輸出通道,表示不存向服務(wù)端輸出內(nèi)容
            in = socket.getInputStream();
            msg = input(in);
            function.callback(msg);
        }catch(UnknownHostException uhe){
            uhe.printStackTrace();
            throw new IOException("主機連接創(chuàng)建異常:" + uhe.getMessage());
        }catch(IOException ioe){
            ioe.printStackTrace();
            throw ioe;
        }finally{
            streamClose(in, out);
            clientClose(socket);
        }
    }

    public static void streamClose(InputStream in, OutputStream out){
        //IOUtils.closeQuietly(in); 可用IOUtils工具類關(guān)閉流
        if (in != null){
            try {
                in.close();
            }catch(IOException ioe){
                System.out.println("關(guān)閉輸入流異常:" + ioe.getMessage());
            }
        }
        if (out != null){
            try {
                out.flush();
                out.close();
            }catch(IOException ioe){
                System.out.println("關(guān)閉輸出流異常:" + ioe.getMessage());
            }
        }
    }

    private static ServerSocket createServer(int port) throws IOException {
        System.out.println("監(jiān)聽端口號:" + port);
        return new ServerSocket(port);
    }

    private static Socket createServerSocket(ServerSocket serverSocket) throws IOException {
        Socket socket = serverSocket.accept();
        System.out.println("獲取到客戶端連接:" + socket.getInetAddress().getHostAddress());
        return socket;
    }

    private static Socket createClientSocket(String host, int port) throws UnknownHostException, IOException {
        return new Socket(host, port);
    }

    private static void serverClose(ServerSocket server) {
        if (server != null && !server.isClosed()){
            try{
                server.close();
            }catch(IOException ioe){
                System.out.println("服務(wù)關(guān)閉異常:" + ioe.getMessage());
            }
        }
    }

    private static void clientClose(Socket socket){
        if (socket != null && !socket.isClosed()){
            try{
                socket.close();
            }catch(IOException ioe){
                System.out.println("Socket關(guān)閉異常:" + ioe.getMessage());
            }
        }
    }

    public static OutputStream output(OutputStream out, String content) throws IOException {
        try{
            out.write(content.getBytes(ENCODING));
        }finally{
            return out;
        }
    }

    public static String input(InputStream in) throws IOException {
        int len;
        char[] b = new char[1024];
        StringBuilder sb = new StringBuilder();
        BufferedReader reader ;
        try {
            //以字符流為主,如需字節(jié)流,則不需要BufferedReader和InputStreamReader,可以直接從InputStream中獲取或采用對應(yīng)緩沖包裝類
            reader = new BufferedReader(new InputStreamReader(in, ENCODING));
            while ((len = reader.read(b)) != -1) {
                sb.append(b, 0, len);
            }
            //reader.close();
        }finally{
        }
        return sb.toString();
    }

    public interface SocketFunction{
        void callback(String msg);
    }

    public static void main(String[] args) throws IOException{
        String data = "這是測試數(shù)據(jù):";
        //測試時,請分別單獨啟動send和listen方法
        //客戶端
//        send("127.0.0.1",8111, "this is client test", new SocketFunction(){
//            @Override
//            public void callback(String msg) {
//                System.out.println(data + msg);
//            }
//        });

        //服務(wù)端
        listen(8111, "this is server test", new SocketFunction() {
            @Override
            public void callback(String msg) {
                System.out.println(data + msg);
            }
        });
    }

}

說明:

做過項目的人都知道,很多寫過的可重復(fù)利用的代碼塊或有用的工具類沒有怎么整理,當(dāng)需要的時候,又得打開項目查找一翻,雖然功能開發(fā)不難,但是又得花時間成本去寫去測試,這樣的重復(fù)造輪子的事情太多次了;因此不如把輪子保留,供大家一起使用;

1.這個輪子可以有:需要使用的時候確實還不存在這個組件。
2.我需要的時候輪子不在:每一種技術(shù)或工具產(chǎn)生都有它的項目背景,當(dāng)代碼寫在項目里的時候,我知道有這個東西,當(dāng)換了一個項目或公司后,沒有備份也沒有記錄,這個時候你不在了,又得花時間手打一遍;
3.我不知道是不是造輪子:大多數(shù)情況下初學(xué)者很難分清楚自己是不是在重復(fù)造輪子,事實上造輪子不是我目的。我的目的是完成工作任務(wù),任務(wù)完成的速度越快越好,質(zhì)量越高越好。而不是去判斷自己在不在造輪子。
4.不想重復(fù)花時間造輪子:有時候還會碰到一些并不困難但是很占時間的東西,當(dāng)然有現(xiàn)成的輪子是花時間最少的;
5.我就是想學(xué)習(xí)輪子:初學(xué)者的并不是在重復(fù)造輪子,而是學(xué)習(xí)后以提高為自己的知識與技能。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

標(biāo)題名稱:JAVA中怎么實現(xiàn)一個Socket操作工具類
文章出自:http://vcdvsql.cn/article2/jheioc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司關(guān)鍵詞優(yōu)化定制開發(fā)品牌網(wǎng)站設(shè)計標(biāo)簽優(yōu)化網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作