Windows Network Programming Summary (1)

xiaoxiao2021-03-06  108

Windows Network Programming Summary (1)

1. About Bind

The specific meaning of INADDR_any is to bind to 0.0.0.0. At this point, it will be valid for all the addresses. If the system considers redundancy, use multiple network cards, then this kind of bind will be bind on all NIC. In this case, you can receive a packet on all valid addresses.

E.g:

SockAddr_in local;

Local.sin_addr.s_addr = htonl (INADDR_Any);

Another way is as follows:

SockAddr_in local;

Hostent * thishost = gethostbyname ("");

Char * ip = inet_ntoa (* (struct in_addr *) * thisHost-> h_addr_list);

Local.sin_addr.s_addr = inet_addr (ip);

In this manner, the current first available address will be bound to the system. In a multi-NIC environment, there may be problems.

The most common way:

Const char localip [] = "192.168.0.100";

SockAddr_in local;

Local.SIN_ADDR.S_ADDR = INET_ADDR (localip);

Bind (Socket, (LPSOCKADDR) & local, Sizeof (SockAddr_in)

Bind security issues:

If you use INADDR_any when you are in Bind, you will be able to monitor on all valid addresses, but Socket has a feature: you can bind multiple sockets on the same port.

Let's take a look at the situation below: Suppose your system has only one IP: 192.168.0.100, you want BIND to 4096 port. For the two bind below, when the packet arrives, will anyone receive it?

Local.sin_addr.s_addr = htonl (INADDR_Any);

Local.sin_addr.s_addr = INET_ADDR ("192.168.0.100");

The Winsocke library is handled in this way: who is bound to get the category, who gets the packet. Obviously, the second BIND will get the data packet arrived. What is avoided? Use the SO_EXECLUSINEADDRUSE option. It should be noted that this option can be used after Windows NT 4 Service Pack 4 (including SP4).

Sample code:

#ifndef so_execlusineaddruse

#define so_execlusineaddruse ((int) (~ so_reuseaddr))

#ENDIF

SockAddr_in local;

BOOL VAL = True;

Local. SIN_FAMILY = AF_INET;

Local. SIN_PORT = HTONS (4096);

Local.sin_addr.s_addr = htonl (INADDR_Any);

Setsocketopt (Socket,

Sol_socket,

SO_EXECLUSINEADDRUSE,

(char *) & VAL,

SizeOf (VAL));

Bind (Socket, (LPSOCKADDR) & local, Sizeof (SockAddr_in)

2. About Connect

In blocking mode, when the connection time of Connect is about 20 seconds, it can be used to determine whether the path to the service host is passage, or first ping the IP address of the other host. A. The gethostbyAddr blocked time regardless of the success of about 4 seconds.

Hostent * Remotehost;

Unsigned int Addr;

AddR = inet_addr ("192.168.0.100);

Remotehost = gethostbyaddr ((char *) & addr, 4, af_inet);

IF (NULL! = Remotehost)

{

// Remote Host Online

}

B, using ping mode for about 2 seconds

3. Nagle algorithm (TCP_Nodelay option)

Winsock enables this algorithm by default. The Nagle Algorithm reduces the number of zero-smash small packets sent by the host by depositing unconfirmed data into the buffer until a package is saving. But for some applications, this algorithm will reduce system performance. This algorithm is turned off using the TCP_Nodelay option. However, it is best to test the Nagle algorithm to improve the transmission speed, then set the TCP_Nodelay option, because the network performance has a significant negative impact. It is also important to note that TCP_NodeLay is an option in the IPPROTO_TCP layer.

There are 2 points in the shortcomings of the Nagle algorithm:

(1) While restricting the total amount of bandwidth consumed by the data report header, it is at the expense of network delay.

(2) In the buffer of the sender, the data package transmitted by the application has a viscous phenomenon, i.e., several data packets transmitted into the receiver to receive a packet, and can not distinguish the boundaries of each package.

The former is not applicable to a system that requires a fast response time because the data is queued instead of immediate transmission. The latter will affect the accuracy of the data processing of the recipient.

Disable Nagle Algorithm:

INT BNODELAY = 1;

Setsockopt (Socket,

Ipproto_tcp,

TCP_Nodelay,

(char *) & bnodelay,

SiZoEOF (Bnodelay));

4. TCPSEGMENTSIZE

TCPSEGMENTSIZE is the maximum length of a single datagram when sending accepts, and the system is 1460bytes.

In the SOCK_STREAM mode, if a single transmission data exceeds 1460, the system will be divided into multiple datagram, and the other party will be a data stream, the application needs to increase the judgment of the broken frame. Of course, the size of the 1460 can be changed in a modified registry, but Micrcosoft believes that 1460 is the best efficiency parameter, which is not recommended.

In the industrial control system, it is recommended to turn off the Nagle algorithm, each time the data is less than 1460 (recommended 1400), so that each time it is transmitted, it is a complete datagram, reducing the fault processing of the other party on the data stream.

5. SO_LINGER option

SO_Linger delay closes the connection and controls through the Struct Linger structure.

Struct linger {

INT L_ONOFF <;

INT L_LINGER;

}

L_Linger needs timeout value (in seconds)

l_onoff indicates to zero indicates that SO_LINGER (at this time, equivalent to SO_DONTLINGER)

Allows SO_LINGER when a non-zero value.

Example:

// Prohibited delay close (SO_DONTLINGER)

Struct linger li = {0, 0}; setsockopt (socket, sol_socket, so_linger, (char *) & li, sizeof (linger));

6. About Shutdown

Int shutdown

Socket S,

Int how

);

The shutdown function is used to disable reception on any type of socket, prohibit send or prohibit the transmission and reception.

There are three types of HOW parameters:

SD_SEND

The subsequent transmission operations on the socket will be disabled at this point.

SD_RECEIVE

The subsequent reception operations on the socket will be disabled at this time.

SD_BOTH

The subsequent transmission / reception operation on the socket will be disabled at this time.

Precautions:

The shutdown function does not turn off the socket and the resource occupied by the socket will be kept until the CloseSocket () call.

No matter whether the SO_Linger setting is or not, the shutdown function will not block.

转载请注明原文地址:https://www.9cbs.com/read-99441.html

New Post(0)