Advertisement
metalni

TCP SERVER

Jan 23rd, 2022
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4.  
  5. public class TCPServer extends Thread {
  6.  
  7.     private int port;
  8.     private String fileOutput;
  9.  
  10.     public TCPServer(int port, String fileOutput) {
  11.         this.port = port;
  12.         this.fileOutput = fileOutput;
  13.     }
  14.  
  15.     @Override
  16.     public void run() {
  17.         System.out.println("TCP is starting...");
  18.         ServerSocket serverSocket = null;
  19.  
  20.         try {
  21.             serverSocket = new ServerSocket(port);
  22.         } catch (IOException e) {
  23.             System.err.println("Socket Server failed to start.");
  24.             return;
  25.         }
  26.         System.out.println("TCP server is started.");
  27.         System.out.println("Waiting for connections...");
  28.         while (true) {
  29.             Socket socket = null;
  30.             try {
  31.                 // CREATE NEW SOCKET AND START WORKER THREAD
  32.                 socket = serverSocket.accept();
  33.                 // GIVE SOCKET AND FILE OUTPUT PATH TO WORKER THREAD
  34.                 new WorkerThread(socket, this.fileOutput).start();
  35.             } catch (IOException e) {
  36.                 e.printStackTrace();
  37.             }
  38.         }
  39.     }
  40.  
  41.     public static void main(String[] args) {
  42.         TCPServer server = new TCPServer(8888, "D:\\data.csv");
  43.         server.start();
  44.     }
  45. }
  46.  
  47.  
  48. class WorkerThread extends Thread {
  49.     private Socket socket;
  50.     private String fileOutput;
  51.  
  52.     public WorkerThread(Socket socket, String fileOutput) {
  53.         this.socket = socket;
  54.         this.fileOutput = fileOutput;
  55.     }
  56.  
  57.     @Override
  58.     public void run() {
  59.         BufferedReader reader = null;
  60.         PrintWriter writer = null;
  61.         FileWriter fileWriter = null;
  62.  
  63.         try {
  64.             reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  65.             writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
  66.  
  67.             String ipAdress = socket.getInetAddress().toString();
  68.             int port = socket.getPort();
  69.  
  70.             System.out.printf("Connected:%s:%d\n", ipAdress, port);
  71.  
  72.             // INITIALLY SEND A HELLO MESSAGE TO CLIENT
  73.             writer.println("HELLO FROM SERVER");
  74.             writer.flush();
  75.  
  76.             // MAKE THE FILE WRAPPER WITH THE FILE OUTPUT PATH
  77.             File outFile = new File(this.fileOutput);
  78.             if(!outFile.exists()) {
  79.                 outFile.createNewFile();
  80.             }
  81.  
  82.             // AFTER FILE WRAPPER IS CREATED, CREATE A FILE WRITER SO WE CAN WRITE TO THAT FILE
  83.             fileWriter = new FileWriter(outFile);
  84.  
  85.             String line = null;
  86.             // RECEIVE ALL COMMANDS FROM CLIENT IN LINE VARIABLE IN A LOOP,
  87.             // SO IF CLIENT SENDS MULTIPLE MESSAGES, SERVER RESPONDS TO ALL OF THEM
  88.             while(!(line=reader.readLine()).isEmpty()) {
  89.  
  90.                 // CLOSE CONNECTION IF CLIENT SENDS QUIT COMMAND
  91.                 if(line.equals("QUIT")) {
  92.                     System.out.printf("Connection to %s:%d closed\n", ipAdress, port);
  93.                     writer.println("CONNECTION CLOSED BY SERVER");
  94.                     writer.flush();
  95.                     break;
  96.                 }
  97.  
  98.                 String[] splitData = line.split(",");
  99.                 if (line.contains("HELLO")) {
  100.                     // SEND MESSAGE TO CLIENT ON CLIENT'S HELLO MESSAGE
  101.                     writer.println("SEND DAILY DATA");
  102.                     writer.flush();
  103.                 } else if (splitData.length == 3) {
  104.                     // WRITE THE DATA IN THE FILE THAT WE OPENED BEFORE WITH THE FILE WRITER
  105.                     fileWriter.write(line + '\n');
  106.                     fileWriter.flush();
  107.                     writer.println("OK");
  108.                     writer.flush();
  109.                 } else {
  110.                     throw new IOException("ERROR NEKOJ AMN");
  111.                 }
  112.             }
  113.         } catch (IOException e) {
  114.             e.printStackTrace();
  115.         } finally {
  116.             if (reader != null) {
  117.                 try {
  118.                     reader.close();
  119.                 } catch (IOException e) {
  120.                     e.printStackTrace();
  121.                 }
  122.             }
  123.             if (writer != null) {
  124.                 writer.close();
  125.             }
  126.             if (socket != null) {
  127.                 try {
  128.                     socket.close();
  129.                 } catch (IOException e) {
  130.                     e.printStackTrace();
  131.                 }
  132.             }
  133.             if(fileWriter != null) {
  134.                 try {
  135.                     fileWriter.close();
  136.                 } catch (IOException e) {
  137.                     e.printStackTrace();
  138.                 }
  139.             }
  140.         }
  141.     }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement