-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccelerometerDataServer.py
More file actions
54 lines (42 loc) · 1.51 KB
/
accelerometerDataServer.py
File metadata and controls
54 lines (42 loc) · 1.51 KB
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
import socket
import random
import time
import pickle
#KILDE https://www.datacamp.com/tutorial/a-complete-guide-to-socket-programming-in-python
def run_server():
# create a socket object
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
port = 8080
hostname = socket.gethostname()
server_ip = socket.gethostbyname(hostname)
# bind the socket to a specific address and port
server.bind((server_ip, port))
# listen for incoming connections
server.listen(0)
print(f"Listening on {server_ip}:{port}")
## pickle server info, så client kan loade det:
serverData={"IP":server_ip, "PORT":port}
with open('accelerometerServer.pkl', 'wb') as file:
pickle.dump(serverData, file, protocol=pickle.HIGHEST_PROTOCOL)
# accept incoming connections
client_socket, client_address = server.accept()
print(f"Accepted connection from {client_address[0]}:{client_address[1]}")
# receive data from the client
while True:
try:
ax = random.random()
ay = random.random()
az = random.random()
txStr = f"{ax},{ay},{az}\n"
print("TX:",txStr[0:-1])
client_socket.send(txStr.encode())
time.sleep(0.1)
except:
client_socket.close()
print("Connection to client closed")
# close server socket
server.close()
return
while True:
run_server()