Socket with if-loops does not give a consistent result
The program is continuously receiving data from the sender side. And I have a checked/unchecked button when I make it checked it runs the if part...that's good. Now I make it unchecked then else part runs and prints Unchecked going to close socket, so that's also good.
The problem is when I am going to make it again checked (2nd time) to run if part again it just print Checked and my program hangs. Why is this happening? It should receive the data but it hangs. Please tell me the solution.
def show_markers(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname('192.168.225.12')
s.connect((host, port))
scale=0
while True:
if self.iconAction.isChecked():
print ('Checked')
m = QgsVertexMarker(self.iface.mapCanvas())
data = s.recv(SIZE)
data1 = s.recv(SIZE)
c = data.decode()
d = data1.decode()
x = float(c)
y = float(d)
print("printing X :", x)
print("printing Y :", y)
rect = QgsRectangle(float(x)-scale, float(y)-scale, float(x)+scale, float(y)+scale)
me = self.iface.mapCanvas()
me.setExtent(rect)
me.refresh()
m.setCenter(QgsPointXY(x, y))
m.setColor(QColor(255, 0, 0))
m.setIconSize(7)
m.setIconType(QgsVertexMarker.ICON_X) # or ICON_CROSS, ICON_X
m.setPenWidth(3)
else:
print('Unchecked going to close socket')
s.shutdown(10)
s.close()
I am assuming it should work like:
- 1st time click on button ---> Checked & if part run [working]
- 2nd time click on button ---> Unchecked & else part run [working]
- 3rd time click on button ---> again it Checked [here it checked but my program hangs & not receiving the data from sender computer]
@rahul6612 I guess your assumption is correct. The server will wait for the responses from client and hence will hang if neither data is sent nor the connection is closed. And I think this issue should be closed by now as it's not an issue with the repo's code.