Experience and practice in developing Windows Sockets communication applications

Source: Internet
Author: User
Tags file transfer protocol

Abstract: This article describes the basic steps and technical points of developing the software and hardware environment for the Windows Sockets communication program. In the Windows 95 Environment, Visual C ++ 4.0 programming is used as an example, this section further describes the experiences and practices of developing a Windows SoC-kets communication program.

Key words socket Windows Sockets Computer Network Communication

I. Introduction

Socket was initially a network communication interface developed by the University of California Berkeley for UNIX operating systems. With the widespread use of UNIX operating systems, sockets have become one of the most popular network communication application interfaces. In early 1990s, several companies, including Sun Microsystems, JSB Corporation, FTP software, microdyne, and Microsoft, jointly developed a set of standards, namely the Windows Sockets specification.

Windows Sockets API is a network programming interface of Microsoft Windows. It inherits the main features of Berkeley sockets and expands it. These extensions mainly provide some asynchronous functions and support asynchronous selection of network events that comply with the Windows message driver features. These extensions enable application developers to compile software that complies with the windows programming mode, making it possible to develop high-performance network communication programs in windows.

A socket is actually a communication endpoint. With it, the socket application developed by the user can communicate with other socket applications through the network.

In recent years, with the popularity of computer networks and Windows 95, applications developed by many users need to realize data communication between networks. In this case, according to, using Visual C ++ 4.0 for Windows Sockets application development experience, this article describes the development of inter-network data communication programs.

2. Develop the software and hardware environment of the Windows Sockets network communication program

The operating system software can be windows 95 or Windows NT, because they all support Windows Sockets APIs. In the following introduction, we will take the development in Windows 95 as an example.

The commonly used programming language is Microsoft Visual C ++ 4.0, which is currently popular in terms of visualization and object-oriented technology. Visual c ++ 4.0 can be run in Windows 95 or Windows NT, its development system has added a fully integrated Windows-based development tool and a "visualized" user interface driver model based on the traditional C/C ++ development process. The Microsoft Foundation Class Library in Visual C ++ 4.0 is a series of C ++ classes, it encapsulates various functions for compiling applications for the Microsoft Windows operating system series. In terms of sockets, Visual C ++ 4.0 encapsulates a series of functions in the original Windows Sockets library, and then produces classes such as csocket and csocketfile, which encapsulate various functions related to sockets.

The network communication protocol used is generally TCP/IP. Both Windows 95 and Windows NT have this protocol. However, the developed network communication application cannot directly deal with the TCP/IP core, but the network application programming interface Windows Sockets API. The Windows Sockets API can communicate directly with the TCP/IP core. The TCP/IP Core Protocol, together with the network physical medium (such as the network card), is a facility that provides communication between network applications. The relationship 1 is shown in.

Network Communication Application

Windows Sockets API

TCP/IP Core Protocol

Physical Media

Figure 1 Relationship Between TCP/IP protocol core and network communication applications

