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

📄 server.java

📁 一个用于WEB方式的文本交谈.即N对N方式交谈
💻 JAVA
字号:
package com.watch;

import java.io.*;
import java.net.*;
import java.util.Vector;


public class Server {
    static ServerSocket serverSocket = null;
    public static int port = 4444;
    public static boolean listening = true;
    Vector clientSockets = new Vector(10);
    //public static boolean isListened = false;

    public Server(OperatorApplet applet) throws IOException {
        /*
                 try {
            serverSocket = new ServerSocket(port); //监听端口
                 } catch (IOException ex) {
            System.err.println("listen fail:" + port + ":" + ex);
            throw ex;
                 }
                 System.out.println("listen:" + port);
                 while (listening) {
            System.out.println("kaishi*******");
            addClient(serverSocket.accept(), applet); //建立连接
                 }
                 serverSocket.close();
         */
        System.out.println("***new Server***");
        new listenThread(this, applet).start();
    }

    /**
     * 启动服务器线程并把客户端与服务器建立的socket添加到clientSockets中
     * @param socket Socket
     * @throws IOException
     */
    public void addClient(Socket socket, OperatorApplet applet) throws
            IOException {
        new ServerThread(socket, this, applet).start(); //启动服务器线程
        clientSockets.add(socket); //把客户端socket加入到vector对象中
    }

    /**
     * 把客户端与服务端建立的socket从clientSockets移走
     * @param socket Socket
     * @throws Exception
     */
    public void removeClient(Socket socket) throws Exception {
        System.out.println("socket断开:" + socket);
        clientSockets.remove(socket); //把客户端的socket从vector对象中移走
    }

    /**
     * 发送信息到socket客户端
     * @param msg String
     */
    public void send(String msg) {
        Socket socket = null;
        Vector tempVec = clientSockets; //clientSockets存放所有客户端socket对象信息
        int sendCount = tempVec.size(); //客户端数目
        System.out.println("sendCount...." + sendCount);
        for (int I = 0; I < sendCount; I++) {
            try {
                socket = (Socket) (tempVec.get(I));
                System.out.println("send......" + I + ":" + socket);
                PrintWriter out = null;
                out = new PrintWriter(socket.getOutputStream(), true);
                out.println(msg); //把信息写入到各个客户端
            } catch (IOException ex) {
                System.out.println("send..IOException.." + socket + "::::" + ex);
            } catch (Exception ex2) {
                System.out.println("send..Exception..");
            }
        }
    }

    public static void closeServer() {
        try {
            serverSocket.close();
        } catch (IOException ex) {
            System.out.println("closeServer...." + serverSocket);
        }
    }
    /*
      public static void main(String[] args) throws IOException {
        new Server();
      }
     */
}

⌨️ 快捷键说明

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