-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageclient.py
135 lines (109 loc) · 3.85 KB
/
imageclient.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
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
import socket
import sys
import traceback
import errno
import time
from config import *
host = WIFI_IP
port = 3055
class ImageClient:
#connection to PC through socket server
def __init__(self, host= WIFI_IP, port=3055):
self.host = host
self.port = port
self.client_sock = None
self.socket = None
self.address = None
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("Socket Established")
try:
self.socket.bind(('', self.port))
except socket.error as e:
print("Bind failed", e)
sys.exit()
print("Bind completed")
self.socket.listen(3)
# receive the first message from client, know the client address
# print "ImageClient Connected"
def connect(self):
while True:
trying = False
try:
print("Waiting for connection from ImageClient...")
if self.client_sock is None:
self.client_sock, self.address = self.socket.accept()
print("Connected to ImageClient @ " + str(self.address) + "!")
trying = False
except Exception as e:
print('Error connecting with ImageClient %s' % str(e))
if self.client_sock is not None:
self.client_sock.close()
self.client_sock = None
trying = True
if not trying:
break
print("Retrying ImageClient connection")
def disconnect(self):
try:
self.socket.close()
except Exception as e:
print("ImageClient disconnection exception: %s" % str(e))
def write(self, msg):
try:
print('To ImgRec %s' %msg.encode('utf-8')) #debugging of message sent
self.client_sock.send(msg.encode('utf-8'))
except socket.error as e:
if isinstance(e.args, tuple):
print("errno is %d" % e[0])
if e[0] == errno.EPIPE:
# remote peer disconnected
print("Detected remote disconnect")
else:
# for another error
pass
else:
print("socket error ", e)
sys.exit()
except IOError as e:
print("ImageClient read exception", e)
print(traceback.format_exc())
pass
def read(self):
try:
msg = self.client_sock.recv(1024).decode()
if len(msg) >0:
print('From ImgRec: %s'%msg)
return msg
return None
except socket.error as e:
if isinstance(e.args, tuple):
print("errno is %d" % e[0])
if e[0] == errno.EPIPE:
# remote peer disconnected
print("Detected remote disconnect")
else:
# for another error
pass
else:
print("socket error ", e)
sys.exit()
except IOError as e:
print("ImageClient read exception: ", e)
print(traceback.format_exc())
pass
# if __name__ == '__main__':
# ImageClient = ImageClient()
# try:
# counter = 6
# while True:
# if counter == 0:
# ImageClient.write("s") # signal to stop
# ImageClient.write("t") # signal to take picture
# print(ImageClient.read())
# time.sleep(5)
# print("TRYING TO WRITE")
# counter -= 1
# #ImageClient.write("")
# except KeyboardInterrupt:
# print("Terminating the program now...")