📄 imserverframe.java
字号:
} ); setVisible(true); } public static void main(String args[]){ //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); new IMServerFrame().init(); } public void startServer() throws IOException{ server = new ServerSocket(6544,100); //所有设置已经完成,开始创建侦听套接字 chatMeg.append(Color.LIGHT_GRAY,"服务器程序已启动…………" + "\n"); //调整界面控件 startServer.setEnabled(false); stopServer.setEnabled(true); //创建在线用户信息列表 userInfoList = new UserInfoList(); //创建服务器侦听线程 listenThread = new ServerListenThread(); listenThread.sendObject(server,chatMeg, userInfoList,userOnlineList); listenThread.start(); } public void stopServer(){//实现关闭服务的功能 sendStopToAll();//向所有在线用户发送服务器关闭的消息 closeAllStream();//关闭所有的输入输出流 } public void sendStopToAll() {//向所有用户发送服务器关闭的消息 int userCount = userInfoList.getUserCount(); int testEnd = 0; while (testEnd < userCount) { SingleUserMegData closeServer = userInfoList.find( testEnd); if (closeServer == null) { testEnd++; continue; } try { //准备消息 closeServer.dataOut.writeObject("服务器关闭"); closeServer.dataOut.flush(); } catch (Exception e) { JOptionPane.showMessageDialog(this,"关闭套接字出现异常!", "系统警告",JOptionPane.ERROR_MESSAGE); } testEnd++; } } public void closeAllStream(){ try{ chatMeg.append(Color.YELLOW,"服务器已经停止…………" + "\n"); userOnlineList.removeAllItems(); userOnlineList.addItem("所有人"); int userCount = userInfoList.getUserCount();//得到在线用户个数 int testEnd = 0; while(testEnd < userCount){ SingleUserMegData singleUserMegData = userInfoList.find(testEnd); singleUserMegData.dataIn.close();//关闭输入流 singleUserMegData.dataOut.close();//关闭输出流 singleUserMegData.socket.close();//关闭套接字 testEnd++; } if(!server.isClosed()){ listenThread.isStop = true; } //调整控件 startServer.setEnabled(true); stopServer.setEnabled(false); server.close();//关闭侦听套接字 }catch(IOException e){ JOptionPane.showMessageDialog(this,"关闭套接字失败!", "系统警告",JOptionPane.ERROR_MESSAGE); } } public void sendSystemMeg(){//实现发送系统消息的功能 //得到消息发送对象 String toSomebody = ""; String message = ""; toSomebody = userOnlineList.getSelectedItem().toString(); message = systemMeg.getText() + "\n"; if(toSomebody.equalsIgnoreCase("所有人")){ sendMegToAll(message); systemMeg.setText("在此发送系统信息"); }else{//如果只发给特定用户 SingleUserMegData node = userInfoList.find(toSomebody); systemMegDataOut(node,"系统信息",message); systemMeg.setText("在此发送系统信息"); } chatMeg.append(Color.BLUE,"[系统信息]" + message); } public void sendMegToAll(String meg) {//把系统消息发送给所有人 int userCount = userInfoList.getUserCount(); int testEnd = 0; while(testEnd < userCount){ SingleUserMegData node = userInfoList.find(testEnd); if(node == null){ testEnd++; continue; } dataOut(node,"系统信息"); dataOut(node,meg); systemMeg.setText("在这里输入系统信息"); testEnd++; } } public void kickUser() {//服务器踢人 String toSomebody = ""; int kickIndex = 0; //从组合框中找到要踢出的用户 toSomebody = userOnlineList.getSelectedItem().toString(); kickIndex = userOnlineList.getSelectedIndex(); //获得所要踢出的用户的索引 SingleUserMegData delUser = userInfoList.find(toSomebody); if(kickIndex == 0){ return; }else{ //发送踢人信息 dataOut(delUser,"kicked!"); dataOut(delUser,"你被踢出了大厅!"); } serverOnlineList(delUser);//重新组合用户列表,最重要的是要关闭接收线程 sendUserListToAll();//把最新用户列表发送给所有人! } public void serverOnlineList( SingleUserMegData delUser){ String kickUserStr = "用户" + delUser.userName + "被踢!" + "\n"; int userCount = userInfoList.getUserCount(); int testEnd = 0; userOnlineList.removeAllItems(); userOnlineList.addItem("所有人"); userInfoList.delete(delUser); listenThread.recevThread.isStop = true; while(testEnd < userCount){ delUser = userInfoList.find(testEnd); if(delUser == null){ testEnd++ ; continue; } userOnlineList.addItem(delUser.userName); testEnd++; } sendMegToAll(kickUserStr);//把踢人的消息发给所有在线用户 chatMeg.append(Color.red,kickUserStr); } private void warningUser(){//警告用户 //从组合框中找到要警告的用户 String toSomebody = userOnlineList.getSelectedItem().toString(); //从用户数据中找到要警告的用户 SingleUserMegData warningUser = userInfoList.find(toSomebody); dataOut(warningUser,"系统信息");//向用户发送警告信息 dataOut(warningUser,"[严重警告]" + systemMeg.getText()+ "\n"); chatMeg.append(Color.PINK,"[警告信息]" + systemMeg.getText() + "\n"); } public void sendUserListToAll() {//像所有用户发送用户列表 String userList = ""; int userCount = userInfoList.getUserCount(); int testEnd = 0; while(testEnd < userCount){//遍历用户列表,组成用户列表串 SingleUserMegData node = userInfoList.find(testEnd); if(node == null){ testEnd++; continue; } userList += node.userName; userList += '\n'; testEnd++; } testEnd = 0; while(testEnd < userCount){//遍历链表,依次发送用户列表信息 SingleUserMegData node = userInfoList.find(testEnd); if(node == null){ testEnd++; continue; } dataOut(node,"用户列表"); testEnd++; } } public void dataOut(SingleUserMegData node,String writeObject){ //输出信息 try{ node.dataOut.writeObject(writeObject); node.dataOut.flush(); }catch(Exception e){ JOptionPane.showMessageDialog(this,"发送消息失败!!!", "系统警告",JOptionPane.ERROR_MESSAGE); } } public void systemMegDataOut(SingleUserMegData node, String writeObject,String message){ try{ node.dataOut.writeObject("系统信息"); node.dataOut.flush(); node.dataOut.writeObject(message); node.dataOut.flush(); }catch(IOException e){ JOptionPane.showMessageDialog(this,"发送系统消息失败!!", "系统警告",JOptionPane.ERROR_MESSAGE); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -