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