WinSock programming basics, winsock programming

Source: Internet
Author: User

WinSock programming basics, winsock programming

I. Socket mode

1. Blocking Mode

When a socket is created, the default blocking mode is used. Calling the recv function will make the program wait and the program will be returned only when the data is received.

2. Non-blocking mode:

You can call the ioctlsocket function to explicitly enable the socket to work in non-blocking mode.

u_long ul = 1;    SOCKET s = ::socket(AF_INET,SOCK_STREAM,0);    ::ioctlsocket(s,FIONBIO,(u_long*)&ul);

3. I/O models provided by windows

1. Select model

Use the select function to manage I/O

# Include ".. /.. /common/InitSock. h "# include <stdio. h> CInitSock initsock; int main () {USHORT nPort = 4567; SOCKET sListen =: socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in sin. sin_family = AF_INET; sin. sin_port = htons (nPort); sin. sin_addr.S_un.S_addr = INADDR_ANY; // This is the address of the server. // bind the socket to the local machine if (: bind (sListen, (sockaddr *) & sin, sizeof (sin )) = SOCKET_ERROR) {printf ("Failed bind () \ n"); return -1;} // enter the listening mode: listen (sListen, 5); // select model processing process // 1) initialize a socket fdsocket, add the listening socket handle to this collection fd_set fdsocket; // all available socket sets FD_ZERO (& fdsocket); FD_SET (sListen, & fdsocket); while (TRUE) {// 2) pass a copy of The fdsocket set fdRead to the select function // when something happens, the select function removes the socket handle that has no pending I/O operations in the fdRead set, then return fd_set fdRead = fdsocket; int nRet =: select (0, & fdRead, NULL); if (nRet> 0) {// 3) compare the fdsocket set with the fdread set processed by select. // determine Some sockets have pending I/O and further process these I/O for (int I = 0; I! = (Int) fdsocket. fd_count; ++ I) {if (FD_ISSET (fdsocket. fd_array [I], & fdRead) {if (fdsocket. fd_array [I] = sListen) {// (1) the listener socket receives the new connection if (fdsocket. fd_count <FD_SETSIZE) {sockaddr_in addrRemote; int nAddrLen = sizeof (addrRemote); SOCKET sNew =: accept (sListen, (SOCKADDR *) & addrRemote, & nAddrLen); FD_SET (sNew, & fdsocket); printf ("received connection % s \ n",: inet_ntoa (addrRemote. sin_addr);} else {printf ("too much connecti Ons! \ N "); continue;} else {char szText [256]; int nRecv =: recv (fdsocket. fd_array [I], szText, strlen (szText), 0); // read if (nRecv> 0) {szText [nRecv] = '\ n '; printf ("received data: % s", szText);} else {// Connection closed restart or interrupt: closesocket (fdsocket. fd_array [I]); FD_CLR (fdsocket. fd_array [I], & fdsocket) ;}}} else {printf ("Failed select () \ n"); break ;}} return 0 ;}

2. WSAAsyncSelect Model

The WSAAsyncSelect model allows applications to receive Network Time notifications in the form of windows messages. The CSocket of MFC is also the model.

(To adapt to the windows Message Drive environment, many network applications with low performance requirements use the WSAASyncSelect model)

3. WSAEventSelect Model

4. Overlapped I/O model

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.