Experience and practice in developing Windows sockets communication applications

zhaozj2021-02-17  47

Experience and Practice of Developing Windows Sockets Communication Applications Qingdao Navy Submarine Software Center (266071) Xu Li Guo Summary This article describes the soft, hardware environment, basic steps, and technical points of the development of Windows Sockets communication programs, and in the Windows 95 environment, Programming with Visual C 4.0 as an example, further expounding the experience and practice of developing Windows SoC-KETS communication programs. Keyword socket Windows Sockets computer network communication 1, introducer socket (Socket) initially was initiated by the University of California Berkeley to develop network communication interfaces developed by the UNIX operating system. With the wide use of UNIX operating systems, sockets become current One of the most popular network communication applications interfaces. In the early 1990s, several companies such as Sun Microsystems, JSB Corporation, FTP Software, Microdyne and Microsoft have developed a set of standards, namely Windows Sockets specifications. The Windows Sockets API is a web program design interface for Microsoft Windows, which inherits the main features of Berkeley Sockets, and is important to it. These expansion mainly provide some asynchronous functions and add a network event asynchronous selection mechanism that meets Windows message driver characteristics. These expansions facilitate application developers to prepare software that meets Windows programming mode, which makes it possible to develop high-performance network communication programs under Windows. Socket actually refers to a communication endpoint, with the Socket application developed by the user, can communicate with other Socket applications over the network. In recent years, with the popularity of computer networks and Windows 95, many users developed by many users need to implement data communication between networks, whereby the author uses Visual C 4.0 to develop the development of Windows Sockets applications based on Visual C 4.0 according to the Windows 95 environment. Experience, elaborate on the development of data communication procedures between networks. Second, the software system software used to develop the Windows Sockets network communication program can be Windows 95, or it can be Windows NT because they all support Windows Sockets API, in the following introduction, we will be in Windows 95 The development of the environment is an example. The programming language used is generally optional currently more popular visualization and using Microsoft Visual C 4.0 for object-oriented technology. Visual C 4.0 can run in a Windows 95 or Windows NT environment, which has added a comprehensive integrated Windows-based development tool and a "visual" user interface drive model based on traditional C / C development processes. The Microsoft Base class (MFC, ie Microsoft Foundation Class) library in Visual C 4.0 is a series of C classes, which encapsulates various functions to write applications for the Microsoft Windows operating system series. In terms of socket, Visual C 4.0 has a series of encapsulation for the original Windows Sockets library function, which has created CSocket, CsocketFile, etc., which encapsulates various functions about Socket. The network communication protocol used is generally TCP / IP. Windows 95 and Windows NT have this protocol.

However, the developed network communication applications cannot be deal directly with the TCP / IP core, but to deal with the network application programming interface Windows Sockets API. Windows Sockets API can communicate directly with the TCP / IP core. TCP / IP core protocols together with network physical media (such as NIC) are all available in network applications. Its relationship is shown in Figure 1. Network Communication Application Windows Sockets APITCP / IP Core Protocol Physical Media Figure 1 TCP / IP Protocol Core and Network Communication Application Diagram In Application of TCP / IP Network Protocol, Windows Sockets Network Communication Procedure over each network node computer The main mode of interaction is the client / server (Client / Server) mode. That is, the customer issues a service request to the server, and the server provides the corresponding service after receiving the request. Using this mode network application When communicating, the server side's network application needs to start first, and open a communication channel to inform the local host, which can be on a certain public address (reserved port, such as file transfer protocol FTP) 21) Receive customer requests. The client's network application is then launched and opens a communication channel to connect to the retention port of the host where the server is located. 2 is a Windows Sockets communication program on PC 1, PC 2, and PC 3, with a Windows Sockets communication program on PC 0 of the server, using a typical topology of communication using client / server mode. PC 0 Ethernet Switch or Hub PC 1 PC 2 PC 3 Figure 2 Using a Windows Sockets Communication Procedure Using Customer / Server Mode Interaction with a Windows Sockets Communication Procedure Using the computer used in the network topology to meet Windows 95 operation Configuration requirements. If there is condition, it is best to use Pentium or its microcomputer above, to configure memory to 16MB, and use a hard disk of 1GB. The computer on each node in the network needs to install the NIC and install the driver of the NIC through Windows 95 or other channels. In addition, if the network like Figure 2, if an Ethernet switch (SWITCH) is used, you need to purchase an Ethernet switch with a 100Mbps port and a 10Mbps port, such as the 3com company's Switch 1000, and pass the top 5 lines (UTP ) Connect the NIC on each computer into the corresponding port in the switch, ie, to access the PC 0 with 100 Mbps Ethernet card to the 100Mbps port, and access other computers into 3 10 Mbps ports. When configuring the network, you should first pass the network configuration items in the Windows 95 Control Panel and the settings of the shared properties in the Windows 95 Explorer, each computer node can find yourself and other computers in "Online Neighbors". Enable file resources to share each other. There is a lot of articles on this aspect, and there is no longer described here. To implement the data communication of the Windows Sockets application, only the file resources are not enough, but also to add TCP / IP protocols to the network configuration item in the Windows 95 Control Panel, give the corresponding IP address, these IPs The address is not repeated in the local area network built. Like the network in Figure 2, the Class C address is usually used.

