Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DBE-DS-Group1-WS24
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container 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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Robin Leber
DBE-DS-Group1-WS24
Commits
27791319
Commit
27791319
authored
1 year ago
by
Robin Leber
Browse files
Options
Downloads
Patches
Plain Diff
Add server file
parent
1ab44099
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server.py
+53
-0
53 additions, 0 deletions
server.py
with
53 additions
and
0 deletions
server.py
0 → 100644
+
53
−
0
View file @
27791319
import
socket
import
threading
def
handle_client
(
client_socket
,
client_address
):
while
True
:
try
:
# Empfange Nachricht vom Client
data
=
client_socket
.
recv
(
1024
)
if
not
data
:
break
# Sende die empfangene Nachricht an alle anderen Clients
broadcast
(
data
,
client_socket
,
client_address
)
except
:
break
# Client-Socket schließen
client_socket
.
close
()
def
broadcast
(
message
,
sender_socket
,
sender_address
):
for
client
in
clients
:
try
:
# Sende die Nachricht an alle Clients, außer an den Absender
if
client
!=
sender_socket
:
client
.
send
(
f
"
{
sender_address
}
:
{
message
}
"
.
encode
(
'
utf-8
'
))
except
:
# Entferne defekte Verbindungen
clients
.
remove
(
client
)
# Server-Konfiguration
host
=
'
127.0.0.1
'
port
=
5555
# Socket erstellen und binden
server_socket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
server_socket
.
bind
((
host
,
port
))
server_socket
.
listen
(
5
)
print
(
f
'
Server lauscht auf
{
host
}
:
{
port
}
'
)
# Liste für die verbundenen Clients
clients
=
[]
while
True
:
# Warten auf eine Verbindung
client_socket
,
client_address
=
server_socket
.
accept
()
# Neuen Thread für jeden verbundenen Client erstellen
client_thread
=
threading
.
Thread
(
target
=
handle_client
,
args
=
(
client_socket
,
client_address
))
client_thread
.
start
()
# Client-Socket zur Liste hinzufügen
clients
.
append
(
client_socket
)
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