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

    一、socket

      服务端

    public class A01server {
        public static void main(String[] args) throws IOException {
            ServerSocket serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();
    
            InputStream inputStream = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int len = inputStream.read(bytes);
            System.out.println(new String(bytes, 0, len));
    
    
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("收到谢谢".getBytes());
    
            socket.close();
            serverSocket.close();
        }
    }

      客户端

    public class A01client {
        public static void main(String[] args) throws IOException {
            Socket socket = new Socket("127.0.0.1", 8888);
    
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("你好服务器".getBytes());
    
            InputStream inputStream = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int len = inputStream.read(bytes);
            System.out.println(new String(bytes, 0, len));
    
            socket.close();
        }
    }

    二、socket文件上传案例

      服务端

    public class A02fileserver {
        public static void main(String[] args) throws IOException {
            ServerSocket serverSocket = new ServerSocket(8888);
    
            while (true) {
                Socket socket = serverSocket.accept();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            InputStream inputStream = socket.getInputStream();
                            File file = new File("upload");
                            if (!file.exists()) {
                                file.mkdirs();
                            }
                            FileOutputStream fileOutputStream = new FileOutputStream(file + "\" + System.currentTimeMillis() + ".txt");
    
                            byte[] bytes = new byte[1024];
                            int len = 0;
                            while ((len = inputStream.read(bytes)) != -1) {
                                fileOutputStream.write(bytes, 0, len);
                            }
    
                            socket.getOutputStream().write("上传成功".getBytes());
    
                            fileOutputStream.close();
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
    }

      客户端

    public class A02fileclient {
        public static void main(String[] args) throws IOException {
            FileInputStream fileInputStream = new FileInputStream("data.txt");
            Socket socket = new Socket("127.0.0.1", 8888);
            OutputStream outputStream = socket.getOutputStream();
    
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
    
            socket.shutdownOutput();//输出流结束标记
    
            InputStream inputStream = socket.getInputStream();
            while ((len = inputStream.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, len));
            }
    
            fileInputStream.close();
            socket.close();
        }
    }

    三、模拟BS服务器

    public class A03browserserver {
        public static void main(String[] args) throws IOException {
            ServerSocket serverSocket = new ServerSocket(8080);
    
            while (true) {
                Socket socket = serverSocket.accept();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            InputStream inputStream = socket.getInputStream();
                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                            String line = bufferedReader.readLine();
                            String[] arr = line.split(" ");
                            String htmlpath = arr[1].substring(1);
                            System.out.println(htmlpath);
    
                            FileInputStream fileInputStream = new FileInputStream(htmlpath);
                            OutputStream outputStream = socket.getOutputStream();
                            outputStream.write("HTTP/1.1 200 OK
    ".getBytes());
                            outputStream.write("Content-Type:text/html
    ".getBytes());
                            outputStream.write("
    ".getBytes());
    
                            byte[] bytes = new byte[1024];
                            int len = 0;
                            while ((len = fileInputStream.read(bytes)) != -1) {
                                outputStream.write(bytes, 0, len);
                            }
    
                            fileInputStream.close();
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
    }

    注意:输入流如果未读到输出流的结束标记,输入流就会处于阻塞状态。

  • 相关阅读:
    oracel中wm_concat函数使用
    plsql快捷键设置
    mybatis generator--逆向工程工具的使用
    subline_2使用格式化html等文件
    subline的使用
    关于ssm整合过程中,spring配置文件无提示功能
    初识shell脚本
    ReentrantReadWriteLock原理
    策略模式
    适配器模式
  • 原文地址:https://www.cnblogs.com/linding/p/13527896.html
Copyright © 2011-2022 走看看