Windows Network Programming

Source: Internet
Author: User

 

Author: Cen- Windows Network Programming MiscellaneousBIND: inaddr_any is bound to 0.0.0.0. In this case, all addresses are valid. If the system considers redundancy and uses multiple NICs, bind will be bound to all NICs. In this case, you can receive packets sent to all valid addresses. Example: sockaddr_in local; local. sin_addr.s_addr = htonl (inaddr_any );
Another method 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 way, it is bound to the first available address in the system. In a multi-nic environment, problems may occur.
The most common method is 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: If you use inaddr_any during bind, you can listen on all valid addresses, but the socket has a feature: you can bind multiple sockets to the same port. Let's take a look at the following situation: assume that your system has only one IP Address: 192.168.0.100, And you want bind to port 4096. For the following two types of BIND, who will receive the packet when it arrives? Local. sin_addr.s_addr = htonl (inaddr_any); Local. sin_addr.s_addr = inet_addr ("192.168.0.100 ");
The winsocke library handles this process as follows: Who binds the most clearly and who obtains the data packet. Obviously, the second type of BIND will get the arrived data packet. What if this problem is avoided? Use the so_execlusineaddruse option. Note that this option is available only after Windows NT 4 Service Pack 4 (including SP4. Sample Code: # Ifndef so_execlusineaddruse <br/> # define so_execlusineaddruse (INT )(~ So_reuseaddr) <br/> # endif <br/> sockaddr_in local; <br/> bool val = true; <br/> Local. sin_family = af_inet; <br/> Local. sin_port = htons (4096); <br/> Local. sin_addr.s_addr = htonl (inaddr_any); <br/> setsocketopt (socket, sol_socket, so_execlusineaddruse, <br/> (char *) & Val, sizeof (VAL )); <br/> BIND (socket, (lpsockaddr) & Local, sizeof (sockaddr_in)1. winsokcet architecture 2. When using this function for acceptex, you must include the header text mswsock. h and the link mswsock. Lib. You can add the following statement to the source program. In this way, mswsock. Lib is automatically linked during compilation. # Pragma comment (Lib, "mswsock. lib ")
The following is an example code of using the acceptex function: <Br/> # define strict <br/> # DEFINE _ win32_winnt 0x0500 // Windows 2000 or later <br/> # define win32_lean_and_mean <br/> # pragma comment (Lib, "ws2_32.lib") <br/> # pragma comment (Lib, "mswsock. lib ") <br/> int main () <br/>{< br/> const int bufsize = 48; <br/> lpfn_acceptex lpfnacceptex = NULL; <br/> guid guidacceptex = wsaid_acceptex; <br/> DWORD dwbytes = 0; <br/> socket listensocket = invalid_socket; <br/> socket clientsocket = invalid_socket; <br/> handle hcompport = invalid_handle_value; <br/> overlapped ol; <br/> char Buf [bufsize]; <br/> // init Winsock Lib .... <br/> listensocket = wsasocket (af_ipx, sock_stream, nsproto_spx, null, 0, callback); </P> <p> clientsocket = wsasocket (af_ipx, sock_stream, nsproto_spx, null, 0, wsa_flag_overlapped); <br/> // bind & listen .... <br/> // associate the listening socket with the completion port <br/> createiocompletionport (handle) listensocket, hcompport, (u_long) 0, 0 ); </P> <p> // get accpetex function <br/> wsaioctl (listensocket, sio_get_extension_function_pointer, <br/> & guidacceptex, sizeof (guidacceptex ), <br/> guid guidacceptex = wsaid_acceptex; <br/> DWORD dwbytes = 0; <br/> socket listensocket = invalid_socket; <br/> socket clientsocket = invalid_socket; <br/> handle hcompport = invalid_handle_value; <br/> overlapped ol; <br/> char Buf [bufsize]; <br/> // init Winsock Lib .... <br/> listensocket = wsasocket (af_ipx, sock_stream, nsproto_spx, null, 0, callback); <br/> clientsocket = wsasocket (af_ipx, sock_stream, empty, null, 0, wsa_flag_overlapped); </P> <p> // bind & listen .... <br/> // associate the listening socket with the completion port <br/> createiocompletionport (handle) listensocket, hcompport, (u_long) 0, 0 ); </P> <p> // get accpetex function <br/> wsaioctl (listensocket, signature, <br/> & guidacceptex, sizeof (guidacceptex), & lpfnacceptex, <br/> sizeof (m_workinfo.acceptinfo.lpfnacceptex), & dwbytes, null, null); <br/> zeromemory (BUF, bufsize); <br/> zeromemory (& ol, sizeof (overlapped); <br/> // Post accept message <br/> lpfnacceptex (listensocket, clientsocket, Buf, 0, <br/> sizeof (sockaddr_in) + 16, <br/> sizeof (sockaddr_in) + 16, & dwbytes, & ol); <br/>}< br/>Note that when getting the acceptex function pointer through wsaioctl, you only need to pass it to wsaioctl a valid socket. The type of this socket will not affect the obtained acceptex function pointer. If you do not want acceptex to establish a connection and wait for the user to send data, you must set the fourth parameter to 0. Parameters 5th and 6 must be the size of the corresponding socket address type plus 16 bytes. To enable the server to better process user connection requests, you can adopt the following two Policies: A. set two boundary values so that the unhandled accept operations remain at a fixed level. The recommended maximum is 10. B. Listen to the fd_accept event on listensocket through the wsaeventselect function. When the port is closed, if there are still unprocessed accepte operations, you should first close listensocket and then process these accept operations (release resources, etc.) in iocp ), do not forcibly terminate unhandled accept operations; otherwise, memory leakage may occur.
To prevent malicious users (no data is sent after a connection is established), you can set the so_connect_time attribute of listensocket.
If you want clientsocket to have the same attributes as listensocket, you must call so_update_accept_context on clientsocket.


 

 

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.