Try to encapsulate Socket libraries with C ++

xiaoxiao2021-03-06  65

I saw a few eyes in these two days, I always didn't have a long, always see the West. Also old love to pick the magic, drill the horns, it is really unhappy. This is not, starting to see Socket is not pleasing. At that time, I saw a friend who gave me a Socket Raw programming code. I saw it very disgusting. as follows:

IF

ErrorCode

=

WSAStartup

(

Makeword

(

2

,

1

), &

WSADATA

)))! =

0

) {

PRINTF

(

"WSASTARTUP FAILED:% D / N"

,

ErrorCode

Return

2

}

Sockmain

=

WSasocket

(

AF_INET

,

SOCK_RAW

,

Ipproto_raw

,

NULL

,

0

,

WSA_FLAG_OVERLAPPED

); If

Sockmain

==

INVALID_SOCKET

) {

PRINTF

(

"Socket Failed:% D / N"

,

Wsagetlasterror

()); Return

3

}

ErrorCode

=

Setsockopt

(

Sockmain

,

Ipproto_ip

,

IP_HDRINCL

, (

charr

*) &

Flag

,

Sizeof

(int)); IF

ErrorCode

==

Socket_ERROR

) {

PRINTF

(

"SET SOCKOPT FAILED:% D / N"

,

Wsagetlasterror

()); Return

4

}

ErrorCode

=

Setsockopt

(

Sockmain

,

SOL_Socket

,

SO_SNDTIMEO

, (

charr

*) &

TIMEOUT

,

Sizeof

(

TIMEOUT

)); If

ErrorCode

==

Socket_ERROR

) {

PRINTF

(

"SET SOCKOPT TIME OUT FAILED:% D / N"

,

Wsagetlasterror

()); Return

5

}

Maybe many people think my nerve, is this the only way to every Socket program? If this is very difficult to suffer, how do you write a large Socket program? Maybe, I said that I am a dead brain, always like it looks good. I always feel that each function is called, following a bunch of returning to the wrong handling statement is very ridiculous, why don't you use an exception to make these more elegant? Elegance on the code also means a decrease in the chance of error.

Coupled with the suspicion of the C abnormal mechanism that Monmelon, it is still unable to understand. So I intend to try to use the exception mechanism to complete the error processing similar to the above code, with the C encapsulated Winsock function library, and later based on this Socket C class library, write some communication programs, thus exposing the disadvantage of some abnormal mechanisms, let It is fully understood to understand some of the harm of an abnormal mechanism. Now my understanding is too shallow, not deep enough.

My current feeling is that the exceptions in the network communications are too normal, because you can't expect what will happen, where there is an error, so it is very suitable when using an abnormal mechanism. And I noticed that a lot of places returned, and there were many mistakes that did not have any ways to recover. Therefore, it should not be contrary to some rules. Try it. In fact, I originally wanted to find a STL similar class library, and the result was not found. Find an ACE result and scared me back, so big. There is no need to put it. I only need a simple Socket class library that simply encapsulates with C features. If someone has heard, please tell me, save me in whiteness here. Oh, but it is not white, it is very good, it is very good.

I didn't read the STL code, so I couldn't write STL or Boost style code, I can only follow my own imagination, repeated adjustment code. However, by encapsulating a class library, constantly DEBUG, constantly perfect, I will definitely understand C will be further. Maybe I will say "Oh? Who is writing the garbage code, it's unbearable, stupid to the extreme ... Wait, it seems to be I wrote" and then shameful. " Ha ha.

At present, I have implemented a simple Socket class library, accurately said that I can't make a class library, I can only call several socket functions with C , I can only call concept tests, but I hope there will be a day in the future. Can grow into a relatively simple, practical C Socket class library. There are 3 classes in this class.

Class

NETEXCEPTION

{

public

:

NETEXCEPTION

();

NETEXCEPTION

(Const)

Int ErrorCode

);

NETEXCEPTION

(Const)

String message

Const

Int msgid

);

Virtual

~

NETEXCEPTION

(

Void

);

String getErrorMessage

(Const)

DWORD ERRORCODE

Const;

protected

:

String m_message

;

INT M_MSGID

;

public

:

String getMessage

(

Void

Const {RETURN

m_message

}

Int getID

(

Void

Const {RETURN

m_msgid

;}};

The NetException class is a throw class for all the exceptions. If there are other libraries need, I will use this class as the base class to inherit. class NetEnviroment {public: NetEnviroment (void); NetEnviroment (const byte bHighVersion, const byte bLowVersion); ~ NetEnviroment (void); inline word MakeWord (const byte wHigh, const byte wLow) {return ((word) wHigh) << 8 | wLow;} inline word getVersion () const {return m_wsadata.wVersion;} inline word getHigherVersion () const {return m_wsadata.wHighVersion;} inline string getDescription () const {return string (m_wsadata.szDescription);} inline string getSystemStatus ( ) const {return string (m_wsadata.szSystemStatus);} protected: WSADATA m_wsadata; bool m_bState; public: void initial (const byte high, const byte low) {if (WSAStartup (MakeWord (high, low), & m_wsadata) = 0! Throw NetException (); m_bState = true;}}; NetENVIROMENT is an environment class for the entire Socket. Anyone who uses the network, only needs to place a NetEnviroment object before, everything is OK, and WSASTARTUP is called in the constructor. WSACLEANUP () will be called in the destructor.

class Socket {public: Socket (); Socket (const int protocol, const int type = SOCK_STREAM, const int af = AF_INET); Socket (const SOCKET s, const int protocol = IPPROTO_TCP, const int type = SOCK_STREAM, const int af = AF_INET); ~ Socket (void); protected: SOCKET m_socket; int m_af, m_type, m_protocol, m_port; SOCKADDR_IN m_remoteaddr; const static int bUF_SIZE = 4096; bool m_Connected; protected: void initSocket (const int protocol = IPPROTO_TCP, const int type = SOCK_STREAM, const int af = AF_INET); public: int Bind (const struct sockaddr * name, const int namelen); int Bind (const word Port); int Listen (const int backlog = SOMAXCONN); Socket Accept (void); INT Connect (Const String Hostname, Int Close; Int Send (Const Char * Buf, Const Len, Const Int Flags = 0); int send (const string message, const Int flags = 0) Int Receive (Char * BUF, Const Int Len, Const Int Flags = 0); String Receive const int flags = 0); int SendTo (const char * buf, const int len, const struct sockaddr * to, const int tolen, const int flags = 0); int ReceiveFrom (char * buf, const int len, struct sockaddr * from, int * fromlen, const int flags = 0); word getLocalPort (void) const; struct in_addr getLocalAddress (void) const; string getLocalAddressString (void) const; word getRemotePort (void) const; struct in_addr getRemoteAddress (void) const; String getRemoteaddressString (void) const; struct sockaddr_in & getsockname (socket s = null) const;}; ostream & operator << (Ostream & OS, Const Socket & S);

转载请注明原文地址:https://www.9cbs.com/read-107207.html

New Post(0)