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