Socket Programming Tutorial (1) TCP server: 7. complete source code in this Chapter

Source: Internet
Author: User
// Filename: tcpserverclass. HPP


# Ifndef tcpserverclass_hpp_included
# Define tcpserverclass_hpp_included


# Include <unistd. h>

# Include <iostream>

# Include <sys/socket. h>

# Include <ARPA/inet. h>

Class tcpserver

{
PRIVATE:

Int listensock;

Int communicationsock;

Sockaddr_in servaddr;

Sockaddr_in clntaddr;
Public:

Tcpserver (INT listen_port );

Bool isaccept ();

Void handleecho ();

};


# Endif // tcpserverclass_hpp_included

 

// Filename: tcpserverclass. cpp


# Include "tcpserverclass. HPP"


Tcpserver: tcpserver (INT listen_port)

{

If (listensock = socket (pf_inet, sock_stream, ipproto_tcp) <0 ){

Throw "socket () failed ";

}


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

Servaddr. sin_family = af_inet;

Servaddr. sin_addr.s_addr = htonl (inaddr_any );

Servaddr. sin_port = htons (listen_port );


If (BIND (listensock, (sockaddr *) & servaddr, sizeof (servaddr) <0 ){

Throw "BIND () failed ";

}


If (Listen (listensock, 10) <0 ){

Throw "Listen () failed ";

}

}

Bool tcpserver: isaccept ()

{

Unsigned int clntaddrlen = sizeof (clntaddr );


If (communicationsock = accept (listensock, (sockaddr *) & clntaddr, & clntaddrlen) <0 ){

Return false;

} Else {

STD: cout <"client (IP:" <inet_ntoa (clntaddr. sin_addr) <") connected. \ n ";

Return true;

}

}

Void tcpserver: handleecho ()

{

Const int buffersize = 32;

Char buffer [buffersize];

Int recvmsgsize;

Bool goon = true;


While (goon = true ){

If (recvmsgsize = Recv (communicationsock, buffer, buffersize, 0) <0 ){

Throw "Recv () failed ";

} Else if (recvmsgsize = 0 ){

Goon = false;

} Else {

If (send (communicationsock, buffer, recvmsgsize, 0 )! = Recvmsgsize ){

Throw "Send () failed ";

}

}

}


Close (communicationsock );

}


Demo program:

// Filename: Main. cpp
// TCP server C ++ style, single work


# Include <iostream>

# Include "tcpserverclass. HPP"

Int echo_server (INT argc, char * argv []);

Int main (INT argc, char * argv [])

{

Int mainrtn = 0;

Try {

Mainrtn = echo_server (argc, argv );

}

Catch (const char * s ){

Perror (s );

Exit (exit_failure );

}


Return mainrtn;

}

Int echo_server (INT argc, char * argv [])

{

Int port;

If (argc = 2 ){

Port = atoi (argv [1]);

} Else {

Port = 5000;

}


Tcpserver myserv (port );


While (true ){

If (myserv. isaccept () = true ){

Myserv. handleecho ();

}

}


Return 0;

}

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.