• java写的服务器,处理多个请求


      /**
         * 连接好多的客户端
         */
        public void getManyCon(){
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(8889);
                while (true){
                    Socket socket = serverSocket.accept();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            new Timer().schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    System.out.println(Thread.currentThread().getName()+" "+new Date()+" "+socket.getInetAddress());
                                }
                            },5000,1000);
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    package network;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.Writer;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    
    public class CliSocketTest {
        public void test(){
            
                Socket socket = null;
                for(int i=0;i<2000;i++)
                        new Thread(new Runnable() {
                            
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                try {
                                    Socket socket = new Socket("192.168.1.12",8889);
                                } catch (UnknownHostException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                        
                    
                }
        public static void main(String[] args) {
            new CliSocketTest().test();
        }
    }
    /**
    * 连接好多的客户端
    */
    public void getManyCon(){
    ServerSocket serverSocket = null;
    try {
    serverSocket = new ServerSocket(8889);
    while (true){
    Socket socket = serverSocket.accept();
    new Thread(new Runnable() {
    @Override
    public void run() {
    new Timer().schedule(new TimerTask() {
    @Override
    public void run() {
    System.out.println(Thread.currentThread().getName()+" "+new Date()+" "+socket.getInetAddress());
    }
    },5000,1000);
    try {
    socket.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }).start();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
  • 相关阅读:
    【leetcode-100】 简单 树相关题目
    【leetcode-101】 对称二叉树
    【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树
    【leetcode-102,107,103】 二叉树的层次遍历
    iOS开发
    对称加密和不对称加密原理
    uiimageview 异步加载图片
    如何让IOS中的文本实现3D效果
    SDWebImage使用,图片加载和缓存
    ios 图片处理( 1.按比例缩放 2.指定宽度按比例缩放
  • 原文地址:https://www.cnblogs.com/fengdaren/p/8686576.html
Copyright © 2020-2023  润新知