zoukankan      html  css  js  c++  java
  • Java Socket 网络编程

    1. 简单的由服务端发送内容到客户端:

    Server:

    package test;
    
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    public class SocketServer {
     
        private static final int PORT = 8088;
     
        public static void main(String[] args) {
            ServerSocket server = null;
            try {
                server = new ServerSocket(PORT);
                while (true) {
                    Socket client = server.accept();
                    new Thread(new Server(client)).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
    }
     
    class Server implements Runnable {
     
        private Socket client;
     
        public Server(Socket client) {
            this.client = client;
        }
     
        public void run() {
            DataOutputStream output = null;
            try {
                output = new DataOutputStream(client.getOutputStream());
                output.writeUTF("XXOOXXOOXXOO");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (output != null) output.close();
                    output = null;
                } catch (IOException e) {}
            }
        }
     
    }

    client:

    package test;
    
    import java.io.*;
    import java.net.Socket;
    import java.net.UnknownHostException;
     
    public class SSLClient extends Socket {
     
        private static final int PORT = 8088;
     
        public static void main(String[] args) {
            Socket socket = null;
            try {
                socket = new Socket("localhost", PORT);
                DataInputStream in = new DataInputStream(socket.getInputStream());
                String res = in.readUTF();
                System.out.println(res);
                if (in != null) in.close();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {}
                }
            }
        }
    }

    2. server 和 client 沟通通讯:

    server:

    package test;
    
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    public class SocketServer {
     
        private static final int PORT = 8088;
     
        public static void main(String[] args) {
    //        ServerSocket server = null;
            try {
                ServerSocket server = new ServerSocket(PORT);            
                Socket socket = server.accept();
    //            new Thread(new Server(socket)).start();
    
                InputStream msg = socket.getInputStream();
                InputStreamReader read = new InputStreamReader(msg);
                BufferedReader br = new BufferedReader(read);
                String info = null;
                
                while ((info = br.readLine())!= null) {
                    System.out.println("I am server, the client says: " + info);
                }
                socket.shutdownInput();
                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);
                pw.write("Welcome!");
                pw.flush();
                pw.close();
                os.close();
                br.close();
                read.close();
                msg.close();
                socket.close();            
                
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
    }

    client:

    package test;
    
    import java.io.*;
    import java.net.Socket;
    import java.net.UnknownHostException;
     
    public class SSLClient extends Socket {
     
        private static final int PORT = 8088;
     
        public static void main(String[] args) {
            Socket socket = null;
            try {
                socket = new Socket("localhost", PORT);
                
                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);
                
                pw.write("user: admin; passwd: 123");
                pw.flush();
                socket.shutdownOutput();
                InputStream is = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String info = null;
                
                while((info=br.readLine()) != null){
                    System.out.println("I am client, server says: " + info);
                }
                
                br.close();
                is.close();
                pw.close();
                os.close();
                socket.close();
                
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {}
                }
            }
        }
    }
  • 相关阅读:
    IDEA热部署插件Jrebel
    Navicat Premium15安装及破解教程
    IDEA中查看类的关系图
    PV、UV、IP名词解释
    Promise由浅入深
    URLSearchParams
    二进制流学习-Blob、ArrayBuffer、File、FileReader和FormData的区别
    前端vue以数据流方式导出word----借助 jquery
    js中 == 、=== 和 Object.is() 的区别
    后端传的是二进制流,前端应该如何通过blob处理二进制文件流格式流,并实现前端下载文件流格式
  • 原文地址:https://www.cnblogs.com/mmzhang/p/8001669.html
Copyright © 2011-2022 走看看