// 修改过..
import java.net.*;
import java.io.*;
public class TCPServerDemo {
static int count=0;
public static final int MAX_CONNECT = 10;
static boolean bRunning = true;
public static void main(String[] args){
try{
ServerSocket ss = new ServerSocket(8003);
//boolean bRunning = true;
while(bRunning){
Socket s = ss.accept();
if (count >= MAX_CONNECT)
{
try{
PrintWriter out = new PrintWriter(s.getOutputStream());
// 超出最大用户限制
out.println("ERR_MAXUSER_EXTENDS..");
out.flush();
s.close();
}
catch(IOException e){
e.printStackTrace();
}
}
else {
if(s!=null)
count++;
new Thread( new Servicer(s)).start();
}
// 服务器程序
import java.net.*;
import java.io.IOException;
public class TCPServerDemo {
static int count=0;
public final int MAX_CONNECT = 10;
static boolean bRunning = true;
public static void main(String[] args){
try{
ServerSocket ss = new ServerSocket(8003);
//boolean bRunning = true;
while(bRunning){
Socket s = ss.accept();
synchronized(count){
if (count >= MAX_CONNECT)
{
try{
PrintWriter out = new PrintWriter(s.getOutputStream());
// 超出最大用户限制
out.println("ERR_MAXUSER_EXTENDS..");
out.flush();
s.close();
log("Connection refused to " + s.getInetAddress().getHostAddress() + ":" + s.getPort() + ": max connections reached.");
}
catch(IOException e){
e.printStackTrace();
}
}
else {
if(s!=null)
count++;
new Thread( new Servicer(s)).start();
}
}
}
ss.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
// 处理客户请求
import java.net.*;
import java.io.*;
public class Servicer implements Runnable {
private Socket s;