Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Robobin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
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
plw1g21
Robobin
Commits
b628ce0c
Commit
b628ce0c
authored
5 months ago
by
Paul-Winpenny
Browse files
Options
Downloads
Patches
Plain Diff
Added graceful disconnect on the pi's side
parent
010f732d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Connectivity/SampleServerPi.py
+40
-31
40 additions, 31 deletions
Connectivity/SampleServerPi.py
with
40 additions
and
31 deletions
Connectivity/SampleServerPi.py
+
40
−
31
View file @
b628ce0c
...
...
@@ -13,46 +13,55 @@ def broadcast_presence():
while
True
:
message
=
b
"
ROBOBIN_PRESENT
"
sock
.
sendto
(
message
,
(
UDP_IP
,
UDP_PORT
))
print
(
"
Broadcasting: {}
"
.
format
(
message
.
decode
()))
time
.
sleep
(
5
)
def
handle_client_connection
(
client_socket
):
while
True
:
request
=
client_socket
.
recv
(
1024
)
if
not
request
:
break
message
=
request
.
decode
()
print
(
"
Received from client: {}
"
.
format
(
message
))
if
message
==
"
PING
"
:
print
(
"
Received PING from client.
"
)
response
=
b
"
PONG
"
client_socket
.
sendall
(
response
)
client_socket
.
close
()
print
(
"
Client disconnected.
"
)
try
:
while
True
:
request
=
client_socket
.
recv
(
1024
)
if
not
request
:
print
(
"
No request received, closing connection.
"
)
break
# Connection closed by the client
message
=
request
.
decode
()
print
(
"
Received from client: {}
"
.
format
(
message
))
if
message
==
"
PING
"
:
print
(
"
Received PING from client.
"
)
response
=
b
"
PONG
"
client_socket
.
sendall
(
response
)
except
ConnectionResetError
:
print
(
"
Client connection was forcibly closed.
"
)
except
Exception
as
e
:
print
(
f
"
An error occurred while handling the client connection:
{
e
}
"
)
finally
:
client_socket
.
close
()
print
(
"
Client disconnected.
"
)
def
listen_for_connections
():
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
sock
.
bind
((
''
,
LISTEN_PORT
))
while
True
:
data
,
addr
=
sock
.
recvfrom
(
1024
)
if
data
.
decode
()
==
"
CONNECT
"
:
print
(
"
Received connection request from {}
"
.
format
(
addr
))
# Create a TCP socket to accept connections
with
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
as
tcp_socket
:
tcp_socket
.
bind
((
''
,
5006
))
# Listen on the fixed TCP port
tcp_socket
.
listen
(
1
)
print
(
"
Listening for TCP connection...
"
)
client_socket
,
client_addr
=
tcp_socket
.
accept
()
print
(
"
Client connected from {}
"
.
format
(
client_addr
))
# Spawn a new thread for handling the client connection
threading
.
Thread
(
target
=
handle_client_connection
,
args
=
(
client_socket
,)).
start
()
try
:
data
,
addr
=
sock
.
recvfrom
(
1024
)
if
data
.
decode
()
==
"
CONNECT
"
:
print
(
"
Received connection request from {}
"
.
format
(
addr
))
# Create a TCP socket to accept connections
with
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
as
tcp_socket
:
tcp_socket
.
bind
((
''
,
5006
))
# Listen on the fixed TCP port
tcp_socket
.
listen
(
1
)
print
(
"
Listening for TCP connection...
"
)
client_socket
,
client_addr
=
tcp_socket
.
accept
()
print
(
"
Client connected from {}
"
.
format
(
client_addr
))
# Spawn a new thread for handling the client connection
threading
.
Thread
(
target
=
handle_client_connection
,
args
=
(
client_socket
,)).
start
()
except
Exception
as
e
:
print
(
f
"
An error occurred while listening for connections:
{
e
}
"
)
# Start the broadcasting and listening in separate threads
broadcast_thread
=
threading
.
Thread
(
target
=
broadcast_presence
)
...
...
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