How many bytes should I read/write to a socket?

testermaster

I'm having some doubts about the number of bytes I should write/read through a socket in C on Unix. I'm used to sending 1024 bytes, but this is really too much sometimes when I send short strings.

I read a string from a file, and I don't know how many bytes this string is, it can vary every time, it can be 10, 20 or 1000. I only know for sure that it's < 1024. So, when I write the code, I don't know the size of bytes to read on the client side, (on the server I can use strlen()). So, is the only solution to always read a maximum number of bytes (1024 in this case), regardless of the length of the string I read from the file?

For instance, with this code:

read(socket,stringBuff,SIZE);

wouldn't it be better if SIZE is 10 instead of 1024 if I want to read a 10 byte string?

Crowman

In the code in your question, if there are only 10 bytes to be read, then it makes no difference whether SIZE is 10 bytes, 1,024 bytes, or 1,000,024 bytes - it'll still just read 10 bytes. The only difference is how much memory you set aside for it, and if it's possible for you to receive a string up to 1,024 bytes, then you're going to have to set aside that much memory anyway.

However, regardless of how many bytes you are trying to read in, you always have to be prepared for the possibility that read() will actually read a different number of them. Particularly on a network, when you can get delays in transmission, even if your server is sending a 1,024 byte string, less than that number of bytes may have arrived by the time your client calls read(), in which case you'll read less than 1,024.

So, you always have to be prepared for the need to get your input in more than one read() call. This means you need to be able to tell when you're done reading input - you can't rely alone on the fact that read() has returned to tell you that you're done. If your server might send more than one message before you've read the first one, then you obviously can't hope to rely on this.

You have three main options:

  1. Always send messages which are the same size, perhaps padding smaller strings with zeros if necessary. This is usually suboptimal for a TCP stream. Just read until you've received exactly this number of bytes.

  2. Have some kind of sentinel mechanism for telling you when a message is over. This might be a newline character, a CRLF, a blank line, or a single dot on a line followed by a blank line, or whatever works for your protocol. Keep reading until you have received this sentinel. To avoid making inefficient system calls of one character at a time, you need to implement some kind of buffering mechanism to make this work well. If you can be sure that your server is sending you lines terminated with a single '\n' character, then using fdopen() and the standard C I/O library may be an option.

  3. Have your server tell you how big the message is (either in an initial fixed length field, or using the same kind of sentinel mechanism from point 2), and then keep reading until you've got that number of bytes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How many classes should I put in one file?

From Java

How many threads should I use in my Java program?

From Java

Spring State Machine - How many should I create?

From Dev

How do I decide how many bytes to read from an inputstream?

From Dev

How many shards should I use with Elasticsearch on a dev & CI environment?

From Dev

Admob: how many banners should I use

From Dev

How many confirmations should I have on ethereum?

From Dev

How should I send data to socket in iOS

From Dev

gRPC - How many stubs/clients should I use?

From Dev

How many principal components should I choose for PCA?

From Dev

How do I get the least significant bits of many bytes?

From Dev

How does the CPU know how many bytes it should read for the next instruction, considering instructions have different lenghts?

From Dev

How many rows should I SELECT?

From Dev

How I should parse this type of bytes?

From Dev

What are "shadow bytes" in AddressSanitizer and how should I interpret them?

From Dev

How many bytes can I send via POST / GET?

From Dev

Cores vs Threads: How many threads should I run on this machine?

From Dev

How should I set rebus up for one producer and many consumers

From Dev

Converter - How many should I use? Are there other ways in WPF with MVVM?

From Dev

How many View Controllers should I have in my game?

From Dev

How should I unit test functions with many subfunctions?

From Dev

How many CPU do I have and how many jobs should I submit?

From Dev

With how many spark nodes should I use Mesos or Yarn?

From Dev

How many cookie should I create for multiple devices?

From Dev

How to find out how many bytes in socket before recv in Python?

From Dev

How many times should I call firebase.analytics()?

From Dev

C# Background Workers - How many should I use simultaneously?

From Dev

How should I frame the data and send multiple bytes over uart?

From Dev

How many training and testing data should i use?

Related Related

  1. 1

    How many classes should I put in one file?

  2. 2

    How many threads should I use in my Java program?

  3. 3

    Spring State Machine - How many should I create?

  4. 4

    How do I decide how many bytes to read from an inputstream?

  5. 5

    How many shards should I use with Elasticsearch on a dev & CI environment?

  6. 6

    Admob: how many banners should I use

  7. 7

    How many confirmations should I have on ethereum?

  8. 8

    How should I send data to socket in iOS

  9. 9

    gRPC - How many stubs/clients should I use?

  10. 10

    How many principal components should I choose for PCA?

  11. 11

    How do I get the least significant bits of many bytes?

  12. 12

    How does the CPU know how many bytes it should read for the next instruction, considering instructions have different lenghts?

  13. 13

    How many rows should I SELECT?

  14. 14

    How I should parse this type of bytes?

  15. 15

    What are "shadow bytes" in AddressSanitizer and how should I interpret them?

  16. 16

    How many bytes can I send via POST / GET?

  17. 17

    Cores vs Threads: How many threads should I run on this machine?

  18. 18

    How should I set rebus up for one producer and many consumers

  19. 19

    Converter - How many should I use? Are there other ways in WPF with MVVM?

  20. 20

    How many View Controllers should I have in my game?

  21. 21

    How should I unit test functions with many subfunctions?

  22. 22

    How many CPU do I have and how many jobs should I submit?

  23. 23

    With how many spark nodes should I use Mesos or Yarn?

  24. 24

    How many cookie should I create for multiple devices?

  25. 25

    How to find out how many bytes in socket before recv in Python?

  26. 26

    How many times should I call firebase.analytics()?

  27. 27

    C# Background Workers - How many should I use simultaneously?

  28. 28

    How should I frame the data and send multiple bytes over uart?

  29. 29

    How many training and testing data should i use?

HotTag

Archive