In applications using TCP/IP protocol, the main mode of interaction between Windows Sockets network communication programs on the computers on each network node is the Client/Server mode. That is, the customer sends a service request to the server. After receiving the request, the server provides the corresponding service. When network applications in this mode communicate with each other, the network application of the server must be started first, and a channel is opened to inform the local host, it can receive customer requests at a recognized address (Reserved port, for example, file transfer protocol FTP is 21. The customer's network application is then started, and a channel is opened to connect to the reserved port of the host where the server is located.

Figure 2 is the Windows Sockets communication program on the client's PC 1, Pc 2, and PC 3. It communicates with the Windows Sockets communication program on the server's PC 0, A typical topology for communication in customer/Server mode.

PC 0

Ethernet switch or hub

PC 1 Pc 2 PC 3

Figure 2 Windows Sockets communication program using the Client/Server mode
The typical network topology used

The Computer Used in the network must meet the Configuration Requirements for running Windows 95. If conditions are met, it is best to use a Pentium or above-grade microcomputer, the memory is configured to 16 MB, and around 1 GB of hard disk.

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 a network like Figure 2 uses an Ethernet switch, you need to purchase an Ethernet switch with a 1000 Mbps port and several 10 Mbps ports, such as switch of 3com, connect the network adapter of each computer to the corresponding port of the switch through five types of lines (UTP), that is, connect the PC 0 with a Mbps Ethernet card to the port of Mbps, connect other computers to three 10 Mbps ports.

When configuring the network, you must first set the network configuration items in the Windows 95 control panel and the file Property Sharing settings in Windows 95 Resource Manager, enable each computer node to find itself and other computers in "Network neighbors" and share file resources with each other. There are already many articles about this, and I will not repeat them here.

To achieve data communication between Windows Sockets applications on the Internet, it is not enough to share file resources with each other. You must add the TCP/IP protocol to the network configuration items in the Windows 95 control panel, at the same time, the corresponding IP addresses are given. These IP addresses must be unique in the created LAN. As shown in figure 2, Class C addresses are generally used.

Take Network 2 as an example. In the dialog box that sets TCP/IP properties, select "specify IP Address" for "ip address ", when "subnet mask" is set to 255.255.0.0, the IP addresses of PC 0, Pc 1, Pc 2, and PC 3 can be set to 166.166.100.101, 166.166.100.102, 166.166.100.103, 166.166.100.104.

III. Basic steps for developing a Windows Sockets communication program

Windows Sockets supports two types of sockets: stream socket (sock_stream), and datagram socket (sock_dgram ). For Windows Sockets communication programs that require precise data transmission, streaming sockets are generally used. Streaming sockets provide a connection-oriented, reliable, error-free, non-repeated data transmission, and data receiving service in the sending order. It provides traffic control to avoid data flow exceeding the limit. At the same time, the data is considered as a byte stream with no length limit. The development of applications using streaming sockets has its basic steps. Figure 3 shows the basic process and functional relationship of the communication program between the server and the client.

Server Client

Create a streaming socket
S1 = socket (......)

Connect the local address to S1
BIND (S1 ,.....)

Listen to connections from clients
Listen (S1 ,.....)
Create a streaming socket
S = socket (........)
Receive the connection and obtain
New socket S2
S2 = accept (S1 ,....)
Replace socket s with the server
Establish a connection to the host
Connect (S ,.......)

On socket S2,
Read/write data until data is written/read on socket S,
Data exchange ends until data exchange ends.
Recv (S2,...) data transmission send (S ,.....)
Send (S2,...) Recv (S ,.....)

Disable socket S2 disable socket s
Closesocket (S2) closesocket (s)

Disable socket S1
Closesocket (S1)

Figure 3 basic processes and relationships of the communication program between the server and the client

Although visual c ++ 4.0 encapsulates a series of functions in the original Windows Sockets library, Visual C ++ 4.0 is used for Windows Sockets application development, the basic process and relationship are still similar to figure 3. In Visual C ++ 4.0, the basic steps of csocket, csocketfile, and carchive communication program development are described as an example.

Server 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 ();
The port is opened by the server.
Communication Channel Number

3. Start listening for connections from the client.
Ser_s1.listen ();

3. client socket cli_s to server
Socket ser_s1 sends a connection request
Cli_s. Connect (ADDR, Port );
(ADDR is used to connect to the server.
Socket address structure pointer, which can be IP Address
Address or machine name. Port is the opened channel number,
The value is the same as that of the server .)

4. construct a new socket
Csocket ser_s2;
5. The server is waiting for access from ser_s2.
Accept customer connection requests
Ser_s1.accept (ser_s2 );

6. Construct a csocketfile-like object 4. Construct a csocketfile-like object
Csocketfile file (& ser_s2); csocketfile file (& cli_s );

7. Construct carchive objects Arin and arout 5. Construct carchive objects Arin and arout.
To receive and send data. To receive and send data.
Carchive Arin (& file, carchive: load); carchive Arin (& file, carchive: load );
Carchive arout (& file, carchive: Store); carchive arout (& file, carchive: Store );

8. Use Arin and arout to receive or send data. 6. Use Arin and arout to send or receive data.
Arin> value; arout <value;
Arout <value; Arin> value;
Value indicates the data transmitted.

9. Destroy the created csocket, 7. Destroy the created csocket,
Csocketfile, carchive, and other class objects such as csocketfile and carchive
Figure 4 basic steps for Windows Sockets application development using Visual C ++ 4.0

4. Using Visual C ++ 4.0 for Windows Sockets Program Development
Technical Points

For Windows Sockets application development using Visual C ++ 4.0, pay attention to the following points:

1. As with conventional programming, both the server side and the client side must perform initialization, and set the ADDR and port defaults in 4, this part of work can still be completed in advance using a message-driven mechanism.

2. Generally, a network communication program is a module in an application. When debugging a network communication program separately, try to agree with other application developers using this communication module, and adopt a uniform interface form, that is, the single document interface SDI, multi-Document Interface MDI, and one type of dialog box-based interface (this is prompted when the project [project] file is formed using Appwizard), although this is not required, however, it can save time and effort for the communication module to port to the required applications, because visual c ++ 4.0 is not only convenient for us, but also inconvenient for us, for example, many related files in the project file are closely related to the interface format used. Many Message-driven functions vary with the Interface format used.

Of course, you can also function the communication module and form a dynamic connection library file (DLL file) for the main program to call.

3. applications that use communication programs as one module often do not wait for data to be sent or received before doing other work. Therefore, the multi-thread (multithreaded) technology should be used in the main program. Place data sending or receiving in a secondary thread with a certain priority (generally higher priority). During data sending or receiving, the main program can still perform other work, for example, use the data received in the previous cycle to draw a curve. In Visual C ++ 4.0, MFC provides many functions related to startup threads, management threads, synchronization threads, and termination threads.

4. In many cases, the communication module is required to receive and send data in real time. For example, if the called main program takes 0.5 seconds as a cycle, you need to perform the following operations during this period: receive data, use the received data for calculation, and send the calculation results to other computer nodes, cycle. Taking full advantage of the asynchronous message-based network event selection mechanism of Windows Sockets and using messages to drive data sending and receiving, we combined other measures, for example, if you place the data receiving and sending on a high-priority thread, in the software design, you should arrange a good sequence to avoid sending a large amount of data to the other party at the same time, ensure that the network has sufficient bandwidth and so on, and successfully implement real-time data transmission.

References:

1. Edited by Jiang Dongxing Lin ehua, Guide to designing Windows Sockets network programs
Tsinghua University Press September 1996
2. Edited by Hu daoyuan, information network system integration technology
Tsinghua University Press March 1996
3. Microsoft, Visual C ++ 4.0 tutorial
4. Online help on Visual C ++ 4.0 software

Analysis of some English words in the text:
Berkeley, Sun Microsystems, JSB Corporation, FTP software, microdyne, Microsoft, Appwizard, multithreaded

 

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.