-
Notifications
You must be signed in to change notification settings - Fork 1
/
recieve_send_udp.py
55 lines (46 loc) · 1.67 KB
/
recieve_send_udp.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
import sys
from threading import Thread
from socket import *
from time import *
UDP_PORT_SENDER = 8001
UDP_PORT_LISTENER = 8000
BUFFER_SIZE = 16 # Normally 1024, but we want fast response
# /-- Hololens Server Network ---
def UDPListener():
print("Starting UDP Listener in "+gethostbyname("")+":"+str(UDP_PORT_LISTENER))
sock = socket(AF_INET,SOCK_DGRAM) # Internet,UDP
sock.bind(('', UDP_PORT_LISTENER))
global hololens_ip
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
hololens_ip = str(data) #Hololens sends his IP but it should be tested with the emulator
print (str(data.decode())+"From:"+hololens_ip)
# /-- Local Network ---
def startUDP(port):
print("Starting UDP Local Server, port:"+port)
sock = socket(AF_INET, SOCK_STREAM) # Localhost, UDP For Piano
sock_sender = socket(AF_INET,SOCK_DGRAM) # Internet,UDP For Hololens
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind(('localhost', int(port)))
sock.listen(0) # do not queue connections
client, addr = sock.accept()
while 1:
data_piano = client.recv(BUFFER_SIZE)
if 'hololens_ip' in globals():
print("Sending "+data_piano+" to:"+str(hololens_ip)+":"+str(UDP_PORT_SENDER))
sock_sender.sendto(data_piano.encode(), (hololens_ip, UDP_PORT_SENDER))
def main(argv):
if(len(argv) != 2):
print ('Sintex error, this program needs 1 arguments: recieve_send.py <port>')
sys.exit(2)
TCPconnection = False
udp_server = Thread(target=startUDP,args=(argv[1],))
udp_server.daemon = False
udp_server.start()
sleep(1)
udp_listener = Thread(target=UDPListener)
udp_listener.daemon = False
udp_listener.start()
if __name__== "__main__":
main(sys.argv)