Socket programming

 1 struct in_addr{
 2     in_addr_t s_addr;
 3 };
 4 struct sockaddr_in{
 5     uint8_t sin_len;
 6     sa_family_t sin_family;
 7     in_port_t sin_port;
 8  
 9     struct in_addr sin_addr;
10     char sin_zero[8];
11 };
 
General socket address structure:
struct sockaddr{
    uint8_t sa_len;
    sa_family_t sa_family;
    char sa_data[14];
};
 
int bind(int ,struct sockaddr *,socklen_t);
 
strcut sockaddr_in serv;
bind(sockfd,(struct sockaddr *)&serv,sizeof(serv));

 

1 transfer process to the kernel: bind, connect, sendto
struct sockaddr_in serv;
connect(sockfd,(SA *)&serv,sizeof(serv));
2 passed to the kernel process: accept, recvfrom, getsockname, getpeername
struct sockaddr_un cli;
socklen_t len;
len = sizeof(cil);
getpeername(unixfd,(SA *)&cli,&len);

 

 
Value - result parameter:
When the function is called, the structure size is a value. When the function returns, the size of the structure is a result (I rely on! This is the philosophy I play with it?)
 
Common values ​​- result parameter:
1 select 3 variables
2 getsockopt variable length
3 recvmsg msghdr two members: msg_namelen \ msg_controllen 
4 ifconfig ifc_len
5 sysctl two length parameters before
 
Little-endian: starting address in the low-order byte is stored
Big endian
 
Verify the size endian:
 1 int main(int argc,char ** argv){
 2 union{ shot s;
 3             char c[sizeof(short)];
 4 }un;
 5 un.s = 0x0102;
 6 printf("%s:",CPU_VENDOR_OS);
 7 if(sizeof(short) ==2){
 8     if(un.c[0]==1 && un.c[1]==2)
 9         printf("big-endian\n");
10     else if(un.c[0]==2 && un.c[1]==1)
11         printf("little-endian\n");
12     else
13         printf("unknown\n");
14 }else{
15     printf("sizeof(short) = %d\n",sizeof(short));
16 }
17 exit(0);
18 }

 

inet_aton the string into a 32-bit binary value network byte order
When inet_addr all 1, an error is returned INADDR_NONE
inet_ntoa into corresponding binary decimal point
 
inet_pton expression format conversion from the format value
inet_ntop values ​​converted from the format into an expression format
 
sock_ntop 
 

Reproduced in: https: //my.oschina.net/u/204616/blog/545198

Guess you like

Origin blog.csdn.net/weixin_34301307/article/details/91989255