Server

mail@pastecode.io avatar
unknown
java
4 years ago
959 B
2
Indexable
Never
package chat.server;

import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    ServerSocket serverSocket;
    private Socket socket;
    private int clientNumber = 0;
    ServerThread serverThread;
    Thread thread;

    public Server() throws Exception {

        serverSocket = new ServerSocket(4444);
        System.out.println("Server started!");
        socket = serverSocket.accept();

        serverThread = new ServerThread(socket, this);
        thread = new Thread(serverThread);
        thread.start();

    }

    public int getClientNumber() {
        return ++clientNumber;
    }

    public int countWords(String message) {
        String[] result = message.split("\s");
        return result.length;
    }


    public static void main(String[] args) {
        try {
            new Server();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}