Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
Lab1-Examples
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Michele Fiori
Lab1-Examples
Commits
2ae34c4c
You need to sign in or sign up before continuing.
Commit
2ae34c4c
authored
Feb 27, 2024
by
Michele Fiori
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor changes
parent
53b573d1
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
22 additions
and
15 deletions
+22
-15
Client.class
build/classes/java/main/iterative/Client.class
+0
-0
IterativeServer.class
build/classes/java/main/iterative/IterativeServer.class
+0
-0
MultiServer.class
build/classes/java/main/multithread/MultiServer.class
+0
-0
ServerThread.class
build/classes/java/main/multithread/ServerThread.class
+0
-0
Client.java
src/main/java/iterative/Client.java
+8
-6
IterativeServer.java
src/main/java/iterative/IterativeServer.java
+14
-9
No files found.
build/classes/java/main/iterative/Client.class
View file @
2ae34c4c
No preview for this file type
build/classes/java/main/iterative/IterativeServer.class
View file @
2ae34c4c
No preview for this file type
build/classes/java/main/multithread/MultiServer.class
View file @
2ae34c4c
No preview for this file type
build/classes/java/main/multithread/ServerThread.class
View file @
2ae34c4c
No preview for this file type
src/main/java/iterative/Client.java
View file @
2ae34c4c
...
@@ -8,7 +8,7 @@ class Client {
...
@@ -8,7 +8,7 @@ class Client {
String
sentence
;
String
sentence
;
String
modifiedSentence
;
String
modifiedSentence
;
/
/ input stream initialization (from user keyboard)
/
* Initialize input stream (from user keyboard) */
BufferedReader
inFromUser
=
BufferedReader
inFromUser
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
...
@@ -17,24 +17,26 @@ class Client {
...
@@ -17,24 +17,26 @@ class Client {
6789: server service port number */
6789: server service port number */
Socket
clientSocket
=
new
Socket
(
"localhost"
,
6789
);
Socket
clientSocket
=
new
Socket
(
"localhost"
,
6789
);
/
/ output stream towards socket initialization
/
* Initialize output stream towards the socket */
DataOutputStream
outToServer
=
DataOutputStream
outToServer
=
new
DataOutputStream
(
clientSocket
.
getOutputStream
());
new
DataOutputStream
(
clientSocket
.
getOutputStream
());
/
/ input stream from socket initialization
/
* Initialize input stream from the socket */
BufferedReader
inFromServer
=
BufferedReader
inFromServer
=
new
BufferedReader
(
new
BufferedReader
(
new
InputStreamReader
(
clientSocket
.
getInputStream
()));
new
InputStreamReader
(
clientSocket
.
getInputStream
()));
/
/ read a line from the user
/
* Read an input line */
sentence
=
inFromUser
.
readLine
();
sentence
=
inFromUser
.
readLine
();
/
/ send the line to the server
/
* Send the line to the server*/
outToServer
.
writeBytes
(
sentence
+
'\n'
);
outToServer
.
writeBytes
(
sentence
+
'\n'
);
/
/ read the response from the server
/
* Read response from the server (string ending with '\n') */
modifiedSentence
=
inFromServer
.
readLine
();
modifiedSentence
=
inFromServer
.
readLine
();
System
.
out
.
println
(
"FROM SERVER: "
+
modifiedSentence
);
System
.
out
.
println
(
"FROM SERVER: "
+
modifiedSentence
);
/* Close the socket */
clientSocket
.
close
();
clientSocket
.
close
();
}
}
}
}
src/main/java/iterative/IterativeServer.java
View file @
2ae34c4c
...
@@ -8,34 +8,39 @@ class IterativeServer {
...
@@ -8,34 +8,39 @@ class IterativeServer {
String
clientSentence
;
String
clientSentence
;
String
capitalizedSentence
;
String
capitalizedSentence
;
/
/ create a "listening socket" on the specified port
/
* Create a "listening socket" on the specified port */
ServerSocket
welcomeSocket
=
new
ServerSocket
(
6789
);
ServerSocket
welcomeSocket
=
new
ServerSocket
(
6789
);
while
(
true
)
{
while
(
true
)
{
/* accept is a blocking call
/*
once a new connection arrived, it creates
* Call to accept function (blocking call).
a new "established socket" */
* When a new connection arrives a new "established socket" is created
*/
Socket
connectionSocket
=
welcomeSocket
.
accept
();
Socket
connectionSocket
=
welcomeSocket
.
accept
();
/
/ input stream from the socket initialization
/
* Initialize input stream from the socket */
BufferedReader
inFromClient
=
BufferedReader
inFromClient
=
new
BufferedReader
(
new
BufferedReader
(
new
InputStreamReader
(
connectionSocket
.
getInputStream
()));
new
InputStreamReader
(
connectionSocket
.
getInputStream
()));
/
/ output stream to the socket initialization
/
* Initialize output stream towards the socket */
DataOutputStream
outToClient
=
DataOutputStream
outToClient
=
new
DataOutputStream
(
connectionSocket
.
getOutputStream
());
new
DataOutputStream
(
connectionSocket
.
getOutputStream
());
/
/ read a line (that terminates with \n) from the client
/
* Read a line (ending with '\n') from the client */
clientSentence
=
inFromClient
.
readLine
();
clientSentence
=
inFromClient
.
readLine
();
/
/ wait for 10 seconds
/
* simulate a processing time of 10 seconds*/
//Thread.sleep(10000);
//Thread.sleep(10000);
/* Build the response */
capitalizedSentence
=
clientSentence
.
toUpperCase
()
+
'\n'
;
capitalizedSentence
=
clientSentence
.
toUpperCase
()
+
'\n'
;
/
/ send the response to the client
/
* Send the response to the client */
outToClient
.
writeBytes
(
capitalizedSentence
);
outToClient
.
writeBytes
(
capitalizedSentence
);
/* Close the connection socket */
connectionSocket
.
close
();
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment