码迷,mamicode.com
首页 > 其他好文 > 详细

处理多个请求

时间:2018-04-01 14:58:34      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:ali   tst   override   catch   col   close   ring   ace   rgs   

  /**
     * 连接好多的客户端
     */
    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();
}
}

处理多个请求

标签:ali   tst   override   catch   col   close   ring   ace   rgs   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!