zoukankan      html  css  js  c++  java
  • 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();
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    一个列表如何根据另一个列表进行排序(数组的相对排序)
    汉诺塔问题
    python面向对象基础
    python爬虫
    软件开发目录规范
    python--->包
    编译python文件
    python文件的俩种用途
    python模块的搜索路径
    python 循环导入的问题
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10327640.html
Copyright © 2011-2022 走看看