"TCP / IP network programming" study notes

1.Windows socket program under Linux and thinking the same, but the details are somewhat different
(1) socket program under Windows relies Winsock.dll or ws2_32.dll, must be loaded in advance. There are two ways to load DLL, please see: Load of DLL dynamic link library
(2) Linux uses the concept of "file descriptor", while Windows uses "file handle" concept; Linux does not distinguish between ordinary files and socket files, and Windows distinguished;
(. 3) under the Linux socket () function returns a value of type int, and the Windows is SOCKET type, i.e. the handle.
The send and receive functions read () / write () function to read and write, using recv () / send () for Windows under (4) Linux.
(5) closed socket, Linux using the close () function, while Windows uses closesocket () function.

2. Assign to the standard input standard output and standard error file descriptor
file descriptor object
 0 Standard Output
 1 Output Standard
 2 Standard Error
 
3.WinSock (Windows Socket) programming dependent dynamic link library in the system provides (DLL), there are two versions:  
Earlier DLL is wsock32.dll, size is 28KB, the corresponding header file winsock1.h;
the latest DLL is ws2_32.dll, size is 69KB, the corresponding header file winsock2.h.
Use #pragma command, loaded at compile time:
 #pragma the Comment (lib, "ws2_32.lib")
WinSock programming The first step is to load ws2_32.dll, then call WSAStartup () function to initialize the version number and indicate you want to use
WSADATA wsaData;
WSAStartup( MAKEWORD(2, 2), &wsaData);

 

4. socket programming

(1) using the socket () function creates a socket
(2) using the bind () function socket is bound up with a specific IP address and port, the only way flow through the IP address and port data in order to processing socket
(3) using the connect () function to establish a connection
(4) sockaddr structure
can be considered, the sockaddr is a universal structure, can be used to store various types of IP address and port number, and is designed to save sockaddr_in structure IPv4 address
in addition sockaddr_in6, with to save the IPv6 address
(5) use listen () function allows the socket into the passive listening state
(6) accept () function can be ready to respond to client requests
(. 7) accept () Returns a new socket to communicate with clients, addr saved client IP address and port number
(8) listen () only let socket into the listening state, does not really receive client requests, listen () behind the code will continue to execute until the Accept ()
(9) the Accept () will block execution (after the code can not be executed), until a new request comes in.
(10) TCP server function call sequence: Socket () -> the bind () -> the listen () -> Accept () -> Read () / Write () -> Close ()
(. 11) the TCP client function calls sequence: socket () -> connect ( ) -> read () / write () -> close ()

No. 5.Ack = Seq number + the number of bytes transmitted + 1
 
6.
 (1) calls the close () / closesocket () function means completely disconnected, i.e., not transmit data nor receive data
 (2) using the shutdown () function can be turned off only one data transmission channel, while retaining the other
int shutdown(int sock, int howto);  //Linux
int shutdown(SOCKET s, int howto);  //Windows

sock for the need to disconnect socket, howto is way off

howto following values ​​under Linux:
  • SHUT_RD: disconnecting the input stream. Socket can not receive data (even if the input buffer received data is also erased), enter the relevant function can not be called.
  • SHUT_WR: disconnect the output stream. The socket can not send data, but the data output buffer if there is not transmitted, then delivered to the destination host.
  • SHUT_RDWR: OFF while I / O stream. Call corresponding to twice the shutdown (), wherein a parameter to SHUT_RD, SHUT_WR to another parameter.
howto has the following values ​​in Windows:
  • SD_RECEIVE: Close receiving operation, i.e. disconnect the input stream.
  • SD_SEND: Close transmitting operation, i.e. disconnect the output stream.
  • SD_BOTH: Close simultaneous receive and transmit operations.
shutdown () to close the connection, instead of sockets, no matter how many times a shutdown call (), the socket remains until you call the close () / closesocket () calls the close () / closesocket () closes the socket when, or calling shutdown () closed the output stream, is sent to the other FIN packet. FIN packet data transfer is completed, the computer receives a FIN packet to know there will be no data transmission over the
 (3) in the case, close () / closesocket () immediately sends FIN packets to the network by default, regardless of the output buffer are there any data, but the data transfer output buffer shutdown () will wait for the completion of FIN packet retransmission, which means, call the close () / closesocket () will be lost output data buffer, and call shutdown ( ) will not.
 
When 7. recv () returns a unique opportunity 0 FIN packet is received
 
8. The network byte order
different ways, and the parsed data to save CPU (main CPU of the Intel family of little-endian), data parsing errors occur when the little-endian and big-endian systems communication systems. Therefore, before transmitting data, to format the data into a unified, - network byte order (Network Byte Order). Unified network byte order is big-endian.
 
9.
the ping domain can view the IP address corresponding to the domain name
nslookup command to view the default DNS server address registered in your computer
 
10.
 (1) The following function to obtain the IP address of the domain name string format by passing
#include <netdb.h>
struct hostent *gethostbyname(const char *hostname);

 Hostnet structure pointer returns success, failure to return a NULL pointer

 
struct hostnet
{
  char *h_name;
  char **h_aliases;
  int h_addrtype;
  int h_lenght;
  char **h_addr_list;
}

 (2) gethostbyaddr () function using the IP address acquisition domain related information

