-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator_process.py
More file actions
57 lines (43 loc) · 1.16 KB
/
simulator_process.py
File metadata and controls
57 lines (43 loc) · 1.16 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
55
56
#!/usr/bin/python
import threading
import time
import socket
import sys
from threads import *
from threads import myThread
from threads import simulatorThread
from threads import serverThread
'''
Main Simulator Process
'''
HOST = ''
PORT = 50000
stop_event = threading.Event()
kill_event = threading.Event()
thread2 = simulatorThread(99, "simulator-thread", stop_event, kill_event)
thread2.start()
stop_event.set()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ('Socket created')
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print ('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit(1)
print ('Socket bind complete')
#Start listening on socket
s.listen(10)
print ('Socket now listening')
thread_count = 0
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print ('Connected with ' + addr[0] + ':' + str(addr[1]))
# Create new threads
server_thread = serverThread(thread_count + 1, "Thread"+str(thread_count), conn, stop_event)
thread_count = thread_count + 1
# Start new Threads
server_thread.start()
kill_event.set()
s.close()