[Reserved] struct sockaddr_in other data type definition programming Socket

Original link: http://www.cnblogs.com/Leon5/archive/2011/10/05/2199516.html

  There are two computer data storage byte order of priority: high byte and low byte first priority. The high order byte of data on the Internet transmission priority in the network, so it is required for the conversion in transmitting data on the Internet is the low byte of the internal data stored priority machine.

    The first type of structure we want to discuss is: struct sockaddr, the socket type is used to store information: 

  struct sockaddr { 

  unsigned short sa_family; / * address family, AF_xxx * / 

  char sa_data [14]; / ​​* 14-byte protocol address * /}; 

  sa_family usually AF_INET; sa_data contains IP address and port number of the socket. 

  There is also a type of structure: 

  struct sockaddr_in { 

   short int sin_family; / * address family * / 

   unsigned short int sin_port; / * port number * / 

   struct in_addr sin_addr; /* IP地址 */ 

   unsigned char sin_zero [8]; / * fill struct sockaddr 0 to maintain the same size * / 

  }; 

  This structure is more convenient to use. sin_zero for (sockaddr_in structure which is used to fill the same length as the struct sockaddr) should bzero () or Memset () function which is set to zero. Pointing sockaddr_in pointer and a pointer to sockaddr can be converted to each other, which means that if a function parameter type is required sockaddr, when you can call a function pointer pointing sockaddr_in converted to point sockaddr pointer; or vice versa. sin_family commonly assigned AF_INET; sin_port sin_addr and should be converted into network-byte order of priority; sin_addr and no conversion is needed. 

  We next discuss several byte order conversion function: 

  htons()--"Host to Network Short" ; htonl()--"Host to Network Long" 

  ntohs()--"Network to Host Short" ; ntohl()--"Network to Host Long" 

  Here, h represents a "host", n represents a "network", s represents a "short", l represents a "long". 

  Open the socket descriptor, and a connection establishing a binding 

  socket function prototype is: 

  int socket(int domain, int type, int protocol);

    Domain parameters specify what type of address, completely in the /usr/include/bits/socket.h, commonly defined as follows 

PF_UNIX / PF_LOCAL / AF_UNIX / AF_LOCAL UNIX interprocess communication protocol P

PF_INET / AF_INENT IPV4 network protocol

PF_INET6 / AF_INET6 IPv6 network protocol

PF_IPX / AF_IPX IPX-Novell protocol

PF_NETLINK / AF_NETLINK core user interface device

PF_X25 / AF_X25 ITU-T X.25 / ISO-8208 protocol

PF_AX25 / AF_AX25 amateur radio AX.25 protocol

PF_ATMPVC / AF_ATMPVC access the original ATM PVCs

PF_APPLETALK/AF_APPLETALK               Appletalk(DDP)协议

PF_PACKET / AF_PACKET primary Packet Interface 

Type parameter values ​​are the following:

SOCK_STREAM provides bidirectional continuous and reliable stream of data, i.e. the TCP, supports OOB (out-of-band) mechanism. You must use connect () before the transfer of all data to establish the connection status

SOCK_DGRAM discontinuous untrusted connection packet

SOCK_SEQPACKET providing continuous reliable connection packet

Original network protocol provides access SOCK_RAW

SOCK_RDM provide reliable data packet connection

SOCK_PACKET provide direct communication and network drivers

  protocol is usually assigned "0." The Socket () call returns an integer socket descriptor, you can use it in the call back.

