Windows Socket & socket in MFC

Source: Internet
Author: User
Tags file transfer protocol
I want to investigate a section in my recent work Program The migration from Windows 2000 to Windows Vista is a typical client/server program. The client provides FTP/Telent services for Windows. server and Linux server.
The client often needs to upload and download file data through FTP on the Linux server. when the client runs in the Win2000/XP environment, everything runs well, but in the Vista environment, the communication between the two is blocked, and the specific cause is analyzed through packet capture, when the Windows Socket API sends an FTP package, in the Vista machine, the packet sending is the hostname of the Local Machine, rather than the IP address.
Therefore, we need to analyze the inheritance classes involved in Windows Socket and casyncsocket in MFC, and find out where the problem occurs.
This article Article , Record the basic knowledge about network programming in Windows recently, and hope to solve the above problems in the near future.
In addition, Windows Vista is really too BT ........................ the prompt box in the pop-up dialog box will drive people crazy! Socket Definition

A socket is a communication endpoint. It is an object used by a Windows Sockets application to send or receive data packets over the network. The socket has a type, is associated with a running process, and can have a name. Currently, sockets exchange data only with other sockets in the same "communication domain" of the Internet Protocol group.

There are two types of available sockets:

    • Stream socket

      Stream sockets provide data streams without record boundaries, that is, byte streams. Byte streams ensure that they are delivered in the correct order without repetition.

    • Data PACKET socket

      The data PACKET socket supports record-oriented data streams, but it cannot be ensured to be delivered or in the sending order or not repeated.

Socket Data Type

Each MFC socket object encapsulates the handle of a Windows Sockets object. The data type of the handle isSocket.SocketThe handle is similar toHwnd. The MFC socket class provides operations on the encapsulation handle.

Socket programming model
    • Casyncsocket

      This class encapsulates Windows Sockets APIs. Casyncsocket is provided by programmers who understand network programming and want to flexibly program socket APIs directly, and also want to conveniently Use callback functions for network event notification. In addition to packaging sockets as object-oriented form for C ++, the only additional abstraction provided by this class is to convert some socket-related Windows messages into callbacks. For more information, see Windows Sockets: Socket notification.

    • Csocket

      SuchCasyncsocketDerived from, it provides more advanced abstraction for use with sockets through the MFC carchive object. The use of a socket with an archive is very similar to the file serialization protocol using MFC. This makes it betterCasyncsocketThe model is easier to use. Csocket slaveCasyncsocket(It encapsulates the Windows Sockets API) inherits many member functions. You will have to use some of these functions and have a general understanding of socket programming. WhileCsocketYou must use the original API orCasyncsocketClass to manage many aspects of communication. Most importantly,CsocketUse the background processing of Windows messages to provide blocking.CarchiveThe synchronization operation is critical.

 

Windows Sockets: streaming socket

Streaming sockets are generally used in TCP protocol sockets. For example, when FTP is created, all Telnet applications use streaming sockets, And the streaming sockets are created as sock_stream.
Streaming sockets are suitable for File Transfer Protocol (FTP), which facilitates transmission of ASCII or binary files of any size.

If the data must be delivered and the data size is large, the streaming socket is superior to the data PACKET socket.

For details about this example, see the following MFC examples: chatter and chatsrvr.

Operation Sequence of stream socket communication

In constructionCsocketfileBefore the object, the following sequence pairsCasyncsocketAndCsocketThey are all accurate (only a few parameters are different ). Slave ConstructionCsocketfileObject start. The order only appliesCsocket. The following table describes the operation sequence of communication between the client and the server.

Set the communication between the server and the client

Server Client
// Construct a socket

Csocket socksrvr;

// Construct a socket

Csocket sockclient;

// Create the socket

Socksrvr. Create (nport );1, 2

// Create the socket

Sockclient. Create ();2

// Start listening

Socksrvr. Listen ();

 
  // Seek a connection

Sockclient. Connect (straddr, nport );3, 4

// Construct a new, empty socket

Csocket sockrecv;

// Accept connection

Socksrvr. Accept (sockrecv );5

 
// construct file object

csocketfile file (& sockrecv );

// construct file object

csocketfile file (& sockclient );

// construct an archive

carchive Arin (& file,
carchive: load );

-or-

carchive arout (& file,
carchive: Store );

-or both-

// construct an archive

carchive Arin (& file,
carchive: load );

-or-

carchive arout (& file,
carchive: Store );

-or both-

// Use the archive to pass data:

Arin> dwvalue;

-Or-

Arout <dwvalue;6

// Use the archive to pass data:

Arin> dwvalue;

-Or-

Arout <dwvalue;6

1. Here'sNportIs the port number. For port details, see Windows Sockets: Port and socket address.

2. The server must always specify a port so that the client can connect.CreateAn address is also specified for a call. Use default parameters on the client. These parameters require that MFC use any available port.

3. Here'sNportYesport number,StraddrIs the computer address or Internet Protocol (IP) address.

4. The computer address can be in the following forms: "ftp.microsoft.com" and "Microsoft.com ". IP addresses are separated by numbers, for example, 127.54.67.32 ".ConnectFunction to check whether the address is a number separated by points (but it does not ensure that the number is a valid computer on the network ). If notConnectUse another form of computer name.

5. When called on the serverAcceptTo pass a reference to the new socket object. The object must be constructed first, but not called.Create. Note: If the socket object is out of the range, the connection is closed. MFC connects new objectsSocketHandle. This socket can be constructed on the stack (as shown in the table) or on the stack.

6. When the archive and socket files are out of range, they will be closed. When a socket object is out of the range or is deleted, the object's destructor also calls the close member function for this socket object.

Other Instructions on order

The call sequence shown in the preceding table applies to streaming sockets. The data PACKET socket is connectionless and does not require casyncsocket: connect, listen, or accept (but can be used selectively ).Connect). Conversely, if you are usingCasyncsocketClass, the data PACKET socket is usedCasyncsocket: sendtoAndReceivefromMember functions. (If you useConnect, UseSendAndReceive.) BecauseCarchiveIt is not applicable to data packets. If the socket is a data packet, do not use an archivedCsocket.

csocketfile does not support all cfile functions. cfile members (such as seek ) socket communication is meaningless and unavailable. Therefore, some default MFC serialize functions are not compatible with csocketfile . This is especially true for the ceditview class. Do not try to use ceditview :: serializeraw serialize ceditview data by attaching the carchive object to the csocketfile object, instead, use ceditview: serialize (no source ). The serializeraw function expects that the file object has functions not supported by csocketfile , such as seek .

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.