#include <netdb.h>
struct hostnet *gethostbyaddr(const char *addr, socklen_t len, int fanily);

 Back hostnet body structure variable address value success, failure to return a NULL pointer

 addr: in_addr structure pointer contains IP address information
 len: the number of bytes of address information transmitted to the first argument, IPv4 when 4, IPv6 when. 6
 Family: group transfer address information, IPv4 when AF_INET, IPv6 when is AF_INET6
 (3) byte order conversion function
 unsigned short htons(unsigned short);
 unsigned short ntohs(unsigned short);
 unsigned long htonl(unsigned long);
 unsigned long ntohl(unsigned long);

 (4)  converting the character string information to network byte order integer

 #include <arpa/inet.h>
 in_addr_t inet_addr(const char *string);

  Returns the 32-bit big endian integer value on success, failure INADDR_NONE

 (5) inet_aton () will convert the IP address bit string 32 and returns an integer in network byte order, but using the function in_addr structure, with a high frequency of use and which
 
#include <arpa/inet.h>
int inet_aton(const char *string, struct in_addr *addr);

  Return 1 (true) if successful, returns 0 (false) fails

 (6) inet_ntoa () function can be converted to integer in network byte order IP address, to a string
#include <Arpa / inet.h>
  char * inet_ntoa ( struct in_addr ADR);

  Return a successful transition of the address of the string value, -1 on failure;

        Copy should call the function immediately after completion string information to other memory space, because, if a call inet_ntoa function again, there is a string of information previously stored may be overwritten
 
 (7) the network address information of the initialization method:
 
struct sockaddr_in addr;
char *serv_ip = "211.217.168.13";
char *serv_port "9190";
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(serv_ip);
addr.sin_port = htons(atoi(serv_port));

 INADDR_ANY constant distribution server using the IP address, the computer can obtain the IP address of the server is running automatically

 addr.sin_addr.s_addr = htonl(INADDR_ANY);
11. The socket option
getsockopt(int sock, int level, int optname, void *optval, socklen_t *optlen);
setsockopt(int sock, int level, int optname, const void *optval, socklen_t optlen)
  • SO_SNDBUF: Enter the buffer size for available options
  •  SO_RCVBUF: output buffer size related options
  •  SO_REUSEADDR: This option can be set TRUE to reassign Time_wait socket port number in the state to the new socket
 TCP_NODELAY set to 1 to disable the Nagle algorithm
 
12. Multi-process
  (1) create a process by calling the fork function
 #include <unistd.h>
 pid_t fork(void);

 The parent process: fork function returns the child process ID

 Child process: fork function returns 0
(2) should be passed to the child process the parent process to create a child process exit parameter value or return statement to return values, prevent zombie
 destruction zombie 1: using the wait function
#include <unistd.h>
pid_t wait(int *statloc);

 -> Return successful termination of the child process ID, failure to return -1

 The return value passed when the child dies to save memory space referred to statloc, have to be isolated by the following macro
  •  Returns a "true" true normal child process termination WIFEXITED
  •  WEXITSTATUS return the child process return value
 Destroy zombies 2: Use waitpid function
 #include <sys/wait.h>
 pid_t waitpid(pid_t pid, int *statloc, int options);

 -> Return successful termination of the child process ID, failure to return -1

 pid target child process terminates waiting ID, if the pass -1, may wait for any child process terminates

 (3) statloc parameters statloc and wait function has the same meaning
 (4) options deliver constant WNOHANG sys / wait.h declared, even if no child process termination will not enter the group state tournament, but the function returns 0 and exit
 (5) signal and signal function
 #include <signal.h>
 void (*signal(int signal, void (*func)(void)))(int);

 -> In order to generate a signal when the call, the function returns a pointer registered before

 (6) using the signal processing function sigaction
 #include <signal.h>
 int sugacyion(int signo, const struct sigaction *act, struct sigaction *oldact);

 -> return 0 on success, -1 on failure

 After copying the socket file descriptor via fork function, corresponding to the same port multiple sockets, only those sockets descriptors are terminated, in order to destroy the socket
 
inter-process communication 13. The
 function creates the pipeline:
 #include <unistd.h>
 int pipe(int filedes[2]);

 -> return 0 on success, -1 on failure

 filedes [0]: it is used to receive data through the pipeline file descriptor, i.e. the outlet pipe
 filedse [1]: is used to transmit data through the pipeline file descriptor, i.e. the inlet pipe
 
14.I / O multiplexing
 (1) for fd_set speaking of macro variables:
 FD_ZERO(fd_set *fdset)
 FD_SET(int fd, fd_set *fdset)
 FD_CLR(int fd, fd_set *fdset)
 FD_ISSET(fint fd, d_set *fdset)

(2) select functions:

 #include <sys/select.h>
 #include <sys/time.h>
 int select(int maxfd, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout);

 

15. A plurality of I / O functions

 (1) Upon receipt of MSG_OOB urgent message, the system will generate SIGURG speaking the message and calls the registered signal handler
 (2) the process of processing the signal must be specified when SIGURG signal processing, and geipid function call this function returns the process ID
 (3) fcntl function for controlling file descriptor
 
fcntl(recv_sock, F_SETOWN, getpid());

 The meaning of the above is called "socket owner (F_SETOWN) will be changed to point to a file descriptor recv_sock getpid function return value as the ID of the process

 (4) Emergency emergency message pointer to the next position (offset +1), meaning that the emergency message supervise message handling, not limited emergency message transmission form
 (5) while the recv call transfer function options MSG_PEEK , is to ensure that even if there is no data to be read does not enter the blocked state, set MSG_PEEK options and call recv function, even if the read data input buffer will not be deleted, this option is often in cooperation with MSG_DONTWAIT, for function call non-blocking manner verify presence or absence of data to be read

Guess you like

Origin www.cnblogs.com/chenweilin/p/11816095.html