error code:

  EPROTONOSUPPORT domain parameters specified type is not supported protocol parameter type or protocol specified

  ENFILE core memory insufficient to establish a new socket structure

  EMFILE process file table overflow can no longer create a new socket

  EACCESS insufficient permissions to establish protocol parameter type or protocol specified

  ENOBUFS / ENOMEM Insufficient memory

  EINVAL parameter domain / type / protocol is not legitimate 

  Once socket call returns a socket descriptor, you should the socket associated with a port on your local machine (often when you need to call this function when the design of the server-side program. You can then request service listening on the port ; and the client is generally no need to call this function). Bind function prototype is: 

  int bind(int sockfd,struct sockaddr *my_addr, int addrlen); 

  Sockfd is a socket descriptor, my_addr sockaddr contains a pointer to the pointer information according to the type of IP address and port number; addrlen often set to sizeof (struct sockaddr). 

  Finally, to bind function point to note is that you can use the following assignment automatically obtain the IP address of the machine and get a random port number is not occupied: 

  my_addr.sin_port = 0; / * Select a random port number system is not used * / 

  my_addr.sin_addr.s_addr = INADDR_ANY; / * fill in the IP address of * / 

  By my_addr.sin_port set to 0, the function will automatically select an unused port for you to use. Similarly, by my_addr.sin_addr.s_addr INADDR_ANY set, the system will automatically fill the local IP address. Bind () function returns 0 when successfully invoked; returns "-1" when an error is encountered and errno is set to the corresponding error number. Also note that, when calling the function, generally not the port number is set to a value less than 1024 to 1024 are reserved as a port number, you can use any port number is not greater than 1024 occupied. 

  Connect () function is used to establish a TCP connection with the remote server, which is a function prototype: 

  int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); 

  Sockfd sockt descriptor is the destination server; serv_addr object pointer contains IP address and port number. -1 error is encountered in errno contains the corresponding error code. Client-side programming without calling bind (), because in this case only know the IP address of the destination machine, and customers to establish a connection through which port the server does not need to be concerned about, the kernel will automatically select an unoccupied port for client to use. 

  Listen () - listen for service requests 

  On the server side process, when a port after the socket and bundled, it is necessary to listen to the port to be processed service request arrives. 

  int listen(int sockfd, int backlog); 

  Sockfd Socket system call returns a socket descriptor; backlog specified maximum number of requests allowed in the request queue, waits for incoming connection request accept () thereof (see below) in the queue. Backlog the number of requests waiting in the queue and services is limited, most of the default value is 20. -1 listen when an error is encountered, errno is set to the corresponding error code. 

  Therefore, the server program is generally a function call in the following order: 

  socket(); bind(); listen(); /* accept() goes here */ 

  accept () - a service request connection port. 

  When a client attempts to connect to the server listening port, the connection request will be queued server accept () it. By calling accept () function for the establishment of a connection, accept () function will return a new socket descriptor to be used for the new connection. And the server may continue to listen in on the previous socket, and can send data on the new socket descriptor () (transmission) and the recv () (receiving) operation: 

  int accept(int sockfd, void *addr, int *addrlen); 

  sockfd is listening socket descriptor, a pointer is typically addr sockaddr_in variable, the variable is used to store information of the host connection request made and services (the request issued by a host from a port); addrten typically one point value sizeof (struct sockaddr_in) integer pointer variable. Returns -1 and a corresponding set errno value when an error occurs. 

  Send () and the recv () - Data transfer 

  These two functions are for data transmission on a connection-oriented socket. 

  Send () function prototype is: 

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

  Sockfd you want to transfer data socket descriptor, msg is a pointer pointing to the data to be transmitted. 

  Len is the length in bytes of data. Is set to 0 (about the usage parameter may refer to the man page) flags under normal circumstances. 

  char *msg = "Beej was here!"; int len, bytes_sent; ... ... 

  len = strlen(msg); bytes_sent = send(sockfd, msg,len,0); ... ... 

  Send () function returns the number of bytes actually sent out, may be less than the data you want to send. It is necessary to send () Returns the value of the measurement. When the send () Returns the value len does not match, this situation should be handled. 

  recv () function prototype: 

  int recv(int sockfd,void *buf,int len,unsigned int flags); 

  Sockfd accept data socket descriptor; buf is located to receive data buffer; len is the length of the buffer. Flags also set to 0. Recv () returns the number of bytes actually received, or when an error occurs, and sets the appropriate return -1 errno value. 

  Sendto () and a recvfrom () - using the data reported for data transmission 

  In connectionless datagram socket, since no local socket connection to a remote machine, the destination address specified in the transmission data should be, the sendto () is the function prototype: 

  int sendto(int sockfd, const void *msg,int len,unsigned int flags,const struct sockaddr *to, int tolen); 

  The ratio function send () function more than two parameters, indicates to the IP address and port number information of the destination machine, and is often assigned tolen sizeof (struct sockaddr). Sendto function also returns the number of data bytes actually transmitted or -1 when a transmission error. 

  Recvfrom () function prototype is: 

  int recvfrom(int sockfd,void *buf,int len,unsigned int flags,struct sockaddr *from,int *fromlen); 

  It is from a variable of type struct sockaddr, this holds the IP address and port number of the source machine. fromlen often set to sizeof (struct sockaddr). When recvfrom () returns, fromlen contains the number of bytes of data actually stored in from. Recvfrom () function returns the number of bytes received or -1 when an error occurs, and sets the appropriate errno. 

  It should be noted is that when you for datagram socket calls the connect () function, you can also use send () and recv () for data transmission, but the socket is still a datagram socket, and use UDP transport layer service. However, sending or receiving a datagram, the kernel will automatically add whom the source and the destination address information. 

  Close () and shutdown () - End Data Transfer 

  When all the data after the end of the operation, you can call the close () function to release the socket, thereby stopping any data operations on the socket are: close (sockfd); 

  You can also call the shutdown () function to close the socket. This function allows you to stop data transmission in only one direction, and the data transmission in one direction to proceed. As you can turn off the write operation of a socket and allowed to continue receiving data on the socket, until all the data is read. 

  int shutdown(int sockfd,int how); 

  Sockfd meaning is obvious, but how parameters can be set to the following values: 

  · 0 ------- allowed to continue to receive data 

  · 1 ------- allowed to continue sending data 

  1.2 ------- allowed to continue to send and receive data, are then allowed to call close () 

  0 shutdown returned when the operation succeeds, returns -1 when an error occurs (and corresponding set errno). 

  DNS-- domain name service-related functions 

  Because IP addresses are difficult to remember and to read and write, to read and write so easy to remember, people often use the domain name to represent the host, which requires conversion of domain names and IP addresses. Function gethostbyname () is the completion of this conversion, the function prototype is: 

  struct hostent *gethostbyname(const char *name); 

  One type hosten called function returns a structure, which is defined as follows: 

  struct hostent { 

   char * h_name; / * * the official domain name of the host / 

   char ** h_aliases; / * a NULL-terminated host alias array * / 

   int h_addrtype; / * return address type, as in the Internet environment AF-INET * / 

   int h_length; / * length of the address byte * / 

   char ** h_addr_list; / * a 0 to the end of the array, containing all the address of the host * / 

  }; 

  #define h_addr h_addr_list [0] / * the first address in the h-addr-list in * /

Reproduced in: https: //www.cnblogs.com/Leon5/archive/2011/10/05/2199516.html

Guess you like

Origin blog.csdn.net/weixin_30772261/article/details/94972905