Take the figure 2 network as an example. In the dialog box for the TCP / IP attributes, "IP Address" is selected "Specify IP Address", which is set to 255.255.0.0 in "subnet shield". In the case, the IP address of each computer 2, PC 3, and PC 3 can be set to 166.166.100. Third, the basic steps developed by the Windows Sockets communication program supports two types of sockets, mine, stream sockets (SOCK_STREAM), and data sets (SOCK_DGRAM). A stream socket is generally used for Windows Sockets communication programs that require precise transmission data. Flow socket provides a service-oriented, reliable, data unlikely, non-repetitive, and sequentially received data in sequence. Its internal traffic control, avoids over-limit of data flow, and data is considered to be byte stream, no length limit. The development of applications with stream socket has its basic steps, Figure 3 is the basic process and role of server side and customer-party communication programs. Server clients Create stream socket S1 = socket (...) to connect local addresses to S1 to S1, listening to connection Listen (S1, .. ...) Creating Flow Socket S = Socket (........) Receive connection and get new socket S2S2 = Accept (S1, ....) to connect the socket S with The server establishes the connector to connect connect (s, .....) on the socket s2, read / write data until writing / reading data on the socket s, data exchange is completed until the data exchanges RECV (S2, .....) Data Transmission Send (s, .....) Send (S2, .....) Recv (s, .....) Close socket S2 Close socket SCLOSSOCKET (S2) CloseSocket (S) Close Sink S1CloseSocket (S1) Figure 3 Server and Client Communication Program Basic Process and Role Relationship Although Visual C 4.0 has a series of packages for the original Windows Sockets library, but use Visual C 4.0 for Windows Sockets application development, its basic processes and role relationships are still similar to Figure 3. In Visual C 4.0, the method of developing a communication program such as CSocket, CSocketFile, Carchive is often combined, and the basic steps are set forth, as shown in FIG. Server Client (Client) 1, construct a socket 1, construct a socket CSocket Ser_S1; CSocket CLI_S; 2, create the socket 2, create the socket ser_s1.create (port) CLI_S.CREATE (); where port is the communication channel number 3 that is opened for the server, start listening to the connection ser_s1.listen (); 3, the client side socket CLI_S is issued to the server square sleeve SER_S1 Connection requests CLI_S .connect (addr, port); (where addr is to connect to the server square set address structure pointer, the IP address or machine name. PORT open channel number, its value is consistent with the server side.

4, construct a new socket CSocket Ser_S2; 5, the server waits for acceptance of the client connection request ser_s1.accept (Ser_s2); 6, constructing a class csocketfile object 4, constructing a class csocketfile object Csocketfile File & Ser_S2); CSocketfile File (& Cli_s); 7, Table Carchive Object Arin, AROUT 5, Table Carchive object Arin, arout, for data collection. For the receipt of the data. CARCHIVE ARIN (& File, CARCHIVE :: Load); CARCHIVE ARIN (& File, CARCHIVE :: Load); CARCHIVE AROUT (& File, Carchive :: Store); CARCHIVE AROUT (& File, Carchive :: Store); 8, using arin and arout Receiving or sending data 6, using arin and arout, arunt >> value; arout << value; arout << value; arin >> value; where Value is the transmitted data 9, destroying the created CSocket, 7, destroyed CSocket, CsocketFile, CSocketFile, CSocketFile, CAARCHIVE, etc. Object Distraction of CAOCKETFILE, CARCHIVE, etc. Take the basic step four developed by Visual C 4.0, using Visual C 4.0 for Windows Sockets program development Other technical points For Windows Sockets applications for using Visual C 4.0, you need to pay attention to the following: 1. Same as routine programming, regardless of server side or client applications, such as addR in Figure 4. The port default setting, etc., this part of the work can still be completed in terms of the message driver mechanism. 2. Under normal circumstances, the network communication program is a module in an application. When debugging the network communication program separately, try to agree with other application developers using the communication module, uniformly adopt an interface form, namely a single document interface SDI, multi-document interface MDI, and a dialog interface. (This has a prompt when using AppWizard to form a project [Project] file), although this is not necessary, but the communication module can save time and effort during porting to the desired application, because Visual C 4.0 This visualization language is giving us While providing us, it also brings us that some inconveniences, such as many related files in the project files, close contact with the interface used, many message drivers, and different interfaces. Of course, the communication module can also be functionally, and a dynamic connection library file (DLL file) can be formed, which is used to be mainly used. 3. The application of the communication program as one of the modules is often not doing other work after waiting for the data to send or receive, and thus uses multithreaded technologies in the main program. The order of data is to be sent, placed in a auxiliary thread with a certain priority (generally preferred), during the data or receipt, for example, using the previous cycle Data drawing curve.

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

New Post(0)