⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 service.java

📁 java局域网聊天室
💻 JAVA
字号:
package chatnew;
import java.net.*;
import java.awt.List;
import java.util.ArrayList;
import java.io.*;
import java.util.Hashtable;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author 乔聪
 * @version 1.0
 */
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);
                    this.chatContentList.select(this.chatContentList.getItemCount()-1);
                    ChatTookit.updateOnLineUsersList(onLineUsersList,ip2nickname);
                    //将更新信息发送给所有客户端
                    ChatTookit.sendInfoToAll(onLineUsers,this.nickname+" 将昵称改为 "+word);
                    ChatTookit.sendInfoToAll(onLineUsers,ChatTookit.getAllNickname(this.ip2nickname));
                    this.nickname=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);
                    this.chatContentList.select(this.chatContentList.getItemCount() - 1);
                }
                //是否是想对某个用户发送文件
                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);
                    this.chatContentList.select(this.chatContentList.getItemCount() - 1);
                }
                //文件传输完毕
                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+" 的文件传送完毕");
                    this.chatContentList.select(this.chatContentList.getItemCount() - 1);
                }
                else{
                    this.chatContentList.add(this.nickname+"("+this.clientIP+"):" + word);
                    this.chatContentList.select(this.chatContentList.getItemCount() - 1);
                    ChatTookit.sendInfoToAll(onLineUsers,word);
                }
            }
        } catch (Exception ex) {
            System.out.println(clientIP+"断开连接");
            //ex.printStackTrace();
        }finally{
            //更新在线用户Arraylist和ip2nickname
            this.onLineUsers.remove(client);
            this.ip2nickname.remove(ChatTookit.getIP(client));
            //更新服务器端的显示
            chatContentList.add(this.nickname+" 走了");
            this.chatContentList.select(this.chatContentList.getItemCount()-1);
            ChatTookit.updateOnLineUsersList(onLineUsersList,ip2nickname);
            try {
                //将更新信息发送给所有客户端
                ChatTookit.sendInfoToAll(onLineUsers, this.nickname + " 走了");
                ChatTookit.sendInfoToAll(onLineUsers,
                                         ChatTookit.
                                         getAllNickname(this.ip2nickname));
                client.close();
            } catch (IOException ex2) {
                System.out.println(clientIP+"断开连接时出现问题!");
            }
        }


    }

    public void changeNickname(String newName){
        this.nickname=newName;
    }
    public String getIP(){
        return this.clientIP;
    }


}

⌨️ 快捷键说明

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