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

📄 chatserver.java

📁 一个很好的JAVA聊天室系统 可以在课堂里实践并且进行聊天
💻 JAVA
字号:
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.List;
import FIFO;

public class chatserver extends Thread {
  public final static int DEFAULT_PORT = 6001;
  protected int port;
  protected ServerSocket server_port;
  protected ThreadGroup CurrentConnections;
  protected List connection_list;
  protected Vector connections;
  protected ConnectionWatcher watcher;  // define watcher thread object
  protected ServerWriter writer;        // define writer  thread object 

  public chatserver () {
    super("Server");  // name this thread
    this.port = DEFAULT_PORT;
    try { server_port = new ServerSocket(port); } 
    catch (IOException e) {System.err.println(e + "Exception");}
    CurrentConnections = new ThreadGroup("ServerConnections");

    // A frame to display our current connections

    Frame f = new Frame("Server Connections");
    connection_list = new List();
    f.add("Center",connection_list);
    f.setSize(500, 200);
    f.show();

    // A vector to store our connection

    connections = new Vector();
    writer = new ServerWriter(this);               // create writer thread
                 // create watcher thread to wait for other threads to die
    watcher = new ConnectionWatcher(this, writer); 
    this.start();
  }  // end of construction

  public void run() {
    try {
         while(true) {
		Socket client_socket = server_port.accept(); 

                // this thread is a daemon thread detecting every connection request
                // and create individual thread with parameters to be responsible for
                // data communcation with every client  

           	Connection c = new Connection(client_socket, CurrentConnections,
                                               3, watcher, writer);           
   		// we store each Connection thread object in a vector which will be                                // used by thrad writer to write data in list to every chat client

		synchronized(connections) {  
			connections.addElement(c);
                        connection_list.add(c.getInfo());
			}
		} // end of while
 	} // end of try
     catch (IOException e) { System.err.println(e + "Exception"); }
  } // end of run()

  public static void main(String[] args){
    new chatserver();
  }

}   

⌨️ 快捷键说明

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