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

📄 serverchat.java~34~

📁 这是我的Java课程设计
💻 JAVA~34~
字号:
package chattingroom2;
import java.net.*;
import java.io.*;
import java.lang.Thread;
import javax.swing.*;
import chattingroom2.serverThread;
import java.util.Vector;
/**
 * <p>Title: ServerApplication for chattingroom</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author 郭尚波
 * @version 1.0
 */

public class  ServerChat extends Thread{
  static int MaxConnection=30;//连接到服务器的最大主机数
  //DataInputStream input=null; //输入流,来自客户端
  private ServerSocket serverSocket = null;
   public static int port = 8162;
   private boolean  listening = true;
   Vector clientSockets = new Vector(MaxConnection);//定义一个Vector对象数组,用来保存已建立连接的Socket对象
   static Socket tmpSkt;
   public ServerChat(JTextArea jtextArea) throws IOException {
       try {
           serverSocket = new ServerSocket(port);
       }
       catch (Exception ex) {
           JOptionPane.showMessageDialog(null,"监听端口或与客户端断开连接失败!,请重试!","系统提示消息",1);
           System.exit(-1);
       }

       //JOptionPane.showMessageDialog(null,"成功监听端口:" + port,"系统提示消息",1);
       new JOptionPane().updateUI();//刷新屏幕
       int i=0;

       while ( i<MaxConnection)
       {tmpSkt=serverSocket.accept();
         if(tmpSkt.isConnected())
          {
            addClient(jtextArea,tmpSkt);
            i++;
          }
       }

   }

   public void addClient(JTextArea jtextArea,Socket socket) throws IOException {
       new serverThread(socket, this).start();
       clientSockets.add(socket);
       send("Welcome " + socket.getInetAddress().getHostName() + " come into this ChattingRoom.\n");
       if (jtextArea.getRows()==15)//如果文本域超过10行就全部清空
       {jtextArea.removeAll();}
       jtextArea.append(socket.getInetAddress().getHostName()+"已进入\n");//将再线用户信息写到文本域中
       jtextArea.append("聊天室共有 " + clientSockets.size() + " 人");//将再线用户数目写到文本域中
   }

   public void removeClient(JTextArea jtextArea,Socket socket) throws IOException {
       send("Bybye " + socket.getInetAddress().getHostName() );
       clientSockets.remove(socket);
       jtextArea.append(socket.getInetAddress().getHostName() + "已离去");//将下线用户信息写到文本域中
       jtextArea.append("聊天室共有 " + clientSockets.size() + " 人");//将再线用户数目写到文本域中
   }

   public void send(String Info) throws IOException {
       Socket socket = null;
       for (int I = 0; I < clientSockets.size(); I++) {
           socket = (Socket)(clientSockets.get(I));
           PrintWriter out = new PrintWriter(socket.getOutputStream(), true);//获取套接字的输出流
           out.println(Info);//发送到客户端
           out.flush();
       }
   }

   public static void main(String[] args) throws IOException {
       new ServerChat(new JTextArea());
   }
}
//PrintStream output=null; //输出流,指向客户端
  /*static int MaxConnection;//连接到服务器的最大主机数
  public ServerChat(int port,int maxConnection) {
  try {
    svrSocket = new ServerSocket(port);
    int i=0;
    while(maxConnection>=0)
    {
      socket = svrSocket.accept();
      IpString[i]=socket.getInetAddress().getHostAddress();
     if( socket.isConnected())
      {
        serverThread svrThread =new serverThread(socket);
        svrThread.start();
        ServerChat.MaxConnection--;
        i++;
      }
    }
  }
  catch (IOException ex) {
    JOptionPane.showMessageDialog(null,"监听端口或与客户端建立连接失败!","系统提示消息",1);
  }
  }
  public void run(int maxConnection){

  }
  public  void Close()
  {
    try {
      svrSocket.close();
    }
    catch (IOException ex) {
      JOptionPane.showMessageDialog(null,"监听端口或与客户端断开连接失败!,请重试!","系统提示消息",1);

    }
  }
///////
  //////
  public static void main(String[] args) {
    ServerChat serverChat1 = new ServerChat(Integer.parseInt(args[0]),30);
  }

}///////////////////////////////////



 class Server {

}*/

⌨️ 快捷键说明

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