Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit780e8d0

Browse files
author
jsquared21
committed
Add Ex31_04
1 parente3b0620 commit780e8d0

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
importjava.io.*;
2+
importjava.net.*;
3+
importjava.util.Date;
4+
importjavafx.application.Application;
5+
importjavafx.scene.Scene;
6+
importjavafx.scene.layout.StackPane;
7+
importjavafx.scene.control.Label;
8+
importjavafx.stage.Stage;
9+
10+
publicclassExercise31_04ClientextendsApplication {
11+
// Input stream
12+
DataInputStreamfromServer =null;
13+
14+
// Create a label
15+
privateLabellabel =newLabel("");
16+
17+
@Override// Override the start method in the Application class
18+
publicvoidstart(StageprimaryStage) {
19+
20+
// Create a stack pane
21+
StackPanepane =newStackPane();
22+
pane.getChildren().add(label);
23+
24+
// Crate a scene and place it in the stage
25+
Scenescene =newScene(pane,300,75);
26+
primaryStage.setTitle("Exercise31_04Client");// Set the stage title
27+
primaryStage.setScene(scene);// Place the scene in the stage
28+
primaryStage.show();// Display the stage
29+
30+
try {
31+
// Create a socket to connect to the server
32+
Socketsocket =newSocket("localhost",8000);
33+
34+
// Create an input stream to receive data from the server
35+
fromServer =newDataInputStream(socket.getInputStream());
36+
37+
// Receive and display string from the server
38+
label.setText(fromServer.readUTF());
39+
}
40+
catch (IOExceptionex) {
41+
ex.printStackTrace();
42+
}
43+
}
44+
}
Binary file not shown.
Binary file not shown.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
importjava.io.*;
2+
importjava.net.*;
3+
importjava.util.Date;
4+
importjavafx.application.Application;
5+
importjavafx.application.Platform;
6+
importjavafx.scene.Scene;
7+
importjavafx.scene.control.TextArea;
8+
importjavafx.scene.control.ScrollPane;
9+
importjavafx.stage.Stage;
10+
11+
publicclassExercise31_04ServerextendsApplication {
12+
// Text area for displaying contents
13+
privateTextAreata =newTextArea();
14+
15+
// Count the thread
16+
privateintthreadNo =0;
17+
18+
@Override// Override the start method in the Application class
19+
publicvoidstart(StageprimaryStage) {
20+
// Create a scene and place it in the stage
21+
Scenescene =newScene(newScrollPane(ta),450,200);
22+
primaryStage.setTitle("Exercise31_04Sever");// Set the stage title
23+
primaryStage.setScene(scene);// Place the scene in the stage
24+
primaryStage.show();// Display the stage
25+
26+
newThread(() -> {
27+
try {
28+
// Create a server socket
29+
ServerSocketserverSocket =newServerSocket(8000);
30+
ta.appendText("Exercise31_04Sever started at " +newDate() +'\n');
31+
32+
while (true) {
33+
// Listen for a new connection request
34+
Socketsocket =serverSocket.accept();
35+
36+
Platform.runLater(() -> {
37+
// Display the thread number
38+
ta.appendText("Starting thread " +threadNo++ +'\n');
39+
40+
// Find the client's ip address
41+
InetAddressinetAddress =socket.getInetAddress();
42+
ta.appendText("Client IP /" +
43+
inetAddress.getHostAddress() +'\n');
44+
45+
});
46+
47+
// Create and start new thread for the connection
48+
newThread(newHandleAClient(socket)).start();
49+
}
50+
}
51+
catch(IOExceptionex) {
52+
System.err.println(ex);
53+
}
54+
}).start();
55+
}
56+
57+
// Define the thread class for handlind new connection
58+
classHandleAClientimplementsRunnable {
59+
privateSocketsocket;// A connected socket
60+
61+
/** Construct a thread */
62+
publicHandleAClient(Socketsocket) {
63+
this.socket =socket;
64+
}
65+
66+
/** Run a thread */
67+
publicvoidrun() {
68+
try {
69+
// Create a data output stream
70+
DataOutputStreamoutputToClient =newDataOutputStream(
71+
socket.getOutputStream());
72+
73+
// Send a string to the Client
74+
outputToClient.writeUTF("You are visitor " +threadNo);
75+
}
76+
catch (IOExceptionex) {
77+
ex.printStackTrace();
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp