service.java

来自「使用java语言写的聊天工具」· Java 代码 · 共 157 行

JAVA
157
字号
import java.net.*;
import java.awt.List;
import java.util.ArrayList;
import java.io.*;
import java.util.Hashtable;

import javax.swing.JOptionPane;

public class Service extends Thread 
{
    Socket client=null;
    List onLineUsersList=null;
    List chatContentList=null;
    ArrayList onLineUsers=null;
    ArrayList chatContent=null;
    Hashtable ip2nickname=null;
    String clientIP=null;
    String nickname=null;

    public Service(Socket client,Hashtable ip2nickname,List onLineUsersList,List chatContentList,ArrayList onLineUsers,ArrayList chatContent) 
    {
        this.client=client;
        this.chatContent=chatContent;
        this.chatContentList=chatContentList;
        this.onLineUsers=onLineUsers;
        this.onLineUsersList=onLineUsersList;
        this.ip2nickname=ip2nickname;
        //得到用户的ip地址和当前的昵称
        this.clientIP=ChatTookit.getIP(this.client);
        this.nickname=ChatTookit.getNickname(this.client,this.ip2nickname);
        this.start();
    }
    public void run()
    {
        String clientIP=client.getInetAddress().getHostAddress();
        try 
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
            String word=null;
            //接收信息,将用户发过来的信息显示在服务器端面板上,并遍历发送给所有的客户端。
            while((word=br.readLine())!=null)
            {
                //判断是否是修改昵称
                if(word.startsWith("NICKNAME:"))
                {
                    word=word.substring(9);
                    //如果用户的昵称已经被别的用户使用,提示用户重新起昵称
                    if(ip2nickname.contains(word))
                    {
                        ChatTookit.sendInfo(client,"系统管理员:该昵称已存在,请重新起新的昵称!");
                        continue;
                    }
                    this.ip2nickname.put(clientIP,word);
                    //更新服务器端的显示
                    this.chatContentList.add(this.nickname+" 将昵称改为 "+word);
                    ChatTookit.updateOnLineUsersList(onLineUsersList,ip2nickname);
                    //将更新信息发送给所有客户端
                    ChatTookit.sendInfoToAll(onLineUsers,this.nickname+" 将昵称改为 "+word);
                    ChatTookit.sendInfoToAll(onLineUsers,ChatTookit.getAllNickname(this.ip2nickname));
                    this.nickname=word;
                }
                else if(word.startsWith("FLENGTH:"))
                {
                	word=word.substring(8);
                    String toNickname=word.substring(0,word.indexOf("$SPECIAL$"));
                    word=word.substring(word.indexOf("$SPECIAL$")+9);
                    Socket client=ChatTookit.getSocketByIP(onLineUsers,ChatTookit.getIP(ip2nickname,toNickname));
                    ChatTookit.sendInfo(client,"FLENGTH:"+word);
                }
                //是否是只发送给特定用户
                else if(word.startsWith("SPECIAL:"))
                {
                    word=word.substring(8);
                    String toNickname=word.substring(0,word.indexOf("$SPECIAL$"));
                    word=word.substring(word.indexOf("$SPECIAL$")+9);
                    //根据昵称找到该客户端
                    Socket client=ChatTookit.getSocketByIP(onLineUsers,ChatTookit.getIP(ip2nickname,toNickname));
                    ChatTookit.sendInfo(client,this.nickname+" 悄悄对你说:"+word);
//                    this.chatContent.add(this.nickname+" 对 "+toNickname+" 悄悄的说:"+word);
                }
                //是否是想对某个用户发送文件
                else if(word.startsWith("FILE:"))
                {
                    word=word.substring(5);
                    String toNickname=word.substring(0,word.indexOf("$FILE$"));
                    word=word.substring(word.indexOf("$FILE$")+6);
                    //根据昵称找到该客户端
                    Socket clientD=ChatTookit.getSocketByIP(onLineUsers,ChatTookit.getIP(ip2nickname,toNickname));
                    ChatTookit.sendInfo(clientD,this.nickname+" 想发送文件 "+word+" 给你,请点“保存”按钮选择要保存的位置,再点“开始”按钮开始接收文件");
                    //将目标用户的ip地址发送给原用户
                    ChatTookit.sendInfo(this.client,"FILEECHO:"+clientD.getInetAddress().getHostAddress());
//                    this.chatContent.add(this.nickname+" 想给 "+toNickname+" 发送文件 "+word);
                }
                else if(word.startsWith("VOICE:"))
                {
                	String toNickname=word.substring(6);
                	Socket clientD=ChatTookit.getSocketByIP(onLineUsers,ChatTookit.getIP(ip2nickname,toNickname));
                    ChatTookit.sendInfo(clientD,this.nickname+"想和你语音聊天!");
                    ChatTookit.sendInfo(clientD,"VOICE:"+this.client.getInetAddress().getHostAddress());
                    ChatTookit.sendInfo(this.client,"VOICEECHO:"+clientD.getInetAddress().getHostAddress());
                }
                else if(word.startsWith("VOICEEND:"))
                {
                	String toNickname=word.substring(9);
                	Socket clientD=ChatTookit.getSocketByIP(onLineUsers,ChatTookit.getIP(ip2nickname,toNickname));
                	ChatTookit.sendInfo(clientD,"VOICEEND:");
                    //ChatTookit.sendInfo(this.client,"VOICEECHO:"+clientD.getInetAddress().getHostAddress());
                }
                //文件传输完毕
                else if(word.startsWith("FILEFINISH:"))
                {
                    String toNickname=word.substring(11);
                    Socket clientD=ChatTookit.getSocketByIP(onLineUsers,ChatTookit.getIP(ip2nickname,toNickname));
                    ChatTookit.sendInfo(clientD,"FILEFINISH:"+this.nickname);
 //                   this.chatContent.add(this.nickname+" 给 "+toNickname+" 的文件传送完毕");
                }
                else
                {
                    this.chatContentList.add(this.nickname+"("+this.clientIP+"):" + word);
                    ChatTookit.sendInfoToAll(onLineUsers,word);
                }
            }
        } 
        catch (Exception ex) 
        {
        	JOptionPane.showMessageDialog(null, clientIP+"断开连接", "提示",JOptionPane.INFORMATION_MESSAGE);
        }
        finally
        {
            //更新在线用户Arraylist和ip2nickname
            this.onLineUsers.remove(client);
            this.ip2nickname.remove(ChatTookit.getIP(client));
            //更新服务器端的显示
            chatContentList.add(this.nickname+" 走了");
            ChatTookit.updateOnLineUsersList(onLineUsersList,ip2nickname);
            try 
            {
                //将更新信息发送给所有客户端
                ChatTookit.sendInfoToAll(onLineUsers, this.nickname + " 走了");
                ChatTookit.sendInfoToAll(onLineUsers,ChatTookit.getAllNickname(this.ip2nickname));
                client.close();
            } 
            catch (IOException ex2) 
            {
            }
        }
    }
    public void changeNickname(String newName)
    {
        this.nickname=newName;
    }
    public String getIP()
    {
        return this.clientIP;
    }
}

⌨️ 快捷键说明

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