Note: This blog with the previous blog <socket Programming Practice (>), as a result of more code, so divided into two, this chapter is the next, this article mainly explains how to use the enhanced version of the Socket class (serversocket/ Implementation and use of the Clientsocket Class)!
Source of thought:
1)http://www.cnblogs.com/-Lei/archive/2012/09/04/2670964.html
2)http://blog.csdn.net/column/details/linux66.html
3)http://blog.csdn.net/column/details/zjf666.html
Highlight content:
1) Added exception handling (when Serversocket/clientsocket inherits the parent socket, it also handles the possible errors of the parent function call, and throw it out, handled by code that uses these two classes), so that when we write code that is error-prone, You can emancipate your mind and don't always think about what happens when the function call goes wrong!
2) The Seversocket/clientsocket class overrides the Send/receive member function of the parent socket, making the two functions easier to use (as can be seen from the client-side code)
3) in the server-side code, follow the peer connect one process, add a fork call, and each subprocess processes a connection
4) added code to capture SIGCHLD on the server side to prevent zombie processes from appearing.
5) Add the Try/catch statement in the server/client-side code to handle the error situation.
ServerSocket class
In Serversocket.h#ifndef serversocket_h_included#define serversocket_h_included#include "Socket.h" #include " SocketException.h "Class Serversocket:public Socket{public: serversocket (int port, int backlog = somaxconn); Virtual ~serversocket (); void Accept (Socket &clientsocket) const; /**over write the Send & receive**/ size_t Send (const socket& Socket, const std::string& message) const;
size_t Receive (const socket& Socket, std::string& message) const;}; #endif//serversocket_h_included
In Serversocket.cpp#include "ServerSocket.h" serversocket::serversocket (int port, int backlog) {if (! Socket::create ()) {throw socketexception ("Create Server Socket Error"); } if (! Socket::bind (port) {throw socketexception ("Sever socket Bind a port error"); } if (! Socket::listen (backlog)) {throw socketexception ("Server socket Listen error"); }}serversocket::~serversocket () {}void serversocket::accept (Socket &clientsocket) const{if (! Socket::accept (Clientsocket)) {throw socketexception ("Accept a Connect error"); }}size_t serversocket::send (const Socket &socket, const std::string &message) const{size_t nsend = Sock Et::send (Socket,message); if (Nsend < 0) {throw socketexception ("Send messages Error"); } return nsend;} size_t serversocket::receive (const Socket &socket, std::string &message) const{size_t nread = Socket::R Eceive (Socket,message); if (nread = = 0) {throw socketexception ("Peer Connect closed"); } else if (Nread < 0) {throw socketexception ("Receive messages Error"); } return nread;}
Clientsocket class
In Clientsocket.h#ifndef clientsocket_h_included#define clientsocket_h_included#include "Socket.h" #include " SocketException.h "Class Clientsocket:public Socket{public: clientsocket (const std::string &serverhost, int Port); ~clientsocket (); size_t Send (const std::string& message) const; size_t Receive (std::string& message) const;}; #endif//clientsocket_h_included
In Clientsocket.cpp#include "ClientSocket.h" clientsocket::clientsocket (const std::string &serverhost, int port ) {if (! Socket::create ()) {throw socketexception ("Create Client socket error"); } if (! Socket::connect (Serverhost,port)) {throw socketexception ("Connect Server Socket Error"); }}clientsocket::~clientsocket () {}size_t clientsocket::send (const std::string &message) const{size_t nSend = Socket::send (Static_cast<const socket&> (*this), message); if (Nsend < 0) {throw socketexception ("Send messages Error"); } return nsend;} size_t clientsocket::receive (std::string &message) const{size_t nread = socket::receive (static_cast<cons T socket&> (*this), message); if (nread = = 0) {throw socketexception ("Peer Connect closed"); } else if (Nread < 0) {throw socketexception ("Receive messages Error"); } return nread;}
server end and client End full code
Server.cpp#include "ServerSocket.h" #include "SocketException.h" #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <signal.h> #include <iostream>using namespace std;void onsigchild (int signalnumber) {if (Signalnumber = = SIGCHLD) {while ( Wait (NULL)! =-1); }}int Main () {signal (sigchld,onsigchild); cout << "Server Running ..." << Endl; try {ServerSocket server (9001); while (true) {Socket newsocket; Server. Accept (Newsocket); cout << "Client information:" << Endl; cout << ' t ' << newsocket.getaddr () << Endl; cout << ' t ' << newsocket.getport () << Endl; pid_t pid = fork (); if (PID = =-1) {perror ("fork Error"); } else if (PID = = 0) { Server. Close (); while (true) {try {string messages; Server. Receive (newsocket,messages); cout << messages << Endl; Server. Send (newsocket,messages); } catch (SocketException &ex) {Cerr << ex. Description () << Endl; Exit (0); }}}} else {newsocket.close (); }}} catch (SocketException &ex) {Cerr << ex. Description () << Endl; } return 0;}
Client.cpp#include <iostream> #include <string> #include "ClientSocket.h" #include "SocketException.h" Using namespace Std;int Main () {cout<< "Running client ..." <<endl; try {clientsocket clientsocket ("127.0.0.1", 9001); try {string messages; while (Getline (cin,messages)) {if (messages.length () = = 0) { Messages.append ("\ n"); } clientsocket.send (Messages); Messages.clear (); Clientsocket.receive (Messages); cout << messages << Endl; }} catch (SocketException &ex) {Cerr << ex. Description () << Endl; }} catch (SocketException &ex) {Cerr << ex. Description () << Endl; } return 0;}
Run :
Attached -makefile
CC = g++ Cppflags =-wall-g-pthreadbin = Server Clientsources = $ (bin.=.cpp). Phony:clean All: $ (BIN) $ (BIN): $ (SOURCES) Socket.cpp ServerSocket.cpp Clientsocket.cppclean: -rm-rf $ (BIN) bin/ Obj/core
Socket Programming Practice (--socket) API Encapsulation (3)