Python: TypeError: str, bytes or bytearray expected, not int

后端 未结 1 756
我寻月下人不归
我寻月下人不归 2021-01-24 19:16

I\'m trying to create a simple server to client based chat program and the issue is that when I try to execute c.sendto(data,client) this error appears saying that

相关标签:
1条回答
  • 2021-01-24 19:49

    The problem is that you're using sendto() with a connection-mode socket. I think you want c.send(data) instead.

    Details:

    The Python docs for sendto say "The socket should not be connected to a remote socket, since the destination socket is specified by address." Also the man page for sendto says "If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET) socket, the arguments dest_addr and addrlen are ignored (and the error EISCONN may be returned when they are not NULL and 0)." I somewhat suspect that this is happening and Python is misreporting the error in a confusing way.

    The sockets interface and networking in general can be pretty confusing but basically sendto() is reserved for SOCK_DGRAM which is UDP/IP type internet traffic, which you can think of as sending letters or postcards to a recipient. Each one goes out with a recipient address on it and there's no guarantee on order of receipt. On the other hand, connection-mode sockets like SOCK_STREAM use TCP/IP which is a bit more like a phone call in that you make a connection for a certain duration and and each thing you send is delivered in order at each end.

    Since your code seems to be designed for communication over a connection I think you just want c.send(data) and not sendto.

    0 讨论(0)
提交回复
热议问题