-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
77 lines (56 loc) · 2.44 KB
/
Client.py
File metadata and controls
77 lines (56 loc) · 2.44 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
import socket
import random
# pickle module for data Serialisation/De-Serialisation
import pickle
import os
import sys
def main():
host = ""
try:
host = "localhost"
# host = os.environ["http://192.168.1.75:12345/"]
except KeyError:
print('Provide correct IP Address')
sys.exit(1)
# Port on which connection is to be established
port = 6000
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to server on local machine
print('TCP-Client: host=', host, 'port=', port)
s.connect((host, port))
while True:
try:
# <------------ Generate Random array to be sent to server ----------->
arr = []
# Generate random size between 10 and 20
random_size = random.randint(10, 20)
for i in range(0, random_size):
arr.append(random.randint(0, 100))
print('TCP Client: Random array to be sent to Server, Size:', str(random_size), ' Array:', str(arr))
# <------------ Send random int array to TCP-Server ----------->
# Serialise (convert) the array to byte stream before sending to server
data_stream = pickle.dumps(arr)
print('DEBUG:data_stream', data_stream)
# Send serialised byte stream to server
s.sendall(data_stream)
# Sorted accumulated byte stream received from TCP-Server
data = s.recv(1024)
print('DEBUG:data', data)
# De-Serialise (Re-convert) the byte stream into array after receiving from server
sorted_int_arr = pickle.loads(data)
print('TCP Client: Received from Server, New Size:', len(sorted_int_arr), 'New Sorted Array:', sorted_int_arr)
yes_no = input('\nClient: Do you want to send more data to Server? (y/n) : ')
if yes_no == 'y':
continue
else:
break
except:
print('TCP Client: Exception thrown while processing data to be sent to server, processing next data...')
except socket.error:
print('TCP Client: Socket Creation Failed, Check if TCP Server has been started, then start client again...')
finally:
# Close the connection to TCP-Server
s.close()
if __name__ == '__main__':
main()