john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

ChatServer

import java.net.*;
import java.io.*;

public class ChatServer {
    public static void main(String[] args) throws IOException {

        int port = 3880;
        ServerSocket serverSocket = null;
        Socket clientSocket = null;
        Slave slave = null;
        slaveOverseer mySlaveOverseer = new slaveOverseer();

        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + port + ".");
            System.exit(1);
        }

        try {
            while (true) {
                try {
                    clientSocket = serverSocket.accept();
                } catch (IOException e) {
                    System.err.println("Accept failed.");
                    System.exit(1);
                }

                slave = new Slave (clientSocket, mySlaveOverseer);      // This is the only important line.
                slave.start ();                                         // Notice both clientSocket AND mySlaveOverseer
            }                                                           // is passed in.  The slave thread should never
        } finally {                                                     // have to talk to the ChatServer again.  It can simply
            serverSocket.close();                                       // reference mySlaveOverseer
        }
    }
}

  • « Java Chat Server Beta Multithreaded v3
  • slaveOverseer »

Published

Apr 18, 2001

Category

java-chat-university

~95 words

Tags

  • chatcrap 23
  • chatserver 1