Windows* Sockets 2 Application Programming Interface An Interface for Transparent Network Programming Under Microsoft Windowstm revision 2 August 7, 1997



Download 1.64 Mb.
Page11/49
Date31.07.2017
Size1.64 Mb.
#24975
1   ...   7   8   9   10   11   12   13   14   ...   49

58 Byte Ordering


Care must always be taken to account for any differences between the Intel Architecture byte ordering and that used on the wire by individual transport protocols. Any reference to addresses or port numbers passed to or from a WinSock routine must be in the network order for the protocol being utilized. In the case of IP, this includes the IP address and port fields of a struct sockaddr_in (but not the sin_family field).
Consider an application which normally contacts a server on the TCP port corresponding to the "time" service, but which provides a mechanism for the user to specify that an alternative port is to be used. The port number returned by getservbyname() is already in network order, which is the format required for constructing an address, so no translation is required. However if the user elects to use a different port, entered as an integer, the application must convert this from host to TCP/IP network order (using the WSAHtons() function) before using it to construct an address. Conversely, if the application wishes to display the number of the port within an address (returned via, e.g., getpeername()), the port number must be converted from network to host order (using WSANtohs()) before it can be displayed.
Since the Intel Architecture and Internet byte orders are different, the conversions described above are unavoidable. Application writers are cautioned that they should use the standard conversion functions provided as part of WinSock rather than writing their own conversion code, since future implementations of WinSock are likely to run on systems for which the host order is identical to the network byte order. Only applications which use the standard conversion functions are likely to be portable.

59 WinSock 1.1 Compatibility Issues


To provide smooth backwards compatibility, WinSock 2 continues to support all of the WinSock 1.1 semantics and function calls except for those dealing with psuedo blocking. Since WinSock 2 runs only in 32 bit preemptively scheduled environments such as Windows NT and Windows 95, there is no need to implement the psuedo blocking found in WinSock 1.1. This means that the WSAEINPROGRESS error code will never be indicated and that the following WinSock 1.1 functions are not available to WinSock 2 applications:

  1. WSACancelBlockingCall()

  2. WSAIsBlocking()

  3. WSASetBlockingHook()

  4. WSAUnhookBlockingHook()

WinSock 1.1 programs that are written to utilize psuedo blocking will continue to operate correctly since they link to either WINSOCK.DLL or WSOCK32.DLL, both of which continue to support the complete set of WinSock 1.1 functions. In order for these programs to become WinSock 2 applications, some amount of code modification must occur. In most cases, judicious use of threads to accommodate processing that was being accomplished via a blocking hook function will suffice.



60 Default state for a socket’s overlapped attribute


When Microsoft introduced the 32 bit version of WinSock 1.1 with their WSOCK32.DLL, the default case for the socket() function was to create sockets with the overlapped attribute. In order to preserve backwards compatibility with currently deployed WSOCK32.DLL implementations, this will continue to be the case for WinSock 2 as well. That is, in WinSock 2, sockets created via the socket() function will have the overlapped attribute. However, in order to be more compatible with the rest of the Win32 API, sockets created via WSASocket() will, by default, not have the overlapped attribute. This attribute will only be applied if the WSA_FLAG_OVERLAPPED flag bit is set.

61 Winsock 1.1 Blocking routines & EINPROGRESS


