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();
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    通过java代码获取jvm信息和系统信息
    java cp与java jar的区别
    vue下实现WebRTC
    MANIFEST.MF文件详解
    element 前端排序 与 后端排序
    JAVA获取CPUID、主板序列号、硬盘序列号、MAC地址(自己验证过)
    PHP常用代码大全
    程序员从初级到中级10个秘诀
    移动平台还有哪些创业机会
    程序员招聘:如何识别真正的程序员
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10327640.html
Copyright © 2011-2022 走看看