-1

I want to send two packages one by one:

conn.send('package_1')
conn.send('package_2')

The question is it will be assembled in one package, because the interval of these to package is small. I don't want this to happen. So I found s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1), but it still doesn't work.

In client, it still receive a package like this package_1package_2

similar to this question

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
jan
  • 56
  • 6
  • That’s the way TCP works. It’s not message-based. It works more like writing a file. Send/recv is not 1:1. You need to build in a protocol such as sending message length or adding newlines. Buffer the bytes received and extract complete messages – Mark Tolonen Nov 03 '22 at 14:00

1 Answers1

0

That's how TCP works. TCP is not a message protocol. If you want a message protocol, you have to implement (or choose) one.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • So would you explain a little about `TCP_NODELAY` to me. I found this statement from `TCP_NODELAY If set, disable the Nagle algorithm. This means that segments are always sent as soon as possible, even if there is only a small amount of data. When not set, data is buffered until there is a sufficient amount to send out, thereby avoiding the frequent sending of small packets,.`. So does nodelay still has an interval of sending time ? – jan Nov 04 '22 at 06:00
  • @jan The implementation is free to do it however it wants to. As soon "as possible" does not mean immediately. – David Schwartz Nov 04 '22 at 16:15