server.java

来自「用JAVA编译一个聊天程序,由主程序」· Java 代码 · 共 164 行

JAVA
164
字号


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; 

DataInputStream in; 
DataOutputStream out; 

Vector vector1; 
Vector vector2; 
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(); 
if(ss.startsWith("新的朋友")){ 
if(vector1.contains(str)){ iii=vector1.indexOf(str); 
vector1.remove(iii);vector2.removeElement(this);} 
if(vector1.contains(ss) ){ 
out.writeUTF("不可以"); 

} 
else{ 
out.writeUTF("可以"); 
str=ss; 
vector1.add(ss); 
Enumeration enu=vector1.elements(); 
while(enu.hasMoreElements()){ 

out.writeUTF((String)enu.nextElement()); 
} 


bool=true; 
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); 
} 
} 


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(); 
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; 


} 
else{ 

enu=vector2.elements(); 
while(enu.hasMoreElements()){ 
CreateServerThread th=(CreateServerThread)enu.nextElement(); 
th.out.writeUTF(ss); 
} 

}
}
} 
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;} 
} 
} 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?