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();
                }
            }
        }
    }
}

 

posted on 2019-01-27 18:58  lina2014  阅读(81)  评论(0编辑  收藏  举报

导航