001package var.sockets.tcp.filer;
002
003import java.io.BufferedReader;
004import java.io.File;
005import java.io.FileReader;
006import java.io.IOException;
007import java.io.PrintWriter;
008import java.net.ServerSocket;
009import java.net.Socket;
010import java.net.SocketAddress;
011
012/**
013 * threaded server for var.sockets.tcp.filer File service. waits for clients to
014 * connect. Upon connection sends concurrently to other client connections a
015 * manually defined file back to client. closes the connection directly
016 * afterwards.
017 *
018 * @author Sandro Leuchter
019 *
020 */
021public class FileServerThreaded {
022        /**
023         * path to file which will be sent to clients; relative to current working
024         * directory (e.g. project root)
025         */
026        private static final String FILE = "bin/var/sockets/tcp/filer/message.txt";
027        /**
028         * port on which this service is currently listening on localhost
029         */
030        private final int port;
031
032        /**
033         * the only constructor for this class
034         *
035         * @param port port on which this service will be listening on localhost
036         */
037        public FileServerThreaded(int port) {
038                this.port = port;
039        }
040
041        /**
042         * creates server socket on localhost:port, infinitely handles connections to
043         * clients concurrently
044         */
045        public void start() {
046                try (ServerSocket serverSocket = new ServerSocket(this.port)) {
047                        System.out.println("FileServer (threaded) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
048                        File file = new File(FILE);
049                        if (file.exists()) {
050                                System.out.println("\"" + file.getAbsolutePath() + "\" soll gesendet werden.");
051                                while (true) {
052                                        Socket socket = serverSocket.accept();
053                                        new FileThread(socket).start();
054                                }
055                        }
056                } catch (IOException e) {
057                        System.err.println(e);
058                }
059        }
060
061        /**
062         * Each connection is handled with an instance of this class.
063         */
064        private class FileThread extends Thread {
065                /**
066                 * TCP connection to client
067                 */
068                private final Socket socket;
069
070                /**
071                 * the only constructor for this class
072                 *
073                 * @param socket the individual socket that the server created on accepting a
074                 *               client that this EchoThread instance will be communicating with
075                 */
076                public FileThread(Socket socket) {
077                        this.socket = socket;
078                }
079
080                /**
081                 * defines the behavior of this Thread instance, will be executed concurrently
082                 * if start() is called on instance
083                 *
084                 */
085                @Override
086                public void run() {
087                        SocketAddress socketAddress = null;
088                        try (BufferedReader in = new BufferedReader(new FileReader(FILE));
089                                        PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true)) {
090                                socketAddress = this.socket.getRemoteSocketAddress();
091                                System.out.println("Verbindung zu  " + socketAddress + " aufgebaut");
092                                // Inhalt von in zeilenweise an out senden:
093                                System.out.println("Übertragung zu " + socketAddress + " begonnen");
094                                String input;
095                                while ((input = in.readLine()) != null) {
096                                        out.println(input);
097                                        // Thread.sleep(1000);
098                                }
099                                System.out.println("Übertragung zu " + socketAddress + " beendet");
100                        } catch (Exception e) {
101                                System.err.println(e);
102                        } finally {
103                                System.out.println("Verbindung zu  " + socketAddress + " abgebaut");
104                        }
105                }
106        }
107
108        /**
109         * main method: entrypoint to run service
110         *
111         * @param args args[0] must be the port number of the server (int); rest of args
112         *             is ignored
113         */
114        public static void main(String[] args) {
115                int port = Integer.parseInt(args[0]);
116                new FileServerThreaded(port).start();
117        }
118}