Socket Programming Tutorial (iii) TCP principles: 2. design the TCP socket class (I)

Source: Internet
Author: User

As we mentioned in section 1st, socket is an int file descriptor (an abstract descriptor in Winsock). We operate on the socket by sending instructions to this descriptor. This is the idea of C language. In the object-oriented thinking, it is best that socket itself is an object, and various methods are developed by the object itself. It is not difficult to encapsulate a socket by using the object-oriented method, and the concept of describing the socket may be more intuitive. In this section, we will introduce the concept of socket and TCP and conduct oo encapsulation on the socket.
First, each socket object has a unique socket file descriptor, which can properly correspond to the concept of socket. So we construct a base class and make it a pure virtual function-this is because the socket file descriptor must be in a specific structure, then, we will keep an interface that returns the original socket file descriptor, which is reserved for functions in the class function for the convenience of the preparation. For example, the extremely important select () statement will be discussed later, this is a preparation.

Class basesock {
Protected:

Int sockfd;
Public:

Basesock ();

Virtual ~ Basesock () = 0;

Const Int & showsockfd () const;

};

Function implementation:

// Class basesock


Basesock: basesock ():

Sockfd (-1)

{}


Basesock ::~ Basesock ()

{}

Const Int & basesock: showsockfd () const

{

Return sockfd;

}

We set the initial value of sockfd to-1, indicating that this is an invalid file description symbol (file descriptor) When no derived class is constructed ).
Next, let's briefly review the establishment of TCP server in Section 1:
First, we need to establish a listener socket and then activate the listener;
Then, after the client connection information comes over, the client information is transmitted to the new socket through the listening port to establish the communication socket.
We first construct the listen socket:

Class tcplistensock: Public basesock {
PRIVATE:

Sockaddr_in listensockaddr;
Public:

Explicit tcplistensock (unsigned short listen_port );

~ Tcplistensock ();

Void tcplisten (

Int max_connection_requests = 10) const;

};

The purpose of tcplistensock is to passively wait for the client to find the handshake connect (), so as to collect the sock address information (including the IP address and port number) of the client ), then, it is passed to the new socket to establish a communication socket as needed.

Tcplistensock: tcplistensock (unsigned short listen_port)

{

Sockfd = socket (pf_inet,

Sock_stream,

Ipproto_tcp );

If (sockfd <0 ){

Sockclass: error_info ("socket () failed .");

}

Memset (& listensockaddr, 0, sizeof (listensockaddr ));

Listensockaddr. sin_family = af_inet;

Listensockaddr. sin_addr.s_addr = htonl (inaddr_any );

Listensockaddr. sin_port = htons (listen_port );

If (BIND (sockfd,

(Sockaddr *) & listensockaddr,

Sizeof (listensockaddr) <0 ){

Sockclass: error_info ("BIND () failed .");

}

}


Tcplistensock ::~ Tcplistensock ()

{

Close (sockfd );

}

Tcplistensock establishes sockfd by calling socket (); specify the listening port by specifying the port, which is required for the client to find this port. When the IP address is set to inaddr_any, it is actually 0, which means it can be the IP address of any server. Tcplistensock binds sockfd and sockaddr through BIND. This sockfd only has the sockaddr of the local machine, which means: 1. The connection cannot be established, only the datagram is accepted; 2. The information can only be accepted because the sockaddr of the remote destination cannot be sent.
This is sufficient and necessary for the TPC connection process. In fact, the first handshake datagram sent by the client is received by the sockfd, And the handshake response and handshake request returned to the client are sent by the new sockfd.
Listen () activates tcplistensock as a listener. If it is not activated, any handshake connection requests will be ignored by sockfd.

Void tcplistensock: tcplisten (

Int max_connection_requests) const

{

If (Listen (sockfd,

Max_connection_requests) <0 ){

Sockclass: error_info ("Listen () failed .");

}

}

This function seems to be a bit redundant, because the listener can be integrated into the constructor, that is, we can activate it once tcplistensock is established. In fact, this is exactly the practice in sdl_net, it also makes me feel less rigorous, because listening itself is a concept of socket.

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.