Skip to content

Commit f613810

Browse files
committed
commit Exercise_31_04
1 parent f28dbb1 commit f613810

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package exercise_31_04Client;
2+
3+
import java.io.DataInputStream;
4+
import java.io.IOException;
5+
import java.net.Socket;
6+
import javafx.application.Application;
7+
import javafx.scene.Scene;
8+
import javafx.scene.control.Label;
9+
import javafx.scene.layout.BorderPane;
10+
import javafx.stage.Stage;
11+
12+
public class Exercise_31_04Client extends Application {
13+
private Label lbl = new Label();
14+
15+
@Override
16+
public void start(Stage primaryStage) {
17+
Scene scene = new Scene(new BorderPane(lbl), 200, 50);
18+
primaryStage.setTitle("Exercise_31_04Client");
19+
primaryStage.setScene(scene);
20+
primaryStage.show();
21+
22+
try {
23+
Socket socket = new Socket("localhost", 8000);
24+
DataInputStream inputFromServer = new DataInputStream(socket.getInputStream());
25+
int count = inputFromServer.readInt();
26+
lbl.setText("You are visitor " + count);
27+
}
28+
catch (IOException ex) {
29+
ex.printStackTrace();
30+
}
31+
}
32+
33+
public static void main(String[] args) {
34+
launch(args);
35+
}
36+
37+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)