Socket.h
Go to the documentation of this file.
1 #pragma once
2 
7 namespace AppSecInc
8 {
9  namespace TcpIp
10  {
11  namespace Sockets
12  {
19  class Socket
20  {
21  protected:
22  SOCKET m_socket;
23  int m_error;
26  virtual void InternalSetSockOpt(int level, int optname, const char * optval, int optlen);
28  virtual void InternalIoCtlSocket(long cmd, u_long * argp);
30  virtual void InternalCreate(int af = AF_INET, int type = SOCK_STREAM, int protocol = IPPROTO_IP, LPWSAPROTOCOL_INFO lpProtocolInfo = NULL, GROUP g = NULL, DWORD dwFlags = 0);
32  virtual void InternalConnect(const struct sockaddr * name, int namelen, LPWSABUF lpCallerData = NULL, LPWSABUF lpCalleeData = NULL, LPQOS lpSQOS = NULL, LPQOS lpGQOS = NULL);
34  virtual void InternalShutdown(int how);
36  virtual void InternalClose();
37  public:
40  int GetError() const { return m_error; }
42  void ClearError() { m_error = 0; }
44  SOCKET Peek() const { return m_socket; }
46  bool IsCreated() const { return (m_socket != INVALID_SOCKET); }
48  bool IsConnected() const { return m_connected; }
50  void Select(bool read, bool write, bool except, const int timeout_in_seconds, bool& timed_out);
52  void SetHardClose(unsigned short timeout = 0);
54  void SetGracefulClose();
56  void SetSocketReuse();
58  void Socket::Connect(const std::wstring& ip_addr, unsigned port, int type = SOCK_STREAM, int protocol = IPPROTO_IP);
60  void Connect(const std::string& ip_addr, unsigned port, int type = SOCK_STREAM, int protocol = IPPROTO_IP);
62  void Connect(const struct sockaddr * name, int namelen);
64  void Disconnect();
66  void Bind(const std::string& ip_addr, unsigned port, int type = SOCK_STREAM, int protocol = IPPROTO_IP);
68  Socket();
69  virtual ~Socket();
70  };
71  }
72  }
73 }
void SetSocketReuse()
Turn on the resue address option. Allows the socket to be bound to an address that is already in use...
Definition: Socket.cpp:72
int GetError() const
Definition: Socket.h:40
A basic blocking socket.
Definition: Socket.h:19
Socket()
constructor
Definition: Socket.cpp:6
bool IsCreated() const
Returns true if the socket has been created.
Definition: Socket.h:46
virtual ~Socket()
Definition: Socket.cpp:14
void Disconnect()
Shutdown, receive out-of-band data and close the socket (disconnect from a remote host)...
Definition: Socket.cpp:124
virtual void InternalClose()
Close a socket.
Definition: Socket.cpp:49
virtual void InternalSetSockOpt(int level, int optname, const char *optval, int optlen)
Set a socket option.
Definition: Socket.cpp:31
void SetHardClose(unsigned short timeout=0)
Turn on the linger option. Closing a socket does not wait for for the session to breakdown, unless you have requested it by setting the linger socket option.
Definition: Socket.cpp:60
virtual void InternalIoCtlSocket(long cmd, u_long *argp)
Control socket IO.
Definition: Socket.cpp:40
bool m_connected
Definition: Socket.h:24
virtual void InternalConnect(const struct sockaddr *name, int namelen, LPWSABUF lpCallerData=NULL, LPWSABUF lpCalleeData=NULL, LPQOS lpSQOS=NULL, LPQOS lpGQOS=NULL)
Connect a socket. See WSAConnect or ::connect for more information.
Definition: Socket.cpp:92
void ClearError()
Clear the error from the last failed operation on this socket.
Definition: Socket.h:42
virtual void InternalShutdown(int how)
Shutdown a socket.
Definition: Socket.cpp:22
void Bind(const std::string &ip_addr, unsigned port, int type=SOCK_STREAM, int protocol=IPPROTO_IP)
Bind the socket to an address.
Definition: Socket.cpp:147
void Select(bool read, bool write, bool except, const int timeout_in_seconds, bool &timed_out)
The select function determines the status of the socket, waiting if necessary, to perform synchronous...
SOCKET Peek() const
Definition: Socket.h:44
int m_error
Definition: Socket.h:23
virtual void InternalCreate(int af=AF_INET, int type=SOCK_STREAM, int protocol=IPPROTO_IP, LPWSAPROTOCOL_INFO lpProtocolInfo=NULL, GROUP g=NULL, DWORD dwFlags=0)
Create a socket. See WSASocket or ::socket for more information.
Definition: Socket.cpp:78
void SetGracefulClose()
Turn off the linger option. Closing a socket does not wait for for the session to breakdown...
Definition: Socket.cpp:66
void Connect(const std::string &ip_addr, unsigned port, int type=SOCK_STREAM, int protocol=IPPROTO_IP)
Initialize the socket and connect to a destination. Defaults to TCP, specify SOCK_DGRAM for UDP...
Definition: Socket.cpp:109
bool IsConnected() const
Returns true if the socket has been successfuly connected.
Definition: Socket.h:48
SOCKET m_socket
Definition: Socket.h:22


© Application Security Inc. - All Rights Reserved http://msiext.codeplex.com