|
| 1 | +package exercise_31_04Server; |
| 2 | + |
| 3 | +import java.io.DataOutputStream; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.RandomAccessFile; |
| 7 | +import java.net.InetAddress; |
| 8 | +import java.net.ServerSocket; |
| 9 | +import java.net.Socket; |
| 10 | +import java.util.Date; |
| 11 | +import javafx.application.Application; |
| 12 | +import javafx.application.Platform; |
| 13 | +import javafx.scene.Scene; |
| 14 | +import javafx.scene.control.ScrollPane; |
| 15 | +import javafx.scene.control.TextArea; |
| 16 | +import javafx.stage.Stage; |
| 17 | + |
| 18 | +public class Exercise_31_04Server extends Application { |
| 19 | + private TextArea taStatus = new TextArea(); |
| 20 | + private int visitorCount; |
| 21 | + private RandomAccessFile raf; |
| 22 | + |
| 23 | + @Override |
| 24 | + public void start(Stage primaryStage) { |
| 25 | + taStatus.setWrapText(true); |
| 26 | + Scene scene = new Scene(new ScrollPane(taStatus), 370, 200); |
| 27 | + primaryStage.setTitle("Exercise_31_04Server"); |
| 28 | + primaryStage.setScene(scene); |
| 29 | + primaryStage.show(); |
| 30 | + |
| 31 | + try { |
| 32 | + raf = new RandomAccessFile("visitorcount.dat", "rw"); |
| 33 | + if(raf.length() == 0) { |
| 34 | + visitorCount = 0; |
| 35 | + } |
| 36 | + else { |
| 37 | + visitorCount = raf.readInt(); |
| 38 | + } |
| 39 | + } |
| 40 | + catch (FileNotFoundException ex) { |
| 41 | + ex.printStackTrace(); |
| 42 | + } |
| 43 | + catch (IOException ex) { |
| 44 | + ex.printStackTrace(); |
| 45 | + } |
| 46 | + |
| 47 | + new Thread(() -> { |
| 48 | + try { |
| 49 | + ServerSocket serverSocket = new ServerSocket(8000); |
| 50 | + Platform.runLater(() -> taStatus.appendText("Exercise_31_04Server started at " + new Date() + "\n")); |
| 51 | + |
| 52 | + int i = 0; |
| 53 | + while(true) { |
| 54 | + int threadNumber = i; |
| 55 | + Socket socket = serverSocket.accept(); |
| 56 | + InetAddress inetAddress = socket.getInetAddress(); |
| 57 | + |
| 58 | + Platform.runLater(() -> taStatus.appendText("Starting thread " + threadNumber + |
| 59 | + "\nClient IP /" + inetAddress.getHostAddress() + "\n")); |
| 60 | + |
| 61 | + new Thread(new HandleAClient(socket)).start(); |
| 62 | + i++; |
| 63 | + } |
| 64 | + |
| 65 | + }catch (IOException ex) { |
| 66 | + ex.printStackTrace(); |
| 67 | + } |
| 68 | + }).start(); |
| 69 | + } |
| 70 | + |
| 71 | + public synchronized int modifyFile() { |
| 72 | + try { |
| 73 | + raf.seek(0); |
| 74 | + raf.writeInt(++visitorCount); |
| 75 | + } |
| 76 | + catch (IOException ex) { |
| 77 | + ex.printStackTrace(); |
| 78 | + } |
| 79 | + return visitorCount; |
| 80 | + } |
| 81 | + |
| 82 | + private class HandleAClient implements Runnable { |
| 83 | + private Socket socket; |
| 84 | + |
| 85 | + public HandleAClient(Socket socket) { |
| 86 | + this.socket = socket; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void run() { |
| 91 | + try { |
| 92 | + DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream()); |
| 93 | + int i = modifyFile(); |
| 94 | + outputToClient.writeInt(i); |
| 95 | + } |
| 96 | + catch (IOException ex) { |
| 97 | + ex.printStackTrace(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public static void main(String[] args) { |
| 103 | + launch(args); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments