1

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, but s.shutdown(10) & s.close() not closing the socket it shows the error s.shutdown(10) is not a socket object OS [Errno 10022] an invalid argument is supplied Why its not closing the socket or is there other any way to close it.

Additional Requirement

if my socket get closed again i want to make it checked (2nd time) to run if part again to receive data from sender computer is it possible to start socket again without closing my program.

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:

  1. 1st time click on button ---> Checked & if part run [working]

  2. 2nd time click on button ---> Unchecked & else part run to close the socket [not closing]

  3. 3rd time click on button ---> again it Checked and receive data from sender computer [is it possible]

Community
  • 1
  • 1
Rahul Verma
  • 404
  • 8
  • 20
  • See [here](https://gis.stackexchange.com/questions/309811/start-stop-python-plugin-during-execution-by-just-clicking-on-plugin-button) and [here](https://gis.stackexchange.com/questions/311588/toggle-unchecked-button-is-not-closing-the-socket-qgis) – B. Shefter Mar 08 '19 at 22:06
  • @B.Shefter both questions asked by me only that's not the solution i want. – Rahul Verma Mar 09 '19 at 07:03
  • @t.m.adam please take a look here – Rahul Verma Mar 09 '19 at 20:57
  • I guess your socket is closed and you do not reconnect then again. – Michael Mar 10 '19 at 21:46
  • @Michael that i asked can i connect or disconnect as many times i want in a program. is there any way to receive data again after disconnect. – Rahul Verma Mar 10 '19 at 22:03

1 Answers1

3

Once a socket has been shut down or closed, you cannot resurect it but have to create a new one.

The general design should be:

def show_markers(self):
    scale=0
    active_socket = False

    while True:

        if self.iconAction.isChecked():
            print ('Checked')
            if not active_socket:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                host = socket.gethostbyname('192.168.225.12')
                s.connect((host, port))
                active_socket = True
            ...
        else:
            if active_socket:
                print('Unchecked going to close socket')
                s.shutdown(10)               # ??
                s.close()
                active_socket = False

I have marked the line shutdown with an interrogation, because I cannot understand what you are trying to do here. First for portability reasons, you should never use numeric values but only the constants provided by the socket module. Next shutdown is seldom used outside a graceful shutdown which is not fully implemented here.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252