Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class TCPServer extends Thread {
- private int port;
- private String fileOutput;
- public TCPServer(int port, String fileOutput) {
- this.port = port;
- this.fileOutput = fileOutput;
- }
- @Override
- public void run() {
- System.out.println("TCP is starting...");
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(port);
- } catch (IOException e) {
- System.err.println("Socket Server failed to start.");
- return;
- }
- System.out.println("TCP server is started.");
- System.out.println("Waiting for connections...");
- while (true) {
- Socket socket = null;
- try {
- // CREATE NEW SOCKET AND START WORKER THREAD
- socket = serverSocket.accept();
- // GIVE SOCKET AND FILE OUTPUT PATH TO WORKER THREAD
- new WorkerThread(socket, this.fileOutput).start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- TCPServer server = new TCPServer(8888, "D:\\data.csv");
- server.start();
- }
- }
- class WorkerThread extends Thread {
- private Socket socket;
- private String fileOutput;
- public WorkerThread(Socket socket, String fileOutput) {
- this.socket = socket;
- this.fileOutput = fileOutput;
- }
- @Override
- public void run() {
- BufferedReader reader = null;
- PrintWriter writer = null;
- FileWriter fileWriter = null;
- try {
- reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
- String ipAdress = socket.getInetAddress().toString();
- int port = socket.getPort();
- System.out.printf("Connected:%s:%d\n", ipAdress, port);
- // INITIALLY SEND A HELLO MESSAGE TO CLIENT
- writer.println("HELLO FROM SERVER");
- writer.flush();
- // MAKE THE FILE WRAPPER WITH THE FILE OUTPUT PATH
- File outFile = new File(this.fileOutput);
- if(!outFile.exists()) {
- outFile.createNewFile();
- }
- // AFTER FILE WRAPPER IS CREATED, CREATE A FILE WRITER SO WE CAN WRITE TO THAT FILE
- fileWriter = new FileWriter(outFile);
- String line = null;
- // RECEIVE ALL COMMANDS FROM CLIENT IN LINE VARIABLE IN A LOOP,
- // SO IF CLIENT SENDS MULTIPLE MESSAGES, SERVER RESPONDS TO ALL OF THEM
- while(!(line=reader.readLine()).isEmpty()) {
- // CLOSE CONNECTION IF CLIENT SENDS QUIT COMMAND
- if(line.equals("QUIT")) {
- System.out.printf("Connection to %s:%d closed\n", ipAdress, port);
- writer.println("CONNECTION CLOSED BY SERVER");
- writer.flush();
- break;
- }
- String[] splitData = line.split(",");
- if (line.contains("HELLO")) {
- // SEND MESSAGE TO CLIENT ON CLIENT'S HELLO MESSAGE
- writer.println("SEND DAILY DATA");
- writer.flush();
- } else if (splitData.length == 3) {
- // WRITE THE DATA IN THE FILE THAT WE OPENED BEFORE WITH THE FILE WRITER
- fileWriter.write(line + '\n');
- fileWriter.flush();
- writer.println("OK");
- writer.flush();
- } else {
- throw new IOException("ERROR NEKOJ AMN");
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (writer != null) {
- writer.close();
- }
- if (socket != null) {
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(fileWriter != null) {
- try {
- fileWriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement