Socket编程

Posted xidian2014

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket编程相关的知识,希望对你有一定的参考价值。

package com.test;

import org.junit.Test;

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

public class TestSocket {
    @Test
    public void client() {
        Socket socket = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;
        InputStream file = null;
        try {
            socket = new Socket("127.0.0.1", 9890);
            outputStream = socket.getOutputStream();
            inputStream = socket.getInputStream();
            file = new FileInputStream("/Users/lina/Desktop/work/path.py");

            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = file.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            //通过shutdownOutput高速服务器已经发送完数据,后续只能接受数据
            socket.shutdownOutput();

            StringBuilder sb = new StringBuilder();
            while ((len = inputStream.read(buf)) != -1) {
                //注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8
                sb.append(new String(buf, 0, len, "UTF-8"));
            }
            System.out.println("get message from server: " + sb);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (file != null) {
                try {
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void server() {
        ServerSocket server = null;
        Socket socket = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        OutputStream out = null;
        try {
            server = new ServerSocket(9890);
            System.out.println("server将一直等待连接的到来");
            socket = server.accept();
            inputStream = socket.getInputStream();
            outputStream = new FileOutputStream("/Users/lina/Desktop/work/server.py");
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
                outputStream.flush();
            }

            out = socket.getOutputStream();
            out.write("Hello Client,I get the message.".getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (server != null) {
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

以上是关于Socket编程的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

使用 Pygments 检测代码片段的编程语言

面向面试编程代码片段之GC

如何在 Django Summernote 中显示编程片段的代码块?

Linux系统编程—网络编程—socket代码实现