Skip to content

IPInstrument._send() could not send all its data #748

@nyxus

Description

@nyxus

I was browsing the instruments code and found out that in the IPInstrument class the socket.send(...) function is implemented wrongly:

    def _send(self, cmd):
        data = cmd + self._terminator
        self._socket.send(data.encode())

This is what the documentation says about socket.send(...):

socket.send(bytes[, flags])
Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for recv() above. Returns the number of bytes sent. Applications are responsible for checking that all data has been sent; if only some of the data was transmitted, the application needs to attempt delivery of the remaining data.
https://docs.python.org/3.6/library/socket.html

At this moment, if send(...) fails, only a part of the message is transmitted. Which will create strange bugs.

A better solution is to use socket.sendall(...) or as the example shows:

    def mysend(self, msg):
        totalsent = 0
        while totalsent < MSGLEN:
            sent = self.sock.send(msg[totalsent:])
            if sent == 0:
                raise RuntimeError("socket connection broken")
            totalsent = totalsent + sent

https://docs.python.org/3.6/howto/sockets.html#socket-howto

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions