Several questions about the Socket application from UNIX to Windows transplant

zhaozj2021-02-08  234

Socket applications should pay attention to in WINDOWS transplantation from UNIX, Sun Chengshan Sockets, Science and Technology, Zibo City, Shandong Province, today has become the most popular network communication application interface. Sockets were initially divided by Berkeley, Calif, Berkeley, developed to UNIX operating systems, which was transplanted to DOS and Windows systems, especially in recent years, have been widely popular in the world, and has grown rapidly. It has further laid its dominance in the field of network communication program development. Cross-system network interconnection is getting higher and higher in my country. Recently, in the development of banks and securities networking systems, we tried to do transplants from UNIX Sockets to Windows Sockets communication applications, seeing to work with the problems that should be paid attention to in the transplantation process. The people will work together to discuss, in order to play the role of tile jade.

Modify the definition of the header file in the UNIX Sockets application contains the statement of the header file as #include , and the corresponding statement in the Windows Sockets application should be #include . The implementation of Windows Sockets consists of two parts, namely development components and runtime components. The development component is for programmers to develop Windows Sockets applications, which includes Windows Sockets application interface functions libraries, header files, and some documents that describe Windows Sockets implementation, where header file Winsock.h contains Windows Sockets implementation defined. Macro, constant value, data structure, and function call interface prototype. Winsock.h is the header file that must be included in writing a Windows Sockets application. Windows Sockets DLL Initialization and Resource Release In the interface function of Windows Sockets, WSAStartup () is provided with two functions for initializing the Windows Sockets DLL resources at the end of the program when the program starts, they are Two functions that must be used when writing a Windows Sockets application. Change the type of socket from INT to Socket In the UNIX system, the socket type is defined as an INT type, and in the Windows system, the socket type is defined as a socket type, ie UNSigned INT. Errors Acquisition and Settings In the UNIX system, get, set the error code to use global variable errno, and change it in a Windows system to use Function WsageTlasterror () and WsasetLastError (). Windows Sockets provides two error handlers WsageTlasterror () with WSaseTlasterror () to get the nearest error number of setting the current thread without using the multi-threaded environment. Close socket uses a close () function in a UNIX system to close the socket, and in the Windows system, use the closSocket () function. Control of sockets In UNIX systems, use the IOCTL () function and the FCNTL () function to implement control of the socket, and the ioctlsocket () function should be used in the Windows system. The process of GetSockOpt () and setsockopt () functions is different from the support of these two functions in UNIX Sockets and Windows Sockets. In the Windows Sockets getsockopt () and setsockopt () does not support the Berkeley Sockets options are: SO_RCVLOWAT accept the low tide mark SO_RCVTIMEO accept the low tide mark SO_SNDTIMEO timeout SO_SNDLOWAT send send timeout IP_OPTIONS bring options TCP_MAXSEG IP header to obtain the maximum size of the TCP socket is SO_ACCEPTCONN Monitor SO_ERROR Take the error status and clear the SO_TYPE socket type Use the unsupported option to return error code WSAENoProtoOpt, which is returned by the wsagetlasterror () function. Since the support is provided, it is necessary to modify the corresponding source program when performing migration. Use macro fd_xxx to control the FD_SET structure Some functions of Windows Sockets are consistent with UNIX Sockets on the interface, but their internal implementation is different, for example, in the parameter of the function select (), UNIX Sockets implement socket collection The bit mask, but in Windows Sockets is an array of Sockets.

Although the collection of sockets is still represented by fd_set, the code directly modified the FD_SET structure in the UNIX Sockets source file will not work properly in the Windows Sockets environment. Therefore, the direct modification of the structure fd_set should be changed to the source program to be modified by using the fd_xxx macro when performing migration. You should try to use the WSA Macro in the Windows Sockets program to define a macro starting in the Windows Sockets header file, using these macros in the program, can greatly increase the readability of the program. For example: errno = wsagetlasterror (); if (errno = wsaewoldblock) {...} These macros are Windows Sockets may have errors in the application implementation process, which can be expanded to make the program's read and write, it is The Windows Sockets application writes the specification that follows the staff developer, so these macros should be used as much as possible to increase the readability of the program. The processing of blocking calls Windows is a non-preemptive multi-task environment. The switching between the tasks is driven by message. If an application cannot actively give it to control, other applications cannot be executed, this is to have a UNIX operating system The difference in essence. The blocking problem must be considered for applications that are transplanted from the UNIX Sockets environment. To solve the problem of blocking, Windows Sockets specials an increase in blocking processing function: wsaisblocking () detection blocking call WSACANCELBLOCKINGCALL () Cancel a ongoing blocking call wsasetBlocking () Setting your own blocking Handling routine WSAUNHOOKBLOCKINGHOOK () Restore The default blocking process routine is not affecting the blocking process routine in the original environment, when installing your own blocking process routine, pay attention to the program instant pointer to the returned previously installed blocking process routine, and after the processing restore. Try to change the blocking call to the message-based asynchronous operation in the Windows Sockets network program design, although it allows blocking operations, a blocking may block the entire Windows environment, and in the UNIX Sockets program, the default mode of operation of the socket is Blocking. Windows Sockets adopts messy asynchronous access policies in order to support Windows messaging mechanisms, which uses messages based asynchronous access policies. To this end, it is recommended to change the blocking call in the source program to the message-based asynchronous operation. Windows Sockets has added the following functions to implement asynchronous access operations: Wsaasyncselect () standard Berkeley function select () asynchronous version

WSaasyncGetxby () Standard Berkeley Function getXByy ()

WSACANCELASYNCREQUEST () Cancel Function WSAAsyncGetxByy () Execution

Transplantation of the UNIX Sockets application is a more cumbersome work, in addition to the direct related to Sockets in the transplant, pay attention to the difference in the development environment, and the program should also be For some corresponding adjustments, for example, some pointers related to the Windows application should be set to the FAR type, etc., this is no longer described here.

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

New Post(0)