A) Socket: file descriptor 1. purpose: implement network programming in LINUX 2. classification: a) reserved socket: use TCP protocol (SOCK_STREAM) security B) datagram socket: use UDP protocol (SOCK_DGRAM) fast c) original socket: use IP protocol :( SOCK_RAW) 3 .... a) Socket: file descriptor
1. Role: implement network programming in LINUX
2. Category:
A) reserved socket: TCP protocol (SOCK_STREAM) Security
B) datagram socket: use UDP protocol (SOCK_DGRAM) to quickly
C) original socket: use the IP protocol: (SOCK_RAW)
3. network address: struct
A) Struct sockaddr
1. {u_short sa_family; protocol used
2. Char sa_data [14];} 14 bytes specific protocol address
B) Struct sockaddr_in
1. {u_short sin_family; which protocol is used? AF_INET
2. Unsigned short int sin_port; custom port number htons (portnumber)
A) Int inet_aton (const char * cp, struct in_addr * indium)
1. Role: convert an IP address to a 32-bit IP address
B) Char * inet_ntoa (struct in_addr in)
1. function: convert a 32-bit IP address to an IP address
5. network byte order: unified big endian: low front, first transmitted
A) htons: converts the unsigned short type from the host sequence to the network sequence.
B) htonl: converts the unsigned long type from the host sequence to the network sequence.
C) ntohs: converts the unsigned short type from the network sequence to the host sequence.
D) ntohl: converts the unsigned long type from the network sequence to the host sequence.
6. IP address and host name; identifies a host in the network
A) Struct hostent * gethostbyname (const char * hostname)
B) purpose: obtain the IP address through the host name
C) Struct hostent
1. {char * h_name;: host name
2. Char * h_aliases: host alias
3. Int h_addrtype;: address type
4. Int h_length;: Address length
5. Char ** h_addr_list;}: IP address list
6. # define h_addr h_addr_list [0]: The first IP address of the host.
7. create a socket: int socket (int domain, int type, int protocol );
A) # include , # Include
B) Domain: specifies the address type used. AF_INET Ipv4 network protocol
C) Type: SOCK_STREAM provides two-way continuous and reliable data streams, namely, TCP
D) Protocol: specify the number of the transport Protocol used by the socket. generally, set this parameter to 0.
E) return value: if the request succeeds, the socket processing code is returned. if the request fails,-1 is returned.
8. bind IP address and SOCKET: int bind (int sockfd, struct sockaddr * my_addr, int addrlen );
A) # include , # Include
B) Sockfd: socket character, returned through socket
C) my_addr: Address of the network address structure
D) addrlen is the structure length of sockaddr.
E) return value: 0 is returned for success,-1 is returned for failure, and the error cause is stored in errno.
9. connect the server: int connect (int sockfd, struct sockaddr * serv_addr, int addrlen );
10. maximum connected listen: int listen (int s, int backlog );
A) S: socket character
B) Backlog: specifies the maximum connection requirements that can be processed simultaneously.
C) return value: 0 is returned for success,-1 is returned for failure, and the error cause is stored in errno.
11. waiting for request accept: int accept (int s, struct sockaddr * addr, int * addrlen );
A) # include , # Include
B) S: socket character
C) return value: return a new socket processing code. data transmission and reading in the future are processed through the new socket, and the socket of the original parameter s can continue to use accept () to accept new connection requirements. -1 is returned for failure. the error cause is stored in errno. When the connection is established successfully, the structure indicated by the addr parameter is filled with the address data of the remote host.
12. send data send: int send (int s, const void * msg, int len, unsigned int falgs );
A) # include , # Include
B) function: used to transmit data from the specified socket to the other host. The msg parameter points to the data content to be connected, and the len parameter indicates the data length. The flags parameter is generally set to 0.
C) return value: if the request succeeds, the number of actually transmitted characters is returned. if the request fails, the value-1 is returned. The error cause is stored in errno.
13. receive data recv: int recv (int s, void * buf, int len, unsigned int flags );
A) function: it is used to receive data from the remote host through the specified socket and store the data to the memory space directed by the parameter buf. the parameter len is the maximum length of the received data.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service