Here, I will introduce you to a set of C ++ socket classes, so that you can easily use sockets in Windows programs. In addition, I would like to thank Fabien le lez, www.infflux.com, Tamas kaskoto, and other friends who have perfected the source code.
There are actually four classes:
1. socket (base class)
2. socketserver (derived class)
3. socketclient (derived class)
4. socketselect (youyuan)
HereSocketselectIt can be used for select calls on multiple sockets. In the sockets constructor, It is the protected permission.
When compiling these files, make sure to link the ws2_32.lib library.
Return to the header file, execution file, test program, and echo server. There is also a proxy server and a web server.
A brief description of this socket class: Each windows socket program will call wsastartup. This call is encapsulated in socket: start, and it is called the soket constructor. I want to install the V2.0 version here.
The socket constructor calls socket () to create a socket (in lower case ). It passes the sock_stream parameter to display TCP. If you want to use UDP, you must just pass the sock_dgram parameter.
Socketclient: Socketclient inherits from socket. Its constructor requires a host name as the parameter, which is what the Server Client wants to pass. This host name is resolved from gethostname () to an IP address.
Socketserver:Socketserver is also inherited from socket. The first parameter of its constructor indicates the listening port, and the second parameter indicates the maximum number of connections that can be processed;
The source code of the original author is as follows:
1. header file Declaration
/* <Br/> socket. h </P> <p> copyright (c) 2002-2004 René nyffenegger </P> <p> This source code is provided 'as-is ', without any express or implied <br/> warranty. in no event will the author be held liable for any damages <br/> arising from the use of this software. </P> <p> permission is granted to anyone to use this software for any purpose, <br/> including extends cial applications, and to alter it and R Edistribute it <br/> freely, subject to the following restrictions: </P> <p> 1. the origin of this source code must not be misrepresented; you must not <br/> claim that you wrote the original source code. if you use this source code <br/> in a product, an acknowledgment in the product documentation wocould be <br/> appreciated but is not required. </P> <p> 2. altered source versions must be plainly mark Ed as such, and must not be <br/> misrepresented as being the original source code. </P> <p> 3. this notice may not be removed or altered from any source distribution. </P> <p> René nyffenegger rene.nyffenegger@adp-gmbh.ch <br/> */</P> <p> # ifndef socket_h <br/> # define socket_h </P> <p> # include <winsock2.h> </P> <p> # include <string> </P> <p> Enum typesocket {blockingsocket, nonblockingsocket }; </P> <p> class socke T {<br/> Public: </P> <p> virtual ~ Socket (); <br/> socket (const socket &); <br/> socket & operator = (socket &); </P> <p> STD :: string pipeline Eline (); <br/> STD: String receivebytes (); </P> <p> void close (); </P> <p> // The parameter of sendline is not a const reference <br/> // because sendline modifes the STD: string passed. <br/> void sendline (STD: string); </P> <p> // The parameter of sendbytes is a const reference <br/> // because sendbytes Does not modify the STD: string passed <br/> // (in contrast to sendline ). <br/> void sendbytes (const STD: string &); </P> <p> protected: <br/> friend class socketserver; <br/> friend class socketselect; </P> <p> socket (socket S); <br/> socket (); </P> <p> socket S _; </P> <p> int * refcounter _; </P> <p> PRIVATE: <br/> static void start (); <br/> static void end (); <br/> static int nofsockets _; <br/>}; </P> <p> class Socketclient: Public socket {<br/> Public: <br/> socketclient (const STD: string & host, int port); <br/> }; </P> <p> class socketserver: Public socket {<br/> Public: <br/> socketserver (INT port, int connections, typesocket type = blockingsocket ); </P> <p> socket * accept (); <br/>}; </P> <p>/http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/Winsock/wsapiref_2tiq.asp <br/> class socketselect {<br/> Public: <br/> socketselect (socket const * const S1, socket const * const S2 = NULL, typesocket type = blockingsocket); </P> <p> bool readable (socket const * const S); </P> <p> PRIVATE: <br/> fd_set FDS _; <br/>}; </P> <p> # endif <br/>
2. cpp File
/* <Br/> socket. CPP </P> <p> copyright (c) 2002-2004 René nyffenegger </P> <p> This source code is provided 'as-is ', without any express or implied <br/> warranty. in no event will the author be held liable for any damages <br/> arising from the use of this software. </P> <p> permission is granted to anyone to use this software for any purpose, <br/> including extends cial applications, and to alter it and Redistribute it <br/> freely, subject to the following restrictions: </P> <p> 1. the origin of this source code must not be misrepresented; you must not <br/> claim that you wrote the original source code. if you use this source code <br/> in a product, an acknowledgment in the product documentation wocould be <br/> appreciated but is not required. </P> <p> 2. altered source versions must be plainly Ma Rked as such, and must not be <br/> misrepresented as being the original source code. </P> <p> 3. this notice may not be removed or altered from any source distribution. </P> <p> René nyffenegger rene.nyffenegger@adp-gmbh.ch <br/> */</P> <p> # include "socket. H "<br/> # include <iostream> </P> <p> using namespace STD; </P> <p> int socket: nofsockets _ = 0; </P> <p> void socket: Start () {<br/> If (! Nofsockets _) {<br/> wsadata Info; <br/> If (wsastartup (makeword (2, 0), & info )) {<br/> throw "cocould not start WSA"; <br/>}< br/> ++ nofsockets _; <br/>}</P> <p> void socket: end () {<br/> wsacleanup (); <br/>}</P> <p> socket: socket (): S _ (0) {<br/> Start (); <br/> // UDP: Use sock_dgram instead of sock_stream <br/> S _ = socket (af_inet, sock_stream, 0 ); </P> <p> If (S _ = invalid_socket) {<br/> thro W "invalid_socket"; <br/>}</P> <p> refcounter _ = new int (1); <br/>}</P> <p> socket:: socket (socket S): S _ (s) {<br/> Start (); <br/> refcounter _ = new int (1); <br/> }; </P> <p> socket ::~ Socket () {<br/> If (! -- (* Refcounter _) {<br/> close (); <br/> Delete refcounter _; <br/>}</P> <p> -- nofsockets _; <br/> If (! Nofsockets _) end (); <br/>}</P> <p> socket: socket (const socket & O) {<br/> refcounter _ = O. refcounter _; <br/> (* refcounter _) ++; <br/> S _ = O. S _; </P> <p> nofsockets _ ++; <br/>}</P> <p> socket & socket: Operator = (socket & O) {<br/> (* o. refcounter _) ++; </P> <p> refcounter _ = O. refcounter _; <br/> S _ = O. S _; </P> <p> nofsockets _ ++; </P> <p> return * this; <br/>}</P> <p> void socket: Close () {<br/> closesocket (S _); <br/>}</P> <P> STD: String socket: receivebytes () {<br/> STD: String ret; <br/> char Buf [1024]; </P> <p> while (1) {<br/> u_long Arg = 0; <br/> If (ioctlsocket (S _, fionread, & Arg )! = 0) <br/> break; </P> <p> If (ARG = 0) <br/> break; </P> <p> If (Arg> 1024) Arg = 1024; </P> <p> int Rv = Recv (S _, Buf, ARG, 0 ); <br/> If (RV <= 0) break; </P> <p> STD: String t; </P> <p> T. assign (BUF, RV); <br/> RET + = T; <br/>}</P> <p> return ret; <br/>}</P> <p> STD: String socket: Eline () {<br/> STD: String ret; <br/> while (1) {<br/> char R; </P> <p> switch (Recv (S _, & R, 1, 0 )) {<br/> case 0: // not connected anymore; <br/> //... but last line sent <br/> // might not end in/N, <br/> // so return ret anyway. <br/> return ret; <br/> case-1: <br/> return ""; <br/> // If (errno = eagain) {<br/> // return ret; <br/> //} else {<br/> // not connected anymore <br/> // return ""; <br/> // }< br/>}</P> <p> RET + = r; <br/> If (r = '/N ') return ret; <br/>}</P> <p> void socket: sendline (STD: String S) {<br/> S + = '/N'; <br/> send (S _, S. c_str (), S. length (), 0); <br/>}</P> <p> void socket: sendbytes (const STD: string & S) {<br/> send (S _, S. c_str (), S. length (), 0); <br/>}</P> <p> socketserver: socketserver (INT port, int connections, typesocket type) {<br/> sockaddr_in SA; </P> <p> memset (& SA, 0, sizeof (SA); </P> <p> SA. sin_family = pf_inet; <br/> SA. sin_port = htons (port); <br/> S _ = socket (af_inet, sock_stream, 0); <br/> If (S _ = invalid_socket) {<br/> throw "invalid_socket"; <br/>}</P> <p> If (type = nonblockingsocket) {<br/> u_long Arg = 1; <br/> ioctlsocket (S _, fionbio, & Arg ); <br/>}</P> <p>/* bind the socket to the Internet address */<br/> If (BIND (S _, (sockaddr *) & SA, sizeof (sockaddr_in) = socket_error) {<br/> closesocket (S _); <br/> throw "invalid_socket "; <br/>}</P> <p> listen (S _, connections); <br/>}</P> <p> socket * socketserver: accept () {<br/> socket new_sock = accept (S _, 0, 0); <br/> If (new_sock = invalid_socket) {<br/> int rc = wsagetlasterror (); <br/> If (rc = wsaewouldblock) {<br/> return 0; // non-blocking call, no request pending <br/>}< br/> else {<br/> throw "invalid socket "; <br/>}</P> <p> socket * r = new socket (new_sock); <br/> return R; <br/>}</P> <p> socketclient: socketclient (const STD: string & host, int port): socket () {<br/> STD :: string error; </P> <p> hostent * He; <br/> If (He = gethostbyname (host. c_str () = 0) {<br/> error = strerror (errno); <br/> throw error; <br/>}</P> <p> sockaddr_in ADDR; <br/> ADDR. sin_family = af_inet; <br/> ADDR. sin_port = htons (port); <br/> ADDR. sin_addr = * (in_addr *) He-> h_addr); <br/> memset (& (ADDR. sin_zero), 0, 8); </P> <p> If (: connect (S _, (sockaddr *) & ADDR, sizeof (sockaddr ))) {<br/> error = strerror (wsagetlasterror (); <br/> throw error; <br/>}</P> <p> socketselect:: socketselect (socket const * const S1, socket const * const S2, typesocket type) {<br/> fd_zero (& FDS _); <br/> fd_set (const_cast <socket *> (S1)-> S _, & FDS _); <br/> If (S2) {<br/> fd_set (const_cast <socket *> (S2)-> S _, & FDS _); <br/>}</P> <p> timeval tval; <br/> tval. TV _sec = 0; <br/> tval. TV _usec = 1; </P> <p> timeval * ptval; <br/> If (type = nonblockingsocket) {<br/> ptval = & tval; <br/>}< br/> else {<br/> ptval = 0; <br/>}</P> <p> If (select (0, & FDS _, (fd_set *) 0, (fd_set *) 0, ptval) = socket_error) <br/> throw "error in select "; <br/>}</P> <p> bool socketselect: readable (socket const * const s) {<br/> If (fd_isset (S-> S _, & FDS _) return true; <br/> return false; <br/>}< br/>
<Due to my limited level of English and programming, translation is inevitable to have shortcomings. Please forgive me and correct me!>
From: http://www.adp-gmbh.ch/win/misc/sockets.html