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();
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    破解VNC密码
    word无法插入页码
    修改windows远程桌面端口号
    系统每次进入桌面报错“由于启动计算机时出现了页面文件配置问题”
    树梅派安装GMChess中国象棋
    Linux命令Top详解
    树梅派修改root密码及切换账户
    树梅派安装截图工具
    Linux有趣的应用,画只小猫陪伴你
    Windows 10访问XP系统共享文件报”因为文件共享不安全,所以你不能连接到文件共享。。。“
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10327640.html
Copyright © 2011-2022 走看看