Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
se3sose2022vorlesung
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jordine Tobias Dr.
se3sose2022vorlesung
Commits
3e141a91
Commit
3e141a91
authored
2 years ago
by
Jordine Tobias Dr.
Browse files
Options
Downloads
Patches
Plain Diff
added client server demo
parent
44122bc9
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/EchoServer.java
+44
-0
44 additions, 0 deletions
src/main/java/EchoServer.java
src/main/java/GreetClient.java
+47
-0
47 additions, 0 deletions
src/main/java/GreetClient.java
with
91 additions
and
0 deletions
src/main/java/EchoServer.java
0 → 100644
+
44
−
0
View file @
3e141a91
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.PrintWriter
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
// Adapted from https://www.baeldung.com/a-guide-to-java-sockets
public
class
EchoServer
{
private
ServerSocket
serverSocket
;
private
Socket
clientSocket
;
private
PrintWriter
out
;
private
BufferedReader
in
;
public
void
start
(
int
port
)
throws
IOException
{
serverSocket
=
new
ServerSocket
(
port
);
clientSocket
=
serverSocket
.
accept
();
out
=
new
PrintWriter
(
clientSocket
.
getOutputStream
(),
true
);
in
=
new
BufferedReader
(
new
InputStreamReader
(
clientSocket
.
getInputStream
()));
String
inputLine
;
while
((
inputLine
=
in
.
readLine
())
!=
null
)
{
if
(
"."
.
equals
(
inputLine
))
{
out
.
println
(
"good bye"
);
break
;
}
out
.
println
(
inputLine
.
toUpperCase
());
}
}
public
void
stop
()
throws
IOException
{
in
.
close
();
out
.
close
();
clientSocket
.
close
();
serverSocket
.
close
();
}
public
static
void
main
(
String
[]
args
)
throws
IOException
{
EchoServer
server
=
new
EchoServer
();
server
.
start
(
9191
);
}
}
This diff is collapsed.
Click to expand it.
src/main/java/GreetClient.java
0 → 100644
+
47
−
0
View file @
3e141a91
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.PrintWriter
;
import
java.net.Socket
;
import
java.util.Scanner
;
public
class
GreetClient
{
private
Socket
clientSocket
;
private
PrintWriter
out
;
private
BufferedReader
in
;
public
void
startConnection
(
String
ip
,
int
port
)
throws
IOException
{
clientSocket
=
new
Socket
(
ip
,
port
);
out
=
new
PrintWriter
(
clientSocket
.
getOutputStream
(),
true
);
in
=
new
BufferedReader
(
new
InputStreamReader
(
clientSocket
.
getInputStream
()));
}
public
String
sendMessage
(
String
msg
)
throws
IOException
{
out
.
println
(
msg
);
String
resp
=
in
.
readLine
();
return
resp
;
}
public
void
stopConnection
()
throws
IOException
{
in
.
close
();
out
.
close
();
clientSocket
.
close
();
}
public
static
void
main
(
String
[]
args
)
throws
IOException
{
GreetClient
client
=
new
GreetClient
();
Scanner
in
=
new
Scanner
(
System
.
in
);
client
.
startConnection
(
"127.0.0.1"
,
9191
);
while
(
true
){
String
text
=
in
.
nextLine
();
String
response
=
client
.
sendMessage
(
text
);
System
.
out
.
println
(
"Server Response: "
+
response
);
if
(
text
.
equals
(
"."
)){
break
;
}
}
client
.
stopConnection
();
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment