2021-04-07-Wen-Study

» study

C

htons

 //Host byte TO Network byte (Short)
#include <arpa/inet.h>
uint16_t htons(uint16 hostshort);
related(htonl, htons, ntohl, ntohs)
Changes the short memory value from host byte order to network byte order.
ex)
    #include <stdio.h>
    #include <arpa/inet.h>

    int main( void)
    {           
    short host_byte    = 0x1234;
    
    printf( "%x to %x\n", host_byte, htons( host_byte));

    return 0;
    }
    
    $ ./a.out
    1234 to 3412

recvfrom

#include <sys/types.h>
#include <sys/socket.h>
int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
    s: socket descriptor
    *buf: buffer pointer to receive data
    len: length of the buffer in bytes
    flags: option to receive
        MSG_OOB: for socket stream, can be sent by out-of-band data
        MSG_DONTROUTE: can not be routed
        MSG_DONTWAIT: able none blocking network
        MSG_NOSIGNAL: do not receive SIGPIPE signal, when connection is disabled
    *from: address info of receiving place
    *fromlen: sizeof data

*http://forum.falinux.com/zbxe/index.php?mid=C_LIB&document_srl=441147

inet_ntoa

#include <WinSock2.h>
    Convert IP addresses from a dots-and-number string to a struct in_addr and back

atoi, atof, atol

#include <stdlib.h>
atoi = char to int
atof = char to double
atol = char to long int

ex) number = 0
    1. atoi("character"): returns 0 because the character came first.

    2. atoi("number"): returns a number.

    3. atoi("number+letter"): Returns the number before the letter appears.

    4. atoi("letter+number"): Returns 0 because the character comes right out.

The principle of the atoi() function is the principle of reading the string entered as a parameter 
    from the front and converting it to a number until "space" or "non-number character" comes in.

Network

Little Edian
It is a method of writing small units to memory. 
    When performing calculations, calculations can be done easily by starting from the last digit.
    This method has the advantage that the operation is fast 
        because it is small in memory starting from a small unit (the last digit).
    Therefore, general PCs that perform a lot of calculations are 
        usually implemented in a little-endian manner.
Big Edian
It is a method of writing in memory from a large unit.
    Since the sign bit is placed in front of the highest, this method is quick to check the sign bit.
    
    In network communication, the header file is placed at the front, 
        so it is easiest to check the header file and for this reason, the big-endian method is used in network communication.

Network
    As described above, the method is divided into two due to the advantages and disadvantages, 
        which causes problems when communicating with the network.

    The transmitting side transmits in big endian, 
        but the receiving computer processes it in little endian, so the data is actually
        
    When receiving, the problem arises that the data is different from the original data 
        (because the big-endian method is read as little-endian).    

    To solve this problem, the receiving side (little endian) needs to reverse the transmitted data (big endian) and process it.
    This is implemented inside the read() and write() functions.