-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2.py
More file actions
243 lines (198 loc) · 7.01 KB
/
Copy patha2.py
File metadata and controls
243 lines (198 loc) · 7.01 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# import socket programming library
import socket
# import thread module
import threading
clients = [] # Used to keep track of TCP connections
killServer = False # Used to Kill UDP/TCP Server
screenlock = threading.Semaphore(value=1) # Used to ensure printing works
if not hasattr(__builtins__, 'raw_input'):
raw_input = input
# thread fuction
def listenToClientTCP(connection):
global killServer
global screenlock
global clients
clientAddress = connection.getpeername()
while True:
try:
data = connection.recv(256)
data = data if hasattr(__builtins__, 'raw_input') else data.decode()
if not data.endswith('\n'): break
screenlock.acquire()
print(clientAddress, 'sent::::', data)
screenlock.release()
if data == 'hello\n': data = 'world\n'
if data == 'goodbye\n':
data = 'farwell\n'
# connection closed
screenlock.acquire()
print('sending::::', data)
screenlock.release()
data = data if hasattr(__builtins__, 'raw_input') else data.encode()
connection.send(data)
break
if data == 'exit\n':
data = 'ok\n'
screenlock.acquire()
print('sending::::', data)
screenlock.release()
data = data if hasattr(__builtins__, 'raw_input') else data.encode()
connection.send(data)
killServer = True
break
screenlock.acquire()
print('sending::::', data)
screenlock.release()
# Send the data
data = data if hasattr(__builtins__, 'raw_input') else data.encode()
connection.send(data)
except:
break
clients.remove(connection)
connection.close()
def startServerTCP(host, port):
global killServer
global clients
serverAddress = (host, port)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print('starting up on', serverAddress)
sock.bind(serverAddress)
# put the socket into listening mode
sock.listen(5)
sock.settimeout(0.2)
print("Socket is listening")
# a forever loop until client wants to exit
while not killServer:
# establish connection with client
try:
client, addr = sock.accept()
clients.append(client)
# lock acquired by client
screenlock.acquire()
print('Connected to :', addr[0], ':', addr[1])
screenlock.release()
# Start a new thread
threading.Thread(target=listenToClientTCP, args=(client,)).start()
except:
pass
for c in clients:
c.close()
sock.close()
def startClientTCP(host, port):
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
serverAddress = (host, port)
try:
sock.connect(serverAddress)
except:
print("Err... Connection to server could not be made.")
return
try:
while True:
# Send data
message = raw_input("Tpye a message to send::: ")
if len(message) > 255:
print('Message too large... not sending')
continue
message += '\n'
message_orig = message
print('sending::::', message)
try:
message = message if hasattr(__builtins__, 'raw_input') else message.encode()
status = sock.send(message)
except:
print('Failed to send message...')
status = 0
if status == 0:
print('Server disconnected... closing connection.')
break
data = sock.recv(256)
data = data if hasattr(__builtins__, 'raw_input') else data.decode()
print('received::::', data)
if message_orig == 'exit\n' or message_orig == 'goodbye\n':
print('Exiting...')
break
except:
pass
finally:
sock.close()
sockUDP = None # Used so multithreading will work with UDP
def startServerUDP(host, port):
global killServer
global sockUDP
serverAddress = (host, port)
sockUDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print('starting up on', serverAddress)
sockUDP.bind(serverAddress)
sockUDP.settimeout(0.2)
print("Socket is listening")
# a forever loop until client wants to exit
while not killServer:
try:
# establish connection with client
msg, addr = sockUDP.recvfrom(256) # 256 bytes max
# Start a new thread
threading.Thread(target=sendToClientUDP, args=(msg, addr,)).start()
except socket.timeout:
pass
except:
print("failed to recv")
sockUDP.close()
def sendToClientUDP(message, address):
global sockUDP
global killServer
try:
data = message if hasattr(__builtins__, 'raw_input') else message.decode()
if not data.endswith('\n'): return
screenlock.acquire()
print(address, 'sent::::', data)
screenlock.release()
if data == 'hello\n': data = 'world\n'
if data == 'goodbye\n':
data = 'farwell\n'
# UDP doesnt have close connection
# Not sure what they want...
if data == 'exit\n':
data = 'ok\n'
killServer = True
screenlock.acquire()
print('sending::::', data)
screenlock.release()
try:
data = data if hasattr(__builtins__, 'raw_input') else data.encode()
sockUDP.sendto(data, address)
except:
pass
except:
return
def startClientUDP(host, port):
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSocket.settimeout(5.0) # try for a second before disconnecring
addr = (host, port)
while True:
# Send data
message = raw_input("Tpye a message to send::: ")
if len(message) > 255:
print('Message too large... not sending')
continue
message += '\n'
message_orig = message
print('sending::::', message)
try:
message = message if hasattr(__builtins__, 'raw_input') else message.encode()
clientSocket.sendto(message, addr)
except:
print("Unable to send message... Exiting...")
break
try:
data, server = clientSocket.recvfrom(256)
data = data if hasattr(__builtins__, 'raw_input') else data.decode()
except:
print("Unable to receive message... Exiting...")
break
print('received::::', data)
if message_orig == 'exit\n' or message_orig == 'goodbye\n':
print('Exiting...')
break