Simple Winsock Application Design (1)
Lin Jun
I believe that readers have basic understandings for Winsock's definition, system environment, and some Winsock Stack and Winsock applications. Next, I hope that I can introduce a simple WINSOCK network application design for readers.
We will gradually establish a pair of TCP Socket mainfast from the TCP Socket master from the 46 Application Interface (API) defined by Winsock 1.1 specification. In these two programs, Server will create Socket links, shutdown, and data delivery, etc., will use the "Asynchronous" function provided by Winsock; ). Since our focus is not in the MS Windows SDK program design, we will use the easiest way to display messages; the skills of the MS Windows program, please study the relevant books and articles by readers.
Today, let's first take a look at the main connection (CONNECT) and close (close). (See Figure 1.)
(Figure 1. The main TCP socket connection of the master is established and closed)
Previously, the author has simply introduces the concept of the master from the architecture. Now we will explain the examples of life in life, readers will easily understand the description of the author later. We can assume that Server is like some of the services provided by the Telecom, such as "104 review" or "112 obstacles".
(1) The Telecommunications Bureau first established a telephone switch, which is like a call socket () in the call. (2) The Telecommunications Bureau is then set to 104, just like we call the bind (), specify the Server's socket specified (bind) in a port. Of course, the Telecommunications Bureau must let users know this number; and our client program also knows the port used by Server, and there is a way to connect. (3) There will be some auto-service extensions under the 104 review table of the telecommunications office, but its number is limited, so sometimes you will allocate this number (busy line). Similarly, when we create a TCP's server socket, we will call the listen () in a letter to listen; the second parameter of listen () is the number of waiting queue, and the usual value is from 1 to 5. (In fact, the two is still a bit different.) (4) The user knows the 104 check-number service of the Telecom Bureau, and he can use a phone to dial the connection. This is why we client open an identical TCP Socket, then call the connect () to connect the port specified by Server. Of course, like the phone, if Waiting Queue is full, your connection will fail when it is unable to serve, or if Server does not provide this service, your connection will fail. (5) After receiving the call of this inquiry, the Tripline of the Telecommunications Bureau has turned to another extension, and the switchboard itself returns to the waiting state. Server Listening Socket is also the same, when you call the Accept () card, the Server's system creates a new Socket to serve this connection, and the original socket will return to the status of the listener. (6) When you have finished the query, you can hang on the phone, and you will be offline. This is also true of Socket closing between Client and Server; however, this shuts down the offline action, can be shut down by the Client or Server. Some phone query systems are not the same? Next, we will look at how the main TCP socket is reached with these Winsock functions; and uses a function in the Winsock Stack in the WINSOCK Stack in the WINSOCK STACK in the Sockets. Only the programs of the program are listed in the article, please see the program of the appendix.
[Server Establish Socket and enter the listening waiting state]
First we first see how the Server ends creates a TCP Socket and enters the status of listening waiting.
In Figure 1. We can see the first call to the WSAStartup (). described as follows:
WSAStartup (): Connect the application of the application with Winsock.dll's first one. Format: int PASCAL FAR WSAStartup (WORD wVersionRequested, LPWSADATA lpWSAData); Parameters: wVersionRequested want to use the Windows Sockets API version lpWSAData point WSADATA information index Return value: Success - 0 defeat - WSASYSNOTREADY / WSAVERNOTSUPPORTED / WSAEINVAL Description: This function " Must be the first in the application call to the Windows Sockets DLL function, and only this function is successful after the call is successful, and the other Windows Sockets DLL can be called. This file also allows users to specify the Windows Sockets API version to use, and get some information from the designer. In the program, we have to use Winsock 1.1, so we have a section in the program:
WSASTARTUP ((1 << 8) | 1), (LPWSADATA) & WSADATA)
Among them ((Word) ((1 << 8) | 1) means that we want to use the Winsock "1.1" version, and WSADATA is used to store some information about this WinSock Stack.
Let's call the socket () in a function to open the Server's TCP Socket.
Socket (): Create a socket. Format: Socket Pascal Far Socket (int AF, INT TYPE, INT Protocol); Parameters: AF currently only provides PF_INET (AF_INET) TYPE SOCKET (SOCK_STREAM, SOCK_DGRAM) Protocol Communication Agreement (if the user is not specified, set to 0 ) Passage: Success - Socket identification code failed - Invalid_Socket (Call WsageTlasterror () Reason: This file is used to create a socket and build the resources it uses for this Socket. Socket types can be Stream Socket or DataGram Socket.
We have to build TCP Socket, so our second parameter in the program is SOCK_STREAM, and we will record this Socket number in Listen_SD.
Listen_SD = Socket (PF_INET, SOCK_STREAM, 0)
Next we want to specify a location and port to Server, so that Client know which port is to connect which port is to connect; so we call the bind ().
Bind (): Specifies the local location of the Socket (AddRess). Format: Int Pascal Far Bind (Socket S, Const Struct Sockaddr Far * Name, Int Namelen); Paramese: s Socket Deficker Name Socket Address Name Dax Address Delivery Value: Success - 0 Failed - Socket_ERROR (Call WsagetLastError () can be learned to know: This one is specified to specify the local location and port gives an unnamed Socket. If the user does not at the address or port value, then he can set the location to INADDR_Addr_any, and port is 0; then Windows Sockets will automatically set it appropriate and port (1024 to 5000) ), The user can call GetSockName () when the SOCKET is really connected to the value. BIND () VOC wants to specify the order and port, this address must be the IP address of the machine in this program, so if the reader can set the address to INADDR_any when designing the program, so that the Winsock system will automatically The correct location is filled in. If you want to make the program only on a machine, set the location to the IP address of the machine. Since this end is a Server end, we must specify a port number to this Socket.
Readers must pay attention to it, once the TCP Socket is selected, it is no longer possible to call another bit or Port.
In the program we specify the port of the Server end to 7016, the address is set by the system.
struct sockaddr_in sa; sa.sin_family = PF_INET; sa.sin_port = htons (7016); / * port number * / sa.sin_addr.s_addr = INADDR_ANY; / * address * / bind (listen_sd, (struct sockaddr far *) & sa, Sizeof (sa))
We use HTONS () in this manner when specifying the port number, mainly because the value of each machine is different (the PC and UNIX system are different), so we use this function to arrange the Host Order. Convert to NetWork ORDER's arrangement; the same ground, we can also call NTOHS () to restore it. (Host Order machine is different, but NetWork ORDER is the same) (Htons is aimed at short values, with HOTNL and NTOHL for long)
After specifying the final site and port, we call the listen () in a letter, so that this socket enters the listening status. The TCP Socket of a Server side must accept the connection of the Client end after you have finished the list of Listen.
Listen (): Sets Socket to the listening status, ready to be connected. Format: Int Pascal Far Listen (Socket S, INT Backlog); Parameters: S Socket Identification Code Backlog Unspeakable to Complete Complete Connection Requirements The maximum number of transmitted values of the connection requirements of each other: Success - 0 failed - SOCKET_ERROR (Call WsageTlasterror () can be learned) Description: The user can use this file to set the socket to enter the listening state, and set up to how many of the ports that do not really complete the connection before the connection. (At present, the maximum value is limited to 5, the minimum is 1), we set backlog to 1.
Listen (listen_SD, 1)
After the call is Listen, if the Client end is connected, the connection action of the Client side will succeed, but the Server side must call the Accept () in the case, and the connection action of the Server end will be formed. But when can we know the client end to connect, and call ACCEPT in a timely manner? Here we have to use a good WSAAsyncSelect letter, turn the SOCKET of the Server to the Asynchronous mode, let the system actively to inform us that there is Client to connect. (Figure 1. Did not draw this one)
Wsaasyncselect (): Requires a Socket event (Event) notified the user when it takes place. Format: int Pascal Far WSaasyncselect (socket s, hwnd hwnd, unsigned int wmsg, long levent); Parameters: s S socket number HWND Action is completed, the Window HandleWMSG that accepts the message Remove the window of the Window message Levent application is interested in the network Event pass-up value: success - 0 failed - socket_error (Call Wsagetlasterror () can beware of the reason) Description: This function is to send the user to request the Windows Sockets DLL to send a message when detecting a socket. Go to the window of the user; the network event is set by the parameter LEVENT. Call this file to actively set the socket to a non-blocking mode. Levent's value can be the following "OR" portfolio: (see Winsock No. 1.1 88, 89) FD_READ, FD_WRITE, FD_OOB, FD_ACCEPT, FD_CONNECT, FD_CLOSE User If you call this one again for a socket, you will cancel it. Connected to this connection .................
(Figure 3) Demoserv establishes Socket on the Winking system and enters the listening state
Readers must pay attention to the setting of WSAAsyncselect is aimed at "a socket"; that is, if you are when you set this socket (fd_accept), you will receive this message (askNC_EVENT) ). If you open a lot of sockets, let each socket turn to Asynchronous mode, then you must set WSaAsyncselect to the "Every Socket". And if you want to cancel a Socket's Async event notification, then use WSaasyncselect this file; and the fourth parameter Levent must be set to 0. WSaasyncselect (s, hwnd, 0, 0) - Cancel all ASYNC event settings
Here, the author has to tell you, call WSAAsyncSelect, and change this socket to the "non-blocking" mode. But at this time, this Socket does not simply use the ioctlsocket () to change it back to the "Blocking" mode. That is, the "non-blocking" mode changed by WSaasyncselect and IOCTLSocket is still different. If you want to change a SOCKET of "Asynchronous mode to" block "mode, you must call WSAAsyncSelect () to cancel all ASYNC events, and then turn it back to block the block.
IOCTLSOCKET (): Controls the mode of the socket. Format: Int Pascal Far IOCTLSOCKET (Socket S, Long CMD, U_LONG FAR * Argp); Parameters: S Socket Identification Code CMD Directive Name Argp Number Parametric Passage Revitalization: Success - 0 Failed - Socket_ERROR (Call WsageTlasterror () Releaseable Cause) Description: This file is used to get or set the operation parameters of the socket. The instructions provided are: (See Winsock Item 1.1 35, 36) CMD's value: Fionbio - Switch Non-Blocking mode FionRead - Since Socket readable data (current In Buffer information) Item) Siocatmark - OOB data has been read
Since the SOCKET of our Server end is in a non-synchronous mode, the Winsock Stack will take the initiative to inform us when the Client side is connected to us; let's take a look at how the client end is connected to the Server end?
[The Client side actively establishes the connection to the Server)]
Client is also a call wsastartup () in a word to establish a relationship with Winsock Stack; then call socket () to create a TCP Socket. (Reader must use TCP socket to connect to the Server's TCP Socket, instead of using UDP socket; because Sockets of the same agreement can communicate, TCP TCP, UDP is UDP)
Different parts of the SOCKET with the Server side are: the Socket of the Client side can call the bind () in a letter, by yourself to specify the IP address and the PORT number; however, you can do not call Bind (), and the Winsock Stack is powered by IP bit. Address and PORT number (this action is completed by the Winsock system when calling the connect (). Usually we don't call bind (), and by the system, you can call the getSockName () in the GetSockName () to check the system to help us set what IP and Port. In general, the system will automatically help us set between 1024 to 5000; and if the reader wants to set the port with BIND, it is best to 5000 or more numbers. Connect (): Requires a TCP Socket to the specified other party. Format: Int Pascal Far Connect (Socket S, Const Struct Sockaddrfar * Name, Int Namelen); Paramese: s Socket Identification Code Name This Socket wants to connect to the diamelelen name of Namelen Name: Success - 0 failed - SOCKET_ERROR (Call Wsagetlasterror () can be learned) Description: This file is used to establish a connection to the other party. If the specified object address is 0, the error value will be returned. When the connection is completed, the user can use this socket to do the use of transfer or reception.
In our example, the client is to connect to the 7016 PORT listening to the Server on the machine, so we have the following frames. (Suppose our machine's IP is MY_HOST_IP)
Struct SockAddr_in SA; / * Variable Declaration * / sa.sin_family = PF_INET; / * Set the server-end data to be connected * / sa.sin_port = htons (7016); sa.sin_addr.s_addr = HTONL (my_host_ip); connection Mysd, (Struct SockAddr Far *) & sa, sizeof (sa)) / * Establish connection * /
[Server end accepts the connection of the Client side]
Since the SOCKET of our Server end is set to "Non-Synchronous Mode", it is for the FD_ACCEPT event, so when the client is connected, the window of our Server end will receive a message from an Async_Event sent by Winsock Stack. (See the settings of WSAAsyncselecteect)
At this time, we should first use WsagetSelectError (LPARAM) to check if there is an error; From the representative) (here is of course an FD_ACCEPT event); then call the related functions to handle this event. So we call an Accept () in a function to create a connection to the Server end.
Accept (): Accepts a Socket connection requirement to complete the connection of the Stream Socket. Format: Socket Pascal Far Accept (Scoket S, Struct Sockaddr Far * Addr, INT FAR * ADDRLEN); Parameters: s S socket identification code addr storage connection to the end of the address AddRlen Addr length passive: Success - new Socket identification code failed - INVALID_SOCKET (Call Wsagetlasterror () Reason: The application of the Server end calls this one-word to accept the Socket connection action required by the Client side; if the SOCKET of the Server is the Blocking mode, and no When people ask for a connection action, then this one will be held by Block; if it is a non-blocking mode, this file will reply immediately. ACCEPT () The reply value of a function is a new socket, which is not reusable to accept other connection requirements; but the SOCKET originally listened can still accept other people's connection requirements. The Server end of TCP Socket After calling accept (), it will return a new Socket number; and this new Socket number is a Socket that is truly communicating with the Client side. For example, we have established a TCP Socket with socket (), and this Socket number (system given) is 1, then we call Bind (), listen (), accept () is a stenet; After calling accept (), the return value is another Socket number (it is also a system), such as 3; then, the number 3; 3 Socket, we collect the information, we also use Socket 3, not Socket 1; readers should not make mistakes.
Our calls in the program are as follows; we can pass the second parameter to the value, know which IP address and the port number of the Port number are connected to our Server.
Struct SockAddr_in sa; int sa_len = sizeof (sa); my_sd = accept (listen_sd, (strunt sockaddr far *) & sa, & sa_len)
When the Server end is over accept (), the main TCP Socket connection is really established; the Server and Client ports can be used to send information to the other party or to the other party. (Relevant information, we will wait for the next phase)
(Figure 4) Demoserv and DemoCLNT are successfully connected to successfully connected
[Server and Client End Socket Connections]
Finally, let's take a look at how to end the socket connection. Socket is close and simple, and can be started first by any end of Server or Client, as long as CLOSESOCKET () can be called. To turn off the Socket of the listening state, it is also use this one.
CloseSocket (): Close a socket. Format: Int Pascal Far CloseSocket (Socket S); Parameters: S Socket Identification Code Passage Passage: Success - 0 Failed - Socket_ERROR (Call WsageTlasterror () Receptes: This one is used to close some Socket. If the user originally sets SO_DONTLINGER to the SOCKET to be turned off, it will reply immediately after calling this one, but this SOKCET has not been transferred after the data will continue after it will be closed. If the user originally set this socket as SO_Linger, there are two cases: (a) Timeout is set to 0, this Socket is reset immediately, and the not received or the unrecovered information is lost. (b) If TIMEOUT is not 0, the data will be sent, or wait until TIMEOUT will really shut down. Before the end of the program, don't forget to call wsacleanup () to inform Winsock Stack; if you don't call this one, some resources may still be occupied by you and cannot clear the release.
WSACLEANUP (): End the use of Windows Sockets DLL. Format: INT Pascal Far WSacleanup (Void); Parameter: No passage: Success - 0 failed - Socket_ERROR () Call WsageTlasterror () Description: Application must call WSAStartup () before using the Windows Sockets DLL Register to Windows Sockets DLL; when the application is no longer necessary to use the Windows Sockets DLL, you must call this one to log out in order to release the resources it occupied.
Simple Winsock Application Design (2)
Lin Jun
In the previous article, the author introduces you how to establish a connection establishment and shutdown for the TCP socket of the primary / server in the Winsock environment; today I will continue to introduce you how to use TCP Socket to collect Delivery, and detailed explanation of FD_READ and FD_WRITE events in WSAAsyncselect libraries (the author found that there are quite a number of people to don't understand these two events).
I believe that the readers already know that the TCP Socket is successfully established in the Client side call Connect, and the Server is called the Accept Function. After the connection is successful, the Client and Server can use this connection. Socket to transfer information to the other party, or collect the information sent by the other party.
(Figure 1. TCP Socket data delivery)
Before the introduction of the information, the author first introduces the characteristics of TCP Socket and UDP Socket in transmitting data:
Stream (TCP) Socket provides "two-way", "reliable", "order", and "non-revitalization" data transmission.
DataGram (UDP) Socket provides "two-way" communication, but there is no guarantee for "Sill", "Sill", "No Heavy Covers", etc. It may also be omission during transmission.
Since the UDP Socket is transmitted, it does not guarantee that the data can be fully delivered to the other party, so some of our commonly used applications (such as Telnet, Mail, FTP, News ..., etc.) use TCP Socket to ensure information Positiveness. (TCP and UDP package transfer agreement is not in our discussion, readers who want to know, please refer to related books) TCP and UDP sockets are two-way, so we use the same socket to do delivery and collection information. Action; General TCP Socket information is sent, and the two functions of call send () and RECV () are reached, while UDP Socket is used in sendto () and Recvfrom () two functions. However, TCP Socket can also be used in Sendto () and Recvfrom (), and UDP socket also can be used in Send () and RECV (), which will be explained later.
Now let's take a look at the letter instructions for Send () and RECV () and go back to our previous program.
◎ Send (): Use the connect-type Socket to transfer data. Format: Int Pascal Far Send (Socket S, Const Char Far * BUF, INT LEN, INT FLAGS); Parameters: S Socket Identification Code BUF Standby Quick Region Len BUF's Length Flags This Form is called Mode back value: Successful - Delivered Data Length Failed - Socket_ERROR (Call WsageTlasterror () Reason: This function applies to Connect DataGram or Stream Socket to transfer data. For the DataGram Socket, if the size of the DataGram exceeds the limit, any information will not be sent and the error value will be sent back. For Stream Socket, in the Blocking mode, if the storage space in the transfer system is not stored in the transfer buffer, Send () will be held by Block until the data is delivered; if the socket is Set to a non-blocking mode, then how much information will be sent as the current Output Buffer space, will not be held by Block. Users should also pay attention to the Send () written execution completion, and does not mean that the information has been successfully sent to the other party, but in the Output Buffer of the system, waiting to be sent. The value of Flags can be set to 0 or MSG_DONTROUTE and MSG_OOB combination. (See Winsock No. 1.1 48)
◎ RECV (): Receive information from Socket. Format: Int Pascal Far Recv (Socket S, CHAR FAR * BUF, INT LEN, INT FLAGS); Parameters: s Socket Identification BUF Storage Received Temporal Len Buf Length Flags This Function is called Way back value: success - received the length of the data (if the other Socket is closed, it is 0) failed - Socket_ERROR (Call wsagetlasterror () can beware of the reason: this franchel with DataGram Socket or Stream Socket receives information. For Stream Socket, we can receive valid information in Input Buffer, but its quantity does not exceed the size of the LEN. If this Socket is set to SO_OOBINLINE, and the data of Out-of-band is not read, only the data of Out-of-band is taken out. For the DataGram Socket, only the first DataGram is taken out; if the DataGram is larger than the storage space provided by the user, only the information of the space size is taken out, and the more information will be lost, and the wrong message will be replied. In addition, if the socket is the Blocking mode, and there is no information in Input Buffer, RECV () reaches the block to any information; if it is a non-blocking mode, the Input Buffer has no information, it will reply to the error immediately. The value of the parameter flags can be 0 or Msg_Peek, MSG_OOB combination; the MSG_peek represents the buffer provided to the user, but the information does not move from the system's Input Buffer; 0 indicates that the copy is removed. (Refer to the Winsock Item 1.1 41) [Server's data delivery and closing socket]
In the previous period, we said that it is a SERVER for an Asynchronous mode; in the program, we have called the listN_SD's SocketElect () in the WSTEN_SD, and set the FD_ACCEPT event, so when the client is connected to us, the system will pass We an Async_EVENT message (see the previous article content); we are receiving the message and judging is an FD_ACCEPT event, so calling accept () to establish a connection.
MY_SD = Accept (Listen_SD, (Struct SockAddr Far *) & Sa, & Sa_len
We have successfully established the connection of the Server end and the CLIENT connection after the call is successfully established. Since we also want to use the asynchronous method, we must use the wsaasyncselect () to help create some events in the new Socket so that the Winsock Stack can actively inform us when the event occurs. Since our Server is a passive request, then then pays a reply, so we set fd_read events; we also hope that Winsock Stack can actively inform us when you know Client Close Socket, so you also set fd_close events. (The reader must pay attention, we set the Socket number of the event is the new Socket number (MY_SD, HWND, Async_Event, fd_read, hwnd, async_event, fd_read | fd_close)
Here, we also use hwnd this window and async_event this message; in the forebel, the author told you that when you receive an Async_EVENT message, we can use WsageTSelectEvent (LPARAM) to determine which event (fd_read or fd_close) I have happened; so I will not be confused. Then when will we receive a message from fd_read or fd_close event?
[FD_READ Event]
We will receive the FD_READ event to notify us to read the information:
(1) When the WSAAsyncselect function is called to set the FD_READ event on this socket, there is a data in Input Buffer. (2) Input buffer in the original system is empty, and when the system receives the information, it will notify us. (3) The user calls the RECV or Recvfrom function, reads the information from the Input Buffer, but does not read the data once, and then drive an FD_READ event, indicating that there is still information in the Input Buffer.
Reader must pay attention: If we receive a message from the fd_read event notification, we deliberately don't call the RECV or Recvfrom to read the information, and then the system receives the information, and will notify us again, be sure to wait for us to call. After RECV or Recvfrom, it is possible to receive an event notification from FD_READ.
[FD_CLOSE Event]
When the system knows that the other party has closed Socket (received the fin notification, and Hand-Shaking, and the other party to close the action ", we will receive the fd_close event notification so that we can also close this relative Socket. The fd_close event will only happen to TCP Socket because it is Connection-Oriented; for the CONNECTIONLESS UDP Socket, even if fd_close is set, it will not work.
In the program, when the client is sent to a request, the system will notify us HWND window in an Async_EVENT message; we are using WsagetSelectEvent (LPARAM) and WsagetSelectEvent (LPARAM), after the fd_read event and check, call Recv () The function is charged for the information sent by the Client. RECV (WPARAM, & DATA, SIZEOF (DATA), 0)
The author also mentioned that the FD_XXXX event happened that the FD_XXXX event occurred. When the message was received, the parameter WPARAM when the window Handle was called is the Socket number of the event, so the value of WPARAM is also mentioned earlier. My_sd this socket number. The fourth parameter of the RECV () is set to 0, indicating that we have to read the information from the system's Input Buffer and remove it.
After receiving the request, we have to reply the client, which is to send information to client; then we have to use Send ().
Let's put the information in the data temporary area of the data, then call Send () to send it, and we use the same Socket for WPARAM (My_SD) to do the transmission action because it is two-way.
Send (WPARAM, & DATA, STRLEN (DATA), 0)
Server and client send information after a period of time (all the information is delivered), if the client calls CloseSocket () to turn off its socket, then the system is known, notify us of a FD_CLOSE event message, at this time we You can also call CloseSocket () Close our sockets; Of course, we can also call CloseSocket () to take the initiative to close the socket of our end.
[CLIENT data delivery and closing socket]
Our example of the client is a blocking mode, so when the call connect () is connected to the Server, it may wait until success; the connection is returned, and there is no error, the Client and Server terminal TCP socket The connection is successful. At this time, we can use this connected Socket to send information. Since we don't have to set it as an Asynchronous mode, you don't have to call wsaasyncselect () to set events.
The Client end is usually the first request to send request to the Server end, so we call send () to transfer this information. Our data is small, so it will not be lived by Send () written blasts; however, if you want to send the amount of information, you will wait for a while to return; It is necessary to wait for the data to put it until the system Output Buffer will return; this is because our Client's socket is blocking mode. If we use the Socket in the non-blocking mode, then the send () written will depend on the system's Output Buffer space, only to copy as much information to Output Buffer, then return, and tell the user how much information, Not necessarily, all materials are returned to Output Buffer.
We will request to place in the Data data temporary area, then call Send () will be sent. After the data is sent, we call RECV () to wait for the reply of the Server.
Send (MySD, DATA, STRLEN (DATA), 0) RECV (MySD, & Data, Sizeof (Data), 0)
Since our Client is a Blocking mode, RECV () will always stay in Block until one of the following occurs will be returned.
(1) SERVER is sent. (At this point the RETURN value is the data length read) (2) The Server is turned off the opposite Socket. (At this time is 0) (3) The Client side calls WSACANCELBLOCKINGCALL () to cancel the recv () call. (At this point the return is a socket_error error, error code 10004 WSAEINTR)
Similarly, after all the information is finished, we also call CloseSocket () to close Socket.
◎ WSACANCELBLOCKINGCALL (): Cancel the blocking action currently in progress. Format: int Pascal Far WSAcancelBlockingCall (Void); Parameters: No passage: success - 0 failed - Socket_ERROR (Call WsageTlasterror () can beware of the reason) Description: This file is used to cancel the BLOCKING action in progress . The usual use is: (a) Blocking action is in progress, the application receives a message (Mouse, Keyboard, Timer et al.), Which can be called in the paragraph of the message. (b) Blocking action is in progress, while Windows Sockets calls the "BLOCIKING" function of the Windows Sockets, you can call this function in this function to cancel the blocking action. Users must pay attention to a WSAISBLOCKING () and WSAcAcLockingCall (), in addition to WSAisBlocking () and WSAcAll (), otherwise errors can be generated. In addition, if the canceled blocking action is not accept () or select (), the socket may be in a state, and the user is best to call CloseSocket () to close the socket without any action.
(Figure 2.) Demoserv and DemoCLNT to send information on Winking on Winking
(Figure 3.) Demoserv and DemoCLNT on the Schedule on the Socket on Winking Winking
Introducing the data delivery of TCP Socket, the author introduces the readers to sendTo () and Recvfrom () two functions, and many people may easily make the FD_WRITE event.
[Sendto and Recvfrom Version]
In general, TCP Socket uses two functions of Send () and RECV (); the UDP socket is used by Sendto () and Recvfrom (). This is because the TCP is Connection-Oriented, and after the SOCKET real connection program is younger, you can start sending information. At this time, the system already knows the other party, so we don't have to specify where the information is to be sent. UDP is ConnectionLess, and the two parties that send information do not build a real connection, so we need to use Sendto () and Recvfrom () to specify the following party and know who sent us to us. TCP Socket can also be sent by Sendto () and Recvfrom (), just at this time, the last two parameters of these two functions have no effect, it will be ignored by the system. UDP Socket If the Connect () is called to specify the location of the other party (this Connect does not really make a connection action with the other party, telling our own system saying that we only want to collect, send it) Then, Send () and RECV () can also be utilized to send information.
◎ Sendto (): Send data to the destination designated by the user. Format: Int Pascal Far Sendto (Socket S, Const Char Far * BUF, INT LEN, INT FLAGS, CONST STRUCKADDR FAR * TO, INT TOLEN); Parameters: s Socket Identification Code BUF Storage Information To send information LEN BUF's length Flags This function is called way to send the address to the site tolen to the size of the TOLEN TO: Success - the length of the data fails - Socket_ERROR (Call wsagetlasterror () can know why: this The function is suitable for DataGram or Stream Socket to transmit information to the specified address. For the DataGram Socket, if the size of the DataGram exceeds the limit, any information will not be sent and the error value will be sent back. For Stream Socket, its effect is the same as send (); the value of parameters to and Tolen will be ignored by the system. If the storage space in the Transport system is not stored in these information, sendTo () will be held by Block until the data is sent; unless the socket is set to the non-blocking mode. Users must also note that the sendto () letter execution is completed, does not mean that the information has been successfully sent to each other, but may still be in the system's Output Buffer. The value of Flags can be set to 0, MSG_DONTROUTE and MSG_OOB. (See Winsock No. 1.1 Edition 51)
◎ Recvfrom (): Read the information and store the location of the data source. Format: Int Pascal Far Recvfrom (Socket S, CHAR FAR * BUF, INT LEN, INT FLAGS, INT FAR * ADDR FAR * from FROM, INT FAR * FROMLEN); Parameters: s Socket Identification Code BUF Storage Acquisition Zone LEN BUF Length Flags This Function This Function The way from the source from the source, the source of the source, the size of the source, the size of the source: Success - the length of the received data (if the other Socket is closed, 0) failed - Socket_ERROR (Call WsageTlasterror () Reasons for knowledge) Description: This file is used to read the data and record the location where the data source is recorded. For the DataGram Socket (UDP), a DataGram is read at a time; the STREAM Socket (TCP) is the same as the RECV (), the parameter from and fromlen is ignored by the system. If the socket is the Blocking mode, and there is no information in Input Buffer, RECVFTOM () reaches the block to any information; if the non-blocking mode, the Input Buffer does not have any information, it will reply immediately. [FD_WRITE Event]
The author introduced the occurrence opportunity of FD_READ events, and now continues to introduce FD_WRITE this more confused event, because there is really a lot of people in the event.
From the literal, FD_WRITE should be required to inform us whether a socket is now able to call send () or sendto () to transfer information? The answer can be said "Yes", but it and fd_read have different places.
In front, we know that after calling RECV (), if there is information that is not taken out in Input Buffer, the system will notify us of fd_read. Then if we call Send (), the system's Output Buffer still has space written, it will notify us of a fd_write, told us to continue the information? This answer is "negative"! The system will not notify us.
The system will notify us of the information of the FD_WRITE event, only the following cases: