该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
服务器端
import java.net.*;
import java.io.*;
import java.util.*;
class SimpleServer
{
//定义了一个map集合(socket,用户名)
static Map clients=Collections.synchronizedMap(
//HashMap是按照hash码排列的
//改成LinkedHashMap是按照添加顺序排列的
new LinkedHashMap());
public static void main(String[] args) throws IOException
{
//ServerSoket只负责“接受”连接
ServerSocket ss=new ServerSocket(50000);
System.out.println("等待连接ing...");
while(true){
Socket socket=ss.accept();
//"用户名"来自客户端!
//每个客户端建立连接就有一个线程为其服务!
new ServerThread(socket).start();
}
}
}
class ServerThread extends Thread
{
private Socket socket;
public ServerThread(Socket socket)
{
this.socket=socket;
}
public void run()
{//Iterator client=SimpleServer.clients.iterator();
try{
InputStream input=socket.getInputStream();
BufferedReader br=new BufferedReader(
new InputStreamReader(input));
String str=null;
String username="";
//Iterator client=SimpleServer.clients.iterator();
while((str=br.readLine())!=null)//1
{
System.out.println("进入client.hasNext()!");
if(str.startsWith(MyProtocol.username)&&
str.endsWith(MyProtocol.username))
{
username=str.substring(MyProtocol.username.length(),
str.length()-MyProtocol.username.length());
//系统中已存在同名用户
PrintStream servlet_socket=new PrintStream(socket.getOutputStream());
if(SimpleServer.clients.containsValue(username))
{
servlet_socket.println(MyProtocol.nameExists);
System.out.println("存在同名用户啦");
break;
}
else{
servlet_socket.println(MyProtocol.loginSuc);
SimpleServer.clients.put(socket,username);
System.out.println("当前客户端数:"+SimpleServer.clients.size()+","+SimpleServer.clients);
//将用户列表发送给客户端。
broadcast(MyProtocol.userList+SimpleServer.clients.values()
+MyProtocol.userList);
}
}
//此处需注意!!!!不可写成if必须是if-else
else if(str.startsWith(MyProtocol.privateChat)&&
str.endsWith(MyProtocol.privateChat))
{
String privateInfo=str.substring(MyProtocol.privateChat.length(),
str.length()-MyProtocol.privateChat.length());
String[] privateInfoarray=privateInfo.split(MyProtocol.userSplit);
String fromUser=privateInfoarray[0];
String toUser=privateInfoarray[1];
String privateContent=privateInfoarray[2];
privateChat(fromUser,toUser,privateContent);
}
//普通的聊天
else{
broadcast(str);
}
}
//以下这句话永远不会出现,因为一直在等待输入。
System.out.println("进入client.hasNext()!4");
br.close();
}catch(SocketException e)
{//Connection reset
//使用socket来作为key
SimpleServer.clients.remove(socket);
//只要在线的客户端数目更改了,就要再发送一次!
broadcast(MyProtocol.userList+SimpleServer.clients.values()
+MyProtocol.userList);
System.out.println(e.getMessage());
}catch(IOException e)
{
}
}
//每当客户端数量发生变化之后,将新的客户端在线数量发送给客户端
private void broadcast(String info)
{
Iterator client=SimpleServer.clients.keySet().iterator();
while(client.hasNext())
{
System.out.println("进入client.hasNext()!2");
Socket item=client.next();
try{
OutputStream output=item.getOutputStream();
PrintStream socket_to_clients=new PrintStream(output);
System.out.println("准备向客户端发送:"+info);
//输出到个客户端对应的socket
socket_to_clients.println(info);
}catch(IOException e)
{
e.printStackTrace();
}
}
}
//私聊
private void privateChat(String fromUser,String toUser,String privateContent)
{
Iterator client=SimpleServer.clients.keySet().iterator();
while(client.hasNext())
{
Socket item=client.next();
//通过socket获取用户名
if(SimpleServer.clients.get(item).equals(toUser))
{
try{
OutputStream output=item.getOutputStream();
PrintStream socket_to_clients=new PrintStream(output);
System.out.println("准备向客户端发送:"+privateContent);
//输出到个客户端对应的socket
socket_to_clients.println(fromUser+"对你悄悄说:"+privateContent);
}catch(IOException e)
{
e.printStackTrace();
}
}
if(SimpleServer.clients.get(item).equals(fromUser))
{
try{
OutputStream output=item.getOutputStream();
PrintStream socket_to_clients=new PrintStream(output);
System.out.println("准备向客户端发送:"+privateContent);
//输出到个客户端对应的socket
socket_to_clients.println("你对"+toUser+"悄悄说:"+privateContent);
}catch(IOException e)
{
e.printStackTrace();
}
}
}
}
}