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
de1071a1
Commit
de1071a1
authored
Feb 28, 2024
by
Michele Fiori
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added comments on multi-thread
parent
2ae34c4c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
2 deletions
+23
-2
MultiServer.java
src/main/java/multithread/MultiServer.java
+8
-2
ServerThread.java
src/main/java/multithread/ServerThread.java
+15
-0
No files found.
src/main/java/multithread/MultiServer.java
View file @
de1071a1
...
...
@@ -5,16 +5,22 @@ import java.net.*;
class
MultiServer
{
public
static
void
main
(
String
argv
[])
throws
Exception
{
/* Create a "listening socket" on the specified port */
ServerSocket
welcomeSocket
=
new
ServerSocket
(
6789
);
while
(
true
)
{
/*
* Call to accept function (blocking call).
* When a new connection arrives a new "established socket" is created
*/
Socket
connectionSocket
=
welcomeSocket
.
accept
();
/
/ thread creation passing the established socket as arg
/
* Thread creation passing the established socket as argument */
ServerThread
theThread
=
new
ServerThread
(
connectionSocket
);
/
/ start of the thread
/
* Start the thread*/
theThread
.
start
();
}
}
...
...
src/main/java/multithread/ServerThread.java
View file @
de1071a1
...
...
@@ -12,11 +12,16 @@ public class ServerThread extends Thread {
public
ServerThread
(
Socket
s
)
{
connectionSocket
=
s
;
try
{
/* Initialize input stream from the socket */
inFromClient
=
new
BufferedReader
(
new
InputStreamReader
(
connectionSocket
.
getInputStream
()));
/* Initialize output stream towards the socket */
outToClient
=
new
DataOutputStream
(
connectionSocket
.
getOutputStream
());
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
...
...
@@ -26,9 +31,19 @@ public class ServerThread extends Thread {
String
clientSentence
;
String
capitalizedSentence
;
try
{
/* Read a line (ending with '\n') from the client */
clientSentence
=
inFromClient
.
readLine
();
/* simulate a processing time of 10 seconds*/
//Thread.sleep(10000);
/* Build the response */
capitalizedSentence
=
clientSentence
.
toUpperCase
()
+
'\n'
;
/* Send the response to the client */
outToClient
.
writeBytes
(
capitalizedSentence
);
/* Close the connection socket */
connectionSocket
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
...
...
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