Do you often convert 8 bytes of data into network byte orders?Can I?

Asked 1 years ago, Updated 1 years ago, 256 views

I am trying to send and receive structures in C language using tcp sockets.
as defined in #include<sys/stat.h> I wanted to send the structure stat as it is.

I wanted to convert structure stat to network byte order (Big Endian), so I was writing the process of converting member variables one by one.
The member variables in structure stat have variables equivalent to the long type such as mtime, atime, blksize, etc., and my machine is 64 bits, so these variables are 8 bytes.In other words, I tried to convert the byte order because I had to send 8 bytes of data.
When I looked for functions included in #include<arpa.inet.h> as a function for byte order conversion, I found only functions that convert 2 bytes and 4 bytes around htons and htonl.I can make my own and use it, but why isn't it implemented for 8 bytes?
There may be a reason why I can't communicate with the 32-bit machine, but if so, should I adjust each member variable to up to 4 bytes without converting the structure stat above?

Why is byte order conversion of 8 bytes not publicly implemented?(I can't find it if I have one)
Do 32-bit and 64-bit machines often communicate?
That's it.

I'm sorry it's not organized, but if you could tell me some common sense, I'd appreciate it.

linux c network

2022-09-30 22:05

3 Answers

I don't think the Euler would print the structure stat as it is (in binary format). According to man stat,

  • A specific memberst_dev or st_ino must exist

has been decided, but now

  • Specific size, byte offset, and endianness
  • Meaning Values
  • When will the value be updated
  • Whether or not there is an undocumented member

It has not been decided thatTherefore, the binary content of structure stat is not compatible with any OS other than

XY problem (or here).

From Comments

I was a student and it was a personal project without analysis.

Do you think case analysis is something special?Do you want to keep it in the company report database permanently with your boss's seal? No way.

The only way to analyze a case is to give words to what you want to do (not even organized in your brain at the beginning) and find what you can do to achieve it.Reading and considering the contents of the struct stat that Oira did this time is also a good analysis. If you analyze that st_ino and st_uid are valid only for this machine and have different meanings for machines across the network.So what's the point of handing it over to a machine across the network? Another case analysis.It's a specification decision = project analysis, so anyone should do it every day.


2022-09-30 22:05

Do 32-bit and 64-bit machines often communicate?

As far as OS is concerned, only desktop PCs these days are now 64bit, so it's not very common.For business and industrial PCs that have been running for a long time, there are cases where 32-bit environments remain.However, even though the OS is 64-bit, there are many programs running on it that are 32-bit.

It's not easy to use to exchange solid binaries that depend on architecture and environment, so it's better to start by selecting more generic data formats and protocols.In the first place, what kind of use do you expect?

Basically, if you don't have to send data as it is like file data, it's easier to check the contents of the data sent and received by using text data.If you log it out, you can read it as it is.

If you're already deciding what to interact with, like a measuring instrument, you can send a simple ASCII text like fixed length text + CRLF, or if you're exchanging complex data with large variability in data format, you'd better use a versatile combination like json+HTTP(S).

With widely used data formats and protocols, there are almost certainly libraries they can handle, regardless of whether the development language changes or the environment, making it overwhelmingly easier to communicate.


2022-09-30 22:05

It's almost certain that what's called a 32-bit machine or a 64-bit machine is the CPU's ability to handle it all at once. If you can't handle it with one instruction, you just have to divide it into two or three instructions, so it doesn't mean that a 32-bit machine can't handle a 64-bit width value. Although the current C language specification requires the long int type, and there is a rule that this type has a width of 64 bits or more, it can be compiled for 32-bit machines without any problems.

Whether or not it makes sense to treat it as a 64-bit value depends on individual circumstances, so it may be used if necessary. According to one study, 92% of integers in a program are 10 bits or less (which can be expressed in ) and 95% are 30 bits or less. In other words, the smaller values appear overwhelmingly. Of course, the nature of the program greatly influences the nature of the program, but in short, 32-bit is almost enough, so it's quite common for a computer to choose a 32-bit data format, even if it's a 64-bit machine.

Alternatively, variable-length encoding may be used when most of the values should be small but occasionally want to accommodate some large values. Specifically, the WebSocket payload length is assigned only 7 bits, but if the value is 126 or 127, use the next 16 or 64 bits.

Endians also have a Big Endian-based custom in establishing communication standards, but there are no restrictions on the format of data flowing through the application layer.

There can be various expressions, so which one to choose depends on the necessity. If you try to express UNIX time in 32-bit, you have 2038 problem, but if you're only going to use it for a short period of time, that's not a bad decision. What's right depends on what you need.


2022-09-30 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.