java局域网聊天软件服务器端

  • 格式:docx
  • 大小:25.18 KB
  • 文档页数:4

服务器端程序*************支持多人聊天
import java.io.*;
import java.net.*;
importjava.util.*;

public class Server extends ServerSocket
{
static final int SERVER_PORT = 8888; //定义端口
Vector vector1 =new Vector();
Vector vector2 =new Vector();
public Server() throws IOException
{
super(SERVER_PORT);
try
{
while (true)
{
System.out.println("等待客户连接......");
Socket socket = accept();
newCreateServerThread(socket, vector1, vector2);
}
}
catch (IOException e) {System.out.println("客户连接错误");}
finally
{
close();
}
}
public static void main(String[] args) throws IOException
{
new Server();
}
}

class CreateServerThread extends Thread //---建立服务器端线程类
{
Socket socket; //存放套接字socket;
DataInputStream in; //线路输入信息;
DataOutputStream out; //线路输出信息;
Vector vector1; //存放登录聊天室内客户的信息;
Vector vector2; //存放登录聊天室内客户的信息;
publicbooleanbool=false;
String revStr=null;
String string=null;
intuserID;
String str=null;
Enumeration enu; //存放建立连接的客户向量对象;

publicCreateServerThread(Socket s,Vector vec1,Vector vec2) throws IOException
{
socket = s;
vector1=vec1;
vector2=vec2;

out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());

start();
}

public void run()
{
StringTokenizerst; //字符串分析器;
StringTokenizerstc; //字符串分析器;
try
{
while (true)
{
revStr=in.readUTF();

//判断接收的字符串前缀中是否包含有"新用户";
//(1)如果包含有"新用户";
if(revStr.startsWith("新用户"))
{
if(vector1.contains(revStr) )
{ out.writeUTF("该用户名已注册"); }
else
{
////////////////////////
System.out.println("收到客户的登录信息......"+revStr);
// out.writeUTF("正在分析客户的登录信息......");
/////////////////////////
out.writeUTF("可以注册");
str=revStr;
vector1.add(revStr);
Enumeration enu = vector1.elements();//获取向量vector1的枚举对象;
while(enu.hasMoreElements())//遍历当前哈希表;
{
out.writeUTF((String)enu.nextElement());
}
bool = true;
Enumeration enuc = vector2.elements(); //获取向量vector2的枚举对象;

while(enuc.hasMoreElements())//遍历当前哈希表;
{
CreateServerThreadth =
(CreateServerThread)enuc.nextElement();
th.out.writeUTF(revStr);
}
stc = new StringTokenizer(revStr,":");
string = stc.nextToken();
string = stc.nextToken();
Enumeration enuc1 = vector2.elements();
while(enuc1.hasMoreElements())
{
CreateServerThreadth =
(CreateServerThread)enuc1.nextElement();
th.out.writeUTF(string+"...上线了");
}
vector2.add(this);
}
}
//(2)否则是已经注册正在聊天室内的客户
else if(revStr.startsWith("下线了")||socket.isClosed()){
st=new StringTokenizer(revStr,":");
string=st.nextToken();
string=st.nextToken();
userID=vector1.indexOf(str);
vector1.remove(userID);
Enumeration enu=vector2.elements(); //获取向量vector2的枚举对象;
while(enu.hasMoreElements()){
CreateServerThreadth=(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
// 是客户端发来的信息,给予转发出去。
else{
enu=vector2.elements();
while(enu.hasMoreElements()){
CreateServerThreadth=(CreateServerThread)enu.nextElement();
th.out.writeUTF(revStr);
}
} // else end
} //while(true) end
} //try end
catch (IOException e)
{
try{
userID=vector1.indexOf(str);
vector1.remove(userID);
Enumeration enun=vector2.elements();
while(enun.hasMoreElements()){
CreateServerThreadth=(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
}