The client program should send the following values ​​to the server:

1 байт - значение дня (1-31) первой даты, 1 байт - значение месяца (1-12) первой даты, 2 байта - unsigned short, год (0-9999), в СЕТЕВОМ порядке байтов, 1 байт - значение часов (hh, 0-23), 1 байт - значение минут (mm, 0-59), 1 байт - значение секунд (ss, 0-59), 

And we have a function to send:

 int send(int sockfd, const void *buf, int len, int flags); 

I understand that a void pointer can be cast to some other type. For example, char *. Then I can send lines with the necessary information. For example, 05/24/2014.

How to send a two-digit 24 bytes in one byte? If you send in 24 lines, then there will be 2 bytes and you need to follow the network order. Can you send characters by character? Then two bytes will be sent anyway. Or the task "meant" to send one byte for one send? Then there will be two send for date 24 and still the values ​​of the day were not sent to one byte.

And if it is a month or a day that has an insignificant zero in front, as for 05 months. Then is null also needed to be transmitted?

Another question about network order: we have host-to-network-short and host-to-network-long functions. How to check whether the value is translated correctly?

  • five
    Well, just unsigned char x = 24; - here you have one byte with value 24 ... When do you write int x = 1000000000; - do not you think by chance that x in memory takes 10 bytes? ... - Harry
  • 7
    Do you understand that char is a number? Like short , int , long , etc. Usually with a range of -128 to 127 . - HolyBlackCat 5:41 pm

1 answer 1

How to send one-byte two-digit 24?

And where in the task it says that you need to transmit one byte each ?! I am sure there is no such thing. Therefore, do not philosophize slyly, but act on the classics:

 struct buffer { unsigned char day_of_month; unsigned char month; unsigned short year; unsigned char hour: unsigned char minute; unsigned char sec } buffer; size_t ll; buffer.day_of_month = ...; . . . buffer.sec = ...; ll = send(sockfd, buffer, sizeof(buffer), 0); }