One major issue in porting applications from a Berkeley sockets environment to a WinSock 1.1 environment involves "blocking"; that is, invoking a function which does not return until the associated operation is completed. The problem arises when the operation may take an arbitrarily long time to complete: an obvious example is a recv() which may block until data has been received from the peer system. The default behavior within the Berkeley sockets model is for a socket to operate in a blocking mode unless the programmer explicitly requests that operations be treated as non-blocking. WinSock 1.1 environments could not assume preemptive scheduling. Therefore, with WinSock 1.1 it was strongly recommended that programmers use the nonblocking (asynchronous) operations if at all possible. Because this was not always possible, the psuedo blocking facilities described below were provided (NOTE: This is no longer a necessary recommendation with WinSock 2, since it runs on pre-emptive 32-bit operating systems, where deadlocks are not a problem).
Even on a blocking socket, some operations (e.g. bind(), getsockopt(), getpeername()) can be completed immediately. For such operations there is no difference between blocking and non-blocking operation. Other operations (e.g. recv()) may be completed immediately or may take an arbitrary time to complete, depending on various transport conditions. When applied to a blocking socket, these operations are referred to as blocking operations.
With a (16-bit) WinSock 1.1, a blocking operation which cannot be completed immediately is handled via psuedo blocking as follows. The service provider initiates the operation, and then enters a loop in which it dispatches any Windows messages (yielding the processor to another thread if necessary) and then checks for the completion of the WinSock function. If the function has completed, or if WSACancelBlockingCall() has been invoked, the blocking function completes with an appropriate result. Refer to WSASetBlockingHook() for a complete description of this mechanism, including pseudo code for the various functions.
A service provider must allow installation of a blocking hook function that does not process messages in order to avoid the possibility of reentrant messages while a blocking operation is outstanding. The simplest such blocking hook function would simply return FALSE. If a service provider depends on messages for internal operation it may execute PeekMessage(hMyWnd...) before executing the application blocking hook so it can get its messages without affecting the rest of the system.
In (16-bit) WinSock 1.1 environments, if a Windows message is received for a process for which a blocking operation is in progress, there is a risk that the application will attempt to issue another WinSock call. Because of the difficulty of managing this condition safely, the WinSock 1.1 specification did not support such application behavior. In WinSock 1.1, it was not permissible for an application to make multiple nested Windows Sockets function calls. Only one outstanding function call was allowed for a particular task. The only exceptions being two functions that were provided to assist the programmer in this situation. WSAIsBlocking() may be called at any time to determine whether or not a blocking WinSock 1.1 call is in progress. Similarly, WSACancelBlockingCall() may be called at any time to cancel an in-progress blocking call, if any. Any other nesting of functions in WinSock 1.1 will fail with the error WSAEINPROGRESS.
It should be emphasized that this restriction applies to both blocking and non-blocking operations, but only for WinSock 1.1. For WinSock 2 applications (i.e., those that negotiate version 2.0 or higher at the time of WSAStartup()) there is no restriction on the nesting of operations. Operations can become nested under some rare circumstances such as during a WSAAccept() conditional-acceptance callback, or if a service provider in turn invokes a WinSock 2 function.
Although this mechanism is sufficient for simple applications, it cannot support the complex message-dispatching requirements of more advanced applications (for example, those using the MDI model). For such applications, the WinSock 1.1 API included the function WSASetBlockingHook(), which allows the application to specify a special routine that would be called instead of the default message dispatch routine described above.
The WinSock provider calls the blocking hook only if all of the following are true: the routine is one which is defined as being able to block, the specified socket is a blocking socket, and the request cannot be completed immediately. (A socket is set to blocking by default, but the IOCTL FIONBIO, and WSAAsyncSelect()set a socket to nonblocking mode.) If a WinSock 1.1 application uses only non-blocking sockets and uses the WSAAsyncSelect() and/or the WSAAsyncGetXByY() routines instead of select() and the getXbyY() routines, then the blocking hook will never be called and the application does not need to be concerned with the reentrancy issues the blocking hook can introduce.
If a WinSock 1.1 application invokes an asynchronous or non-blocking operation which takes a pointer to a memory object (e.g. a buffer, or a global variable) as an argument, it is the responsibility of the application to ensure that the object is available to WinSock throughout the operation. The application must not invoke any Windows function which might affect the mapping or addressability of the memory involved.


Download 1.64 Mb.

Share with your friends:
1   ...   7   8   9   10   11   12   13   14   ...   49




The database is protected by copyright ©ininet.org 2024
send message

    Main page