0% found this document useful (0 votes)
4 views4 pages

CS1103 Assignment 7

The document describes the implementation of a Chat Server in Java, which listens for client connections on a specified port. It maintains a list of connected client sockets and assigns unique IDs to each client. The server continuously accepts new connections and logs when clients connect.

Uploaded by

alexcreationz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

CS1103 Assignment 7

The document describes the implementation of a Chat Server in Java, which listens for client connections on a specified port. It maintains a list of connected client sockets and assigns unique IDs to each client. The server continuously accepts new connections and logs when clients connect.

Uploaded by

alexcreationz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CS1103 Assignment 7

Implementation of a Chat Server in Java

package chat;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class ChatServer {

private ServerSocket serverSocket;

private List<Socket> clientSockets = new ArrayList<>();

private Map<Socket, Integer> clientIDs = new HashMap<>();

private int nextID = 1;

public ChatServer(int port) {

try {

serverSocket = new ServerSocket(port);

System.out.println("Chat Server started on port " + port);

} catch (IOException e) {

e.printStackTrace();
}

public void start() {

while (true) {

try {

Socket clientSocket = serverSocket.accept();

clientSockets.add(clientSocket);

clientIDs.put(clientSocket, nextID);

nextID++;

System.out.println("Client " + clientIDs.get(clientSocket) + " connected");

} catch (IOException e) {

e.printStackTrace();

public static void main(String[] args) {

ChatServer chatServer = new ChatServer(12345);

chatServer.start();

}
package chat;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class ChatServer {

private ServerSocket serverSocket;

private List<Socket> clientSockets = new ArrayList<>();

private Map<Socket, Integer> clientIDs = new HashMap<>();

private int nextID = 1;

public ChatServer(int port) {

try {

serverSocket = new ServerSocket(port);

System.out.println("Chat Server started on port " + port);

} catch (IOException e) {

e.printStackTrace();

}
}

public void start() {

while (true) {

try {

Socket clientSocket = serverSocket.accept();

clientSockets.add(clientSocket);

clientIDs.put(clientSocket, nextID);

nextID++;

System.out.println("Client " + clientIDs.get(clientSocket) + " connected");

} catch (IOException e) {

e.printStackTrace();

public static void main(String[] args) {

ChatServer chatServer = new ChatServer(12345);

chatServer.start();

You might also like