-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPClient.py
36 lines (30 loc) · 929 Bytes
/
UDPClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python
import socket
import threading
import time
import logging
UDP_IP_R = "127.0.0.1"
UDP_PORT_R = 13337
UDP_IP_S = "127.0.0.1"
UDP_PORT_S = 13338
MESSAGE = socket.gethostname()
def sender(): # client sender only sends once, on WAN might need more
print 'Sending...'
sockS = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockS.sendto(MESSAGE, (UDP_IP_S, UDP_PORT_S))
return
def listener():
print 'Listening...'
try:
sockR = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockR.bind((UDP_IP_R, UDP_PORT_R))
sockR.settimeout(5) # sets timeout in case server is off
data, addr = sockR.recvfrom(1024) # buffer size is 1024 bytes
print "Connected to Controller:", data
except socket.timeout:
print 'timeout: client exiting'
sockR.close()
s = threading.Thread(name='Sender', target=sender)
l = threading.Thread(name='Listener', target=listener)
s.start()
l.start()