首页 > 其他 > 详细

处理多个请求

时间:2018-04-01 14:56:14      阅读:206      评论:0      收藏:0      [点我收藏+]
  /**
     * 连接好多的客户端
     */
    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();
}
}

处理多个请求

原文:https://www.cnblogs.com/fengdaren/p/8686576.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!