Advertisement
Guest User

TCPServer

a guest
Mar 26th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. import java.util.HashMap;
  9.  
  10. public class TCPServer {
  11.  
  12.     private ServerSocket server;
  13.     private HashMap<Integer, Socket> activeConnections;
  14.  
  15.     // todo: Get the required connection
  16.     public Socket getConnection(int id) {
  17.         return activeConnections.getOrDefault(id,null);
  18.     }
  19.  
  20.     // todo: Add connected client to the hash map
  21.     void addConnection(int id, Socket connection) {
  22.         activeConnections.putIfAbsent(id,connection);
  23.     }
  24.  
  25.     synchronized void endConnection(int id){
  26.         activeConnections.remove(id);
  27.     }
  28.  
  29.     //todo: Initialize server
  30.     TCPServer(int port) throws IOException {
  31.         server=new ServerSocket(port);
  32.         activeConnections=new HashMap<>();
  33.     }
  34.  
  35.     // todo: Handle server listening
  36.     // todo: For each connection, start a separate
  37.     // todo: thread (ServerWorkerThread) to handle the communication
  38.     void listen() throws Exception {
  39.         while(true)
  40.         {
  41.             Socket client=server.accept();
  42.             new ServerWorkerThread(client,this).start();
  43.         }
  44.     }
  45.  
  46.     public static void main(String[] args) throws Exception {
  47.         TCPServer server=new TCPServer(9876);
  48.         server.listen();
  49.     }
  50. }
  51. class ServerWorkerThread extends Thread {
  52.     private TCPServer server;
  53.     private Socket socket;
  54.     private int id;
  55.     private DataInputStream dis;
  56.     private DataOutputStream dos;
  57.  
  58.     public ServerWorkerThread(Socket socket, TCPServer server) throws Exception {
  59.         this.server = server;
  60.         this.socket = socket;
  61.         dis = new DataInputStream(socket.getInputStream());
  62.         dos = new DataOutputStream(socket.getOutputStream());
  63.         id = dis.readInt();
  64.         server.addConnection(id,socket);
  65.     }
  66.     @Override
  67.     public void run()
  68.     {
  69.         while (true)
  70.         {
  71.             try {
  72.                  String line= dis.readUTF();
  73.                  if(line.equals("END"))
  74.                  {
  75.                      server.endConnection(id);
  76.                      dos=new DataOutputStream(socket.getOutputStream());
  77.                      dos.writeUTF("END");
  78.                      dos.flush();
  79.                      break;
  80.                  }
  81.                  String lines[]=line.split(":");
  82.                  Socket connection=server.getConnection(Integer.parseInt(lines[1]));
  83.                  dos=new DataOutputStream(connection.getOutputStream());
  84.                  dos.writeUTF(lines[0]);
  85.             } catch (IOException e) {
  86.                 e.printStackTrace();
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement