Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //СЕРВЕР СТРАНА, КЛИЕНТ ПОДОЛЕ
- package labs2.com;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.concurrent.ConcurrentHashMap;
- public class TCPServer {
- private ServerSocket server;
- private static ConcurrentHashMap<Integer, Socket> activeConnections;
- class ServerWorkerThread extends Thread {
- Socket client;
- DataInputStream reader;
- ServerWorkerThread(Socket client) throws IOException {
- this.client = client;
- reader = new DataInputStream(client.getInputStream());
- }
- @Override
- public void run() {
- // todo: Handle listening to messages
- String line = null;
- try {
- while (!(line = reader.readUTF()).isEmpty()) {
- if(line.startsWith("END:")) {
- // izvadi go END: od porakata za da se dobie ID-to
- endConnection(Integer.parseInt(line.substring(4)));
- }else {
- String[] split = line.split(":");
- String targetSubstring = split[split.length - 1];
- Integer target = Integer.parseInt(targetSubstring);
- // izvadi go Id-to od krajot na porakta + 1 za dvete tocki
- String message = line.substring(0, line.length() - (targetSubstring.length() + 1));
- // pisi na target-ot so dataoutputstream
- new DataOutputStream(getConnection(target).getOutputStream()).writeUTF(message);
- }
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- // todo: Get the required connection
- public Socket getConnection(int id) {
- return activeConnections.get(id);
- }
- // todo: Add connected client to the hash map
- void addConnection(int id, Socket connection) {
- activeConnections.put(id, connection);
- }
- synchronized void endConnection(int id){
- activeConnections.remove(id);
- }
- //todo: Initialize server
- TCPServer(int port) throws IOException {
- server = new ServerSocket(port);
- }
- // todo: Handle server listening
- // todo: For each connection, start a separate
- // todo: thread (ServerWorkerThread) to handle the communication
- void listen() throws IOException {
- Socket client;
- while(true) {
- client = server.accept();
- String firstMessage = new DataInputStream(client.getInputStream()).readUTF();
- // Izvadi go CONNECT: od prvata poraka za da se dobie id-to
- addConnection(Integer.parseInt(firstMessage.substring(8)), client);
- ServerWorkerThread workerThread = new ServerWorkerThread(client);
- workerThread.start();
- }
- }
- public static void main(String[] args) throws IOException {
- // todo: Start server
- activeConnections = new ConcurrentHashMap<>();
- TCPServer serverSocket = new TCPServer(8080);
- serverSocket.listen();
- }
- }
- // КЛИЕНТ
- package labs2.com;
- import java.io.*;
- import java.net.Socket;
- class ClientStarterWorkerThread extends Thread {
- private int ID;
- private DataInputStream inputStream;
- public ClientStarterWorkerThread(int clientID, DataInputStream inputStream) {
- this.ID = clientID;
- this.inputStream = inputStream;
- }
- @Override
- public void run() {
- // todo: Handle listening to messages
- String line = null;
- try {
- while ((line = inputStream.readUTF()) != null) {
- System.out.println("Client " + ID + " has received message: " + line);
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public class ClientStarer {
- private int ID;
- Socket socket;
- DataOutputStream writer;
- DataInputStream inputStream;
- private ClientStarer(int id, String host, int port) throws IOException {
- this.ID = id;
- // todo: Connect to server and send client ID
- socket = new Socket(host, port);
- writer = new DataOutputStream(socket.getOutputStream());
- inputStream = new DataInputStream(socket.getInputStream());
- writer.writeUTF("CONNECT:"+id);
- listen();
- }
- void sendMessage(int idReceiver, String message) throws IOException {
- writer.writeUTF(message + ":" + idReceiver);
- }
- // todo: end communication - send END to server
- private void endCommunication() throws IOException {
- writer.writeUTF("END:"+ID);
- }
- private void listen() {
- ClientStarterWorkerThread clientStarterWorkerThread = new ClientStarterWorkerThread(ID, inputStream);
- clientStarterWorkerThread.start();
- }
- public static void main(String[] args) throws IOException, InterruptedException {
- ClientStarer client1 = new ClientStarer(1, "localhost", 8080);
- ClientStarer client2 = new ClientStarer(2, "localhost", 8080);
- ClientStarer client3 = new ClientStarer(3, "localhost", 8080);
- // Simulate chat
- client1.sendMessage(2, "Hello from client 1");
- Thread.sleep(1000);
- client2.sendMessage(3, "Hello from client 2");
- Thread.sleep(1000);
- client1.sendMessage(3, "Hello from client 1");
- Thread.sleep(1000);
- client3.sendMessage(1, "Hello from client 3");
- Thread.sleep(1000);
- client3.sendMessage(2, "Hello from client 3");
- // Exit the chatroom
- client1.endCommunication();
- client2.endCommunication();
- client3.endCommunication();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement