?? server.java
字號:
/***************************************************
* 程序文件名稱: Server.java
* 功能:多用戶聊天室的服務器端程序
***************************************************/
import java.io.*;
import java.net.*;
import java.util.*;
public class Server extends ServerSocket
{
private static final int SERVER_PORT = 10000;
Vector vector1 =new Vector();
Vector vector2 =new Vector();
public Server() throws IOException
{
super(SERVER_PORT);
try
{
while (true)
{
Socket socket = accept();
new CreateServerThread(socket,vector1,vector2);
}
}
catch (IOException e) {}
finally
{
close();
}
}
public static void main(String[] args) throws IOException
{
new Server();
}
}
//---建立服務器端線程類 CreateServerThread
class CreateServerThread extends Thread
{
Socket client; //存放客戶端套接字socket;
DataInputStream in; //線路輸入信息;
DataOutputStream out; //線路輸出信息;
Vector vector1; //聊天室內(nèi)客戶的信息;
Vector vector2; //聊天室內(nèi)客戶的信息;
public boolean bool=false;
String ss=null;
String string=null;
int iii;
String str=null;
Enumeration enu; //存放建立連接的客戶向量對象;
public CreateServerThread(Socket s,Vector vec1,Vector vec2) throws IOException
{
client = s;
vector1=vec1;
vector2=vec2;
out=new DataOutputStream(client.getOutputStream());
in=new DataInputStream(client.getInputStream());
start();
}
public void run()
{
StringTokenizer st; //字符串分析器;
StringTokenizer stc; //字符串分析器;
try
{
while (true)
{
ss=in.readUTF();
//判斷接收的字符串前綴中是否包含有"新用戶";
//(1)如果包含有"新用戶";
if(ss.startsWith("新用戶")){
if(vector1.contains(ss) ){
out.writeUTF("該用戶名已注冊");
}
else{
out.writeUTF("可以注冊");
str=ss;
vector1.add(ss);
//獲取向量vector1的枚舉對象;
Enumeration enu=vector1.elements();
//遍歷當前散列表;
while(enu.hasMoreElements()){
out.writeUTF((String)enu.nextElement());
}
bool=true;
//獲取向量vector2的枚舉對象;
Enumeration enuc=vector2.elements();
//遍歷當前散列表;
while(enuc.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enuc.nextElement();
th.out.writeUTF(ss);
}
stc=new StringTokenizer(ss,":");
string=stc.nextToken();
string=stc.nextToken();
Enumeration enuc1=vector2.elements();
while(enuc1.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enuc1.nextElement();
th.out.writeUTF(string+"...上線了");
}
vector2.add(this);
}
}
//(2)否則是已經(jīng)注冊正在聊天室內(nèi)的客戶
else if(ss.startsWith("下線了")||client.isClosed()){
st=new StringTokenizer(ss,":");
string=st.nextToken();
string=st.nextToken();
iii=vector1.indexOf(str);
vector1.remove(iii);
Enumeration enu=vector2.elements(); //獲取向量vector2的枚舉對象;
while(enu.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enu.nextElement();
if(th!=this&&th.isAlive())
th.out.writeUTF("下線了:"+str.substring(str.indexOf(":")+1));
th.out.writeUTF(str.substring(str.indexOf(":")+1)+"...下線了");
}
vector2.remove(this);
break;
} // elseif end
// 是客戶端發(fā)來的信息,給予轉(zhuǎn)發(fā)出去。
else{
enu=vector2.elements();
while(enu.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enu.nextElement();
th.out.writeUTF(ss);
}
} // else end
} //while(true) end
} //try end
catch (IOException e)
{
try{
iii=vector1.indexOf(str);
vector1.remove(iii);
Enumeration enun=vector2.elements();
while(enun.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enun.nextElement();
if(th!=this&&th.isAlive())
th.out.writeUTF("下線了:"+str.substring(str.indexOf(":")+1));
th.out.writeUTF(str.substring(str.indexOf(":")+1)+"...下線了");
}
vector2.remove(this);
return;
}
catch(Exception ee){return;}
} // catch end
} // run() end
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -