Server class

mail@pastecode.io avatar
unknown
java
4 years ago
846 B
2
Indexable
Never
package Server;

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

public class Server {
	
	private ServerSocket serverSocket;
	private Socket socket;
	private int clientNumber = 0;
	
	public Server() throws Exception {
		
		serverSocket = new ServerSocket(4444);
		System.out.println("Server started!");
		
		while (true) {
		
			socket = serverSocket.accept();
			ServerThread serverThread = new ServerThread(socket, this);
			Thread 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();
		}
	}
}