MFC Network Programming

Source: Internet
Author: User

Recently, I have been learning socket programming. Next I will write some related knowledge about my own learning, it is also a learning note (this article mainly refers to the teaching video and related documents of Sun Xin ).

The first is about winsocket network programming:

1. Socket Type:

There are mainly three types: 1. Streaming socket (sock_stream), which provides connection-oriented and reliable data transmission services. data can be sent without errors and repeatedly, and can be received in order, mainly implemented based on the TCP protocol


2. A datagram socket (sock_dgram) is mainly used to provide connectionless services. data packets can be sent independently without error guarantee, and data may be lost or duplicated, in addition, the order of receipt is chaotic. This type is mainly implemented based on UDP protocol, so the immediacy of this type is relatively high. (Chat communication software is generally implemented in this form)

3. The original socket (sock_raw), which must be used under the root, can accept data frames or data packets on the local Nic, it is very useful for listening to network traffic and analysis.

 

2. Programming process based on TCP (connection-oriented) socket:

1. server-side program process

A. Create a socket ).

B. Bind the socket to a local address and port (BIND ).

C. Set the socket to the listening mode and prepare to accept the client request (Listen ).

D. Wait for the customer's request to arrive. When the request arrives, accept the connection request and return a new socket (accept) corresponding to the connection)

E. Use the returned socket to communicate with the client (send/Recv ).

F. Return. Wait for another customer's request.

G. Disable socket

 

2. Client Programming process

A. Create a socket ).

B. Send a connection request to the server ).

C. Send/Recv ).

D. Close the socket.

 

Server programming process:

1. Create a socket ).

Socket socket (int af, // the specified address family. For TCP/IP socket, it can only be af_inet (or pf_inet) int type, // specifies the socket type, for socket 1.1, it supports two types of sockets. sock_stream specifies to generate a streaming socket, and sock_dgram specifies to generate a datagram socket int protocol // is a protocol related to a specific address family, if it is set to 0, the system automatically selects an appropriate protocol based on the address format and socket category (recommended)
); 

2. Bind the socket to a local address and port (BIND ).

Int BIND (socket S, // The socket const struct sockaddr far * name to bind, // specifies the local address information of the socket. This is a pointer variable pointing to the sockaddr structure, because the address structure is prepared for all address families, the structure usually varies with the network protocol used and the int namelen // specifies the length of the address structure );

A. The sockaddr structure is defined as follows (sockaddr_in structure can be used to replace sockaddr to facilitate address information ):

struct sockaddr {  u_short    sa_family;  char       sa_data[14];};  
struct sockaddr_in{ short sin_family;unsigned short sin_port; IN_ADDR sin_addr; char sin_zero[8];};

In addition, the sin_addr member type in the sockaddr_in structure is in_addr. Its structure definition is as follows:

struct in_addr {  union {          struct { u_char s_b1,s_b2,s_b3,s_b4; }   S_un_b;          struct { u_short s_w1,s_w2; }            S_un_w;          u_long                                   S_addr;  } S_un;};

The in_addr structure is actually a union. Generally, this structure is used to convert an IP address in the dotted-decimal format to the u_long type and assign the result to the member s_addr.

You can specify the IP address as inaddr_any, and allow the socket to send or accept data to any IP address assigned to the local machine.

B. inet_addr and inet_ntoa Functions

In most cases, each machine has only one IP address, but some machines may have multiple NICs. Each Nic can have its own IP address. Using inaddr_any can simplify programming, specifying the address as inaddr_any allows an Independent Application to receive responses from multiple interfaces. If we only want the socket to use one of multiple IP addresses, we must specify the actual address. To do this, we can use the inet_addr function. The prototype declaration of this function is as follows:

unsigned long inet_addr (const char *cp ); 

The inet_addr function requires a string as its parameter, which specifies an IP address in the dotted-decimal format. In addition, the inet_addr function returns a value of the u_long type suitable for s_addr.

The inet_ntoa function converts the value to the opposite. It accepts an in_addr struct type parameter and returns an IP address string in the dotted-decimal format. The prototype declaration of this function is as follows:

char * inet_ntoa (struct in_addr in ); 

3. Set the socket to the listening mode and prepare to accept the client request (Listen)

The listen function is used to set the specified socket to the listening mode. The Declaration prototype is as follows:

Int listen (socket S, // Maximum length of the socket descriptor int backlog waiting for the connection queue );

If the second backlog parameter is set to somaxconn, the lower-level service provider is responsible for setting this socket to the maximum reasonable value. Note that this value is set to set the maximum length of the waiting queue, rather than the number of connections that can be performed on a port at the same time. For example, if the backlog parameter is set to 2, when three requests arrive at the same time, the first two connection requests will be placed in the waiting request connection queue, the application then serves these requests in sequence, and the 3rd connection requests are rejected.

4. Wait for the customer's request to arrive. When the request arrives, accept the connection request and return a new socket (accept) corresponding to the connection ).

Socket accept (socket S, // socket descriptor, which has been set to the listening status struct sockaddr * ADDR through the listen function, // pointer to a buffer, this buffer is used to accept the address of the connection entity, that is, when the client initiates a connection to the server and the Server accepts the connection, saves the IP address and port information of the client that initiates the connection. int * addrlen // is also a return value. It points to an integer pointer and returns the length of the address information );

Not complete...

 

 

 

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.