• socket demo程序


    package cn.example.socket;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class MyClient {
        public static void main(String[] args) throws UnknownHostException,
                IOException {
            // 定义socket
            Socket socket = new Socket("localhost", 9999);
            // 获取输入输出
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();
    
            // 处理
    
            PrintWriter pw = new PrintWriter(outputStream);
            pw.print("hello " + "socket
    ");//换行符
            pw.flush();
            //pw.close();//不能关闭 Exception in thread "main" java.net.SocketException: socket closed
            
            String result = new BufferedReader(new InputStreamReader(inputStream))
                    .readLine();
    
            System.out.println(result);
            inputStream.close();
            outputStream.close();
            
        }
    }
    package cn.example.socket;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class MyServer {
        public static void main(String[] args) throws IOException {
            //建立server
            ServerSocket serverSocket = new ServerSocket();
            //绑定Ip
            serverSocket.bind(new InetSocketAddress("localhost", 9999));
            
            //启用一个线程处理
            while(true){
                System.out.println("server start");
                new Thread(new MysocketThread(serverSocket.accept())).start();
                
            }
            
        }
    }
    
    class MysocketThread implements Runnable {
        private Socket socket;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        public MysocketThread(Socket socket) {
            super();
            this.socket = socket;
        }
    
        @Override
        public void run() {
            try {
                //定义输入输出流
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
                
                //解析读入数据
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                String param = br.readLine();
                
                //根据读入的数据处理业务逻辑,然后返回,这里简单返回
                String backValue = "ok "+param+"
    ";
                
                PrintWriter pw = new PrintWriter(outputStream);
                pw.print(backValue);
                pw.flush();
                pw.close();
                
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    outputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
    }
  • 相关阅读:
    HTTP代理简介
    解决键盘Alt+Win键功能反了的问题
    设置锁屏后关闭显示器时间
    通过ffmpeg下载在线HTTP Live Stream视频
    一款好用的磁盘占用分析工具——WizTree
    vite2 打包的时候vendorxxx.js文件过大的解决方法
    nfPress —— 在线文档也可以加载组件和编写代码
    通过UI库深入了解Vue的插槽的使用技巧
    基于 vite2 + Vue3 写一个在线帮助文档工具
    【mysql】sql查看mysql 数据库 数据表 容量大小
  • 原文地址:https://www.cnblogs.com/rocky-AGE-24/p/7010238.html
Copyright © 2020-2023  润新知