|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?注册
x
本程序实现功能如下:
1.限制连接的客户数;
2.客户端发送数据给服务器,服务器负责将收到的信息转发到其他客户端.
//DateServer.java文件
import java.net.*;
import java.util.Date;
import java.util.*;
import java.io.*;
class Operator extends Thread {
DateServer server;
Socket sr;
OutputStream out;
PrintStream ps;
List<Socket> clients;
public Operator(DateServer _server,Socket s,List<Socket> clients ) {
this.clients=clients;
server = _server;
this.sr = s;
}
public Operator() {
}
public void run() {
try {
InputStream in = sr.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
String data = bin.readLine();
while (data != null) {
//将接收的信息转发出去
//打印接收的信息
System.out.println(data);
//继续读取接收
for(int j=0; j<clients.size(); j )//服务器将它转发其他所有的客户端
{
Socket c1 = clients.get(j);
out = c1.getOutputStream();
ps = new PrintStream(out,true);
if(!c1.equals(sr))ps.println(">>" clients.size() ":" data);
System.out.println("From Client: " " " data);
}
data = bin.readLine();
}//end of while
bin.close();
ps.close();
sr.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
server.delCount();
}
}//end of run
}// end of class Operator
public class DateServer {
ServerSocket ss;
final int MAX_COUNT = 10;
int connectCount = 0;
List <Socket> clients = new ArrayList<Socket>();
public synchronized void addCount() {
connectCount ;
System.out.println("After addCount, connectCount=" connectCount);
}
public synchronized void delCount() {
connectCount--;
System.out.println("After delCount, connectCount=" connectCount);
}
public synchronized boolean isCountExceed() {
if (connectCount > MAX_COUNT)
return true;
else
return false;
}
public static void main(String args[]) {
new DateServer().go();
}
public void go() {
// Register your service on port 5678
try {
ss = new ServerSocket(5678);
} catch (IOException e) { }
// Run the listen/accept loop forever
while (true) {
try {
while (isCountExceed()) {
try {
Thread.sleep(5000);
} catch (Exception e) {
}
}
// Wait here and listen for a connection
Socket s= ss.accept();
clients.add(s);
System.out.println("accept one client");
addCount();
Operator ms = new Operator(this,s,clients);
ms.start();
} catch (Exception e) {
e.printStackTrace();
}
} //end of while loop
} //end of method go()
} //end of class DateServer
//DateClient.java文件
import java.net.*;
import java.util.Date;
import java.io.*;
public class DateClient extends Thread{
//PrintStream OutputS;
Socket s1;
public static void main(String args[]){
PrintStream ps1;
DataInputStream KeyInput = new DataInputStream(System.in);
String sdata;
DateClient dc = new DateClient();
try {
dc.s1 = new Socket("127.0.0.1",5678);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
dc.setName(args[0]);
dc.start();
while(true)
{
try {
OutputStream out1 = dc.s1.getOutputStream();
ps1 = new PrintStream(out1,true);
sdata= KeyInput.readLine(); //以行形式读入
System.out.println("" sdata);
ps1.println(args[0] ":" sdata);
}
catch (IOException e){
return;
}
}
}
public void run() {
OutputStream out;
PrintStream ps;
try{
out = s1.getOutputStream();
ps = new PrintStream(out,true);
ps.println(getName() ":" new Date().toString() " Connected");
System.out.println("send:" getName() ":" new Date().toString());
//接收服务器转发来的消息
InputStream in = s1.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
String data = bin.readLine();
while(data != null){
System.out.println(data);
data = bin.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
} //end of method go()
} //end of class DateClient() |
|