Download Source Code

CWinSocket Class Reference

Windows Socket API wrapper class. More...

#include <WinSocket.h>

List of all members.

Public Member Functions

 CWinSocket ()
 Default Constructor.
virtual ~CWinSocket ()
 Destructor.
void Destroy ()
 Closes the socket and releases related resources.
BOOL Attach (SOCKET hSocket)
 Attaches to existing socket handle.
void Detach ()
 Detaches from existing socket handle without releasing it.
BOOL IsSocket ()
 Returns non-zero if the class contains a valid socket handle.
SOCKET GetSocketHandle ()
 Returns a handle to the socket.
 operator SOCKET ()
 Returns a handle to the socket.
virtual BOOL Create (int af=AF_INET, int type=SOCK_STREAM, int protocol=0)
 Creates a new socket handle.
UINT GetLastError ()
 Returns the most recent error code.
LPCTSTR GetLastErrorStr (LPSTR pStr, LPCTSTR pTemplate=NULL)
 Returns a string describing the last error.
BOOL Bind (UINT uPort)
 Binds the open socket to the specified Port.
BOOL Listen (UINT uMaxConnections=32)
 Creates a socket listening on the bound port.
virtual BOOL Connect (sockaddr_in *pSa, UINT uSize=sizeof(SOCKADDR))
 Connects to the specified address.
BOOL Connect (LPCTSTR pAddress, UINT uPort)
 Address of remote peer.
BOOL GetHostByName (LPCTSTR pHost, LPHOSTENT *pHe, UINT *puPort=NULL)
 Gets host address information from DNS server.
BOOL EventSelect (WSAEVENT hEvent, long lEvents=FD_READ|FD_WRITE|FD_ACCEPT|FD_CONNECT|FD_CLOSE)
 Selects which events will generate callbacks.
BOOL EventSelect (long lEvents=FD_READ|FD_WRITE|FD_ACCEPT|FD_CONNECT|FD_CLOSE)
 Selects which events will generate callbacks.
HANDLE GetEventHandle ()
 Retuns the current event handle.
BOOL IsEventHandle ()
 Returns non-zero if there is a valid event handle.
BOOL CreateEventHandle ()
 Creates a network event handle.
void CloseEventHandle ()
 Closes the event handle.
BOOL WaitEvent (WSAEVENT hEvent, long lEventId=0xffffffff, LPWSANETWORKEVENTS pWne=NULL, UINT uTimeout=INFINITE)
 Waits for a socket event to occur.
BOOL WaitEvent (long lEventId=0xffffffff, LPWSANETWORKEVENTS pWne=NULL, UINT uTimeout=INFINITE)
 Waits for a socket event to occur.
UINT GetEventBit (long lEventMask)
 Returns the bit offset for the specified event.
UINT Recv (LPVOID pData, UINT uSize, UINT *puRead=NULL, UINT uFlags=0)
 Reads data from the socket.
UINT Send (const LPVOID pData, UINT uSize, UINT *puSent=NULL, UINT uFlags=0)
 Writes data to the socket.
UINT Send (LPCTSTR pStr, UINT *puSent=NULL, UINT uFlags=0)
 Writes a NULL terminated string to the socket.
BOOL GetPeerName (LPSTR pName, LPDWORD pdwPort=NULL)
 Gets the remote socket information.
BOOL GetSocketName (LPSTR pName, LPDWORD pdwPort=NULL)
 Gets the local socket information.
BOOL IoCtlSocket (long cmd, LPDWORD pdw)
 Controls the i/o mode of the socket.
BOOL IoCtl (DWORD code, LPVOID pInbuf=NULL, DWORD dwInbuf=0, LPVOID pOutbuf=NULL, DWORD dwOutbuf=0, LPDWORD pBytesReturned=NULL)
 Controls Socket2 mode features of the socket.
BOOL EnableCircularQueueing ()
 Enables circular queueing.
BOOL FindRoute (LPSOCKADDR psa)
 Finds the route to the specified address.
BOOL Flush ()
 Flushes the socket data queue.
BOOL GetBroadcastAddress (LPSOCKADDR psa)
 Receives the current broadcast address.
BOOL GetQOS (LPQOS pqos)
 Returns current quality of service information.
BOOL GetGroupQOS (LPQOS pqos)
 Returns current quality of service information.
BOOL SetMultipointLoopback (BOOL bLoopback)
 Enables / disables multi-point loopback.
BOOL SetMultipointScope (DWORD scope)
 Sets multipoint scope.
BOOL SetQOS (LPQOS pqos)
 Sets quality of service information.
BOOL SetGroupQOS (LPQOS pqos)
 Sets group quality of service information.
BOOL SetSendBufferSize (DWORD size)
 Sets the send buffer size on the socket.
BOOL SetRecvBufferSize (DWORD size)
 Sets the receive buffer size on the socket.

Static Public Member Functions

static BOOL InitSockets (WORD ver=eMinSocketVersion)
 Initializes the Windows Socket API.
static void UninitSockets ()
 Uninitializes the Windows Socket API.
static BOOL IsInitialized ()
 Returns non-zero if the Windows Socket API was successfully initialized.


Detailed Description

Windows Socket API wrapper class.

Provides synchronous socket support

For most advanced applications, you will probably want the asynchronous capability of TWinAsyncSocket.

Example:

    CWinSocket ws;
    char szError[ 1024 ] = "";

    // Connect to web site
    if ( !ws.Connect( "google.com", 80 ) || !ws.WaitEvent( FD_CONNECT ) )

        printf( ws.GetLastErrorStr( szError ) );

    else
    {
        // Send the page request
        ws.Send( "GET / HTTP/1.0\r\n\r\n" );

        // Wait for data
        if ( m_ws.WaitEvent( FD_READ ) )
        {
            char szBuf[ 8 * 1024 ] = "";

            // Read in page data
            UINT uRead = ws.Recv( szBuf, sizeof( szBuf ) );

            printf( szBuf );

        } // end if

    } // end if

See also:
CWinAsyncSocket, CWinSocketProtocol, CWinSocket


Constructor & Destructor Documentation

CWinSocket::~CWinSocket  )  [virtual]
 

Destructor.

Lose the current socket


Member Function Documentation

BOOL CWinSocket::Attach SOCKET  hSocket  )  [inline]
 

Attaches to existing socket handle.

Parameters:
[in] hSocket - Existing socket handle
Returns:
Returns non-zero if success

BOOL CWinSocket::Bind UINT  uPort  ) 
 

Binds the open socket to the specified Port.

Parameters:
[in] uPort - Port to bind to.
Returns:
Returns non-zero if success.
See also:
Create(), Listen(), Connect()

BOOL CWinSocket::Connect LPCTSTR  pAddress,
UINT  uPort
 

Address of remote peer.

Parameters:
[in] pAddress - URL formed address of remote peer.
[in] uPort - Port of remote peer.
Returns:
Returns non-zero if success.

BOOL CWinSocket::Connect sockaddr_in *  pSa,
UINT  uSize = sizeof(SOCKADDR)
[virtual]
 

Connects to the specified address.

Parameters:
[in] pSa - The network address of the target computer.
[in] uSize - Size of the structure in psai.
If psai is NULL, the member variable m_sai is used as the target address.

Returns:
Returns non-zero if success, otherwise zero.

BOOL CWinSocket::Create int  af = AF_INET,
int  type = SOCK_STREAM,
int  protocol = 0
[virtual]
 

Creates a new socket handle.

Parameters:
[in] af - Address family specification.
[in] type - The type specification.
[in] protocol - The protocol to be used with the socket.
Returns:
Returns non-zero if success.
See also:
Bind(), Listen(), Connect()

BOOL CWinSocket::EnableCircularQueueing  )  [inline]
 

Enables circular queueing.

Returns:
Returns non-zero if success.

BOOL CWinSocket::EventSelect long  lEvents = FD_READ | FD_WRITE | FD_ACCEPT | FD_CONNECT | FD_CLOSE  )  [inline]
 

Selects which events will generate callbacks.

Parameters:
[in] lEvents - The events to hook.
Returns:
Returns non-zero if success.

BOOL CWinSocket::EventSelect WSAEVENT  hEvent,
long  lEvents = FD_READ | FD_WRITE | FD_ACCEPT | FD_CONNECT | FD_CLOSE
[inline]
 

Selects which events will generate callbacks.

Parameters:
[in] hEvent - The event handle
[in] lEvents - The events to hook.
Returns:
Returns non-zero if success.

BOOL CWinSocket::FindRoute LPSOCKADDR  psa  )  [inline]
 

Finds the route to the specified address.

Parameters:
[in] psa - Socket address whose route to find.
Returns:
Returns non-zero if success.

BOOL CWinSocket::Flush  )  [inline]
 

Flushes the socket data queue.

Returns:
Returns non-zero if success.

BOOL CWinSocket::GetBroadcastAddress LPSOCKADDR  psa  )  [inline]
 

Receives the current broadcast address.

Parameters:
[out] psa - Receives the current broadcast address
Returns:
Returns non-zero if success.

UINT CWinSocket::GetEventBit long  lEventMask  ) 
 

Returns the bit offset for the specified event.

Parameters:
[in] lEventMask - Event mask
Returns:
Bit offset for specified event
See also:

BOOL CWinSocket::GetGroupQOS LPQOS  pqos  )  [inline]
 

Returns current quality of service information.

Parameters:
[out] pqos - Recieves quality of service information.
Returns:
Returns non-zero if success.

BOOL CWinSocket::GetHostByName LPCTSTR  pHost,
LPHOSTENT *  pHe,
UINT *  puPort = NULL
 

Gets host address information from DNS server.

Parameters:
[in] pHost - Address to lookup.
[out] pHe - Receives the address information.
[out] puPort - Receives the TCP port specified in pHost
Returns:
Returns non-zero if success.

LPCTSTR CWinSocket::GetLastErrorStr LPSTR  pStr,
LPCTSTR  pTemplate = NULL
[inline]
 

Returns a string describing the last error.

Parameters:
[in] pStr - Receives the error string
[in] pTemplate - Optional string template
Returns:
Pointer in pStr
See also:
CWin32::GetSystemErrorMsg()

BOOL CWinSocket::GetPeerName LPSTR  pName,
LPDWORD  pdwPort = NULL
 

Gets the remote socket information.

Parameters:
[out] pName - Receives the remote address of the connected socket.
[out] pdwPort - Receives the remote TCP port of the connected socket.
Returns:
Returns non-zero if success.

BOOL CWinSocket::GetQOS LPQOS  pqos  )  [inline]
 

Returns current quality of service information.

Parameters:
[out] pqos - Recieves quality of service information.
Returns:
Returns non-zero if success.

BOOL CWinSocket::GetSocketName LPSTR  pName,
LPDWORD  pdwPort = NULL
 

Gets the local socket information.

Parameters:
[out] pName - Receives the local address of the connected socket.
[out] pdwPort - Receives the local TCP port of the connected socket.
Returns:
Returns non-zero if success.

BOOL CWinSocket::InitSockets WORD  ver = eMinSocketVersion  )  [static]
 

Initializes the Windows Socket API.

Warning:
You must call this function before any other socket functions! Best if called from your application startup code. Call UninitSockets() before your application shuts down and after all instances of this class have been closed.
Returns:
Returns non-zero if success.

BOOL CWinSocket::IoCtl DWORD  code,
LPVOID  pInbuf = NULL,
DWORD  dwInbuf = 0,
LPVOID  pOutbuf = NULL,
DWORD  dwOutbuf = 0,
LPDWORD  pBytesReturned = NULL
[inline]
 

Controls Socket2 mode features of the socket.

Parameters:
[in] code - The code of the operation to be performed.
[in] pInbuf - Pointer to the input buffer.
[in] dwInbuf - Size of the buffer in pInBuf.
[in] pOutbuf - Pointer to the output buffer.
[in] dwOutbuf - Size of the buffer in pOutbuf.
[in] pBytesReturned - Number of bytes returned in pOutbuf.
See the Windows Socket API function WSAIoctl() for more details.

Returns:
Returns non-zero if success.

BOOL CWinSocket::IoCtlSocket long  cmd,
LPDWORD  pdw
[inline]
 

Controls the i/o mode of the socket.

Parameters:
[in] cmd - The command to perform on the socket.
[in,out] pdw - Input and/or return value for cmd.
See the Windows Socket API function ioctlsocket() for more details.

Returns:
Returns non-zero on success, otherwise zero.

BOOL CWinSocket::Listen UINT  uMaxConnections = 32  ) 
 

Creates a socket listening on the bound port.

Parameters:
[in] uMaxConnections - The maximum number of connections allowed.
Returns:
Returns non-zero if success, otherwise zero.
See also:
Create(), Bind(), Connect()

UINT CWinSocket::Recv LPVOID  pData,
UINT  uSize,
UINT *  puRead = NULL,
UINT  uFlags = 0
 

Reads data from the socket.

Parameters:
[in] pData - Receives the socket data
[in] uSize - Size of buffer in pData
[in] puRead - Receives the number of bytes read
[in] uFlags - Socket receive flags
Returns:
Number of bytes read or SOCKET_ERROR if failure.
See also:

UINT CWinSocket::Send LPCTSTR  pStr,
UINT *  puSent = NULL,
UINT  uFlags = 0
[inline]
 

Writes a NULL terminated string to the socket.

Parameters:
[in] pStr - Pointer to NULL terminated string
[in] puSent - Number of bytes sent
[in] uFlags - Socket write flags
Returns:
Number of bytes sent or SOCKET_ERROR if failure.
See also:

UINT CWinSocket::Send const LPVOID  pData,
UINT  uSize,
UINT *  puSent = NULL,
UINT  uFlags = 0
 

Writes data to the socket.

Parameters:
[in] pData - Buffer containing write data
[in] uSize - Size of the buffer in pData
[in] puSent - Receives the number of bytes written
[in] uFlags - Socket write flags
Returns:
Number of bytes sent or SOCKET_ERROR if failure.
See also:

BOOL CWinSocket::SetGroupQOS LPQOS  pqos  )  [inline]
 

Sets group quality of service information.

Parameters:
[out] pqos - Quality of service information.
Returns:
Returns non-zero if success.

BOOL CWinSocket::SetMultipointLoopback BOOL  bLoopback  )  [inline]
 

Enables / disables multi-point loopback.

Parameters:
[in] bLoopback - Set to non-zero to enable multi-point loopback.
Returns:
Returns non-zero if success.

BOOL CWinSocket::SetMultipointScope DWORD  scope  )  [inline]
 

Sets multipoint scope.

Parameters:
[in] scope - Multipoint scope value
Returns:
Returns non-zero if success.

BOOL CWinSocket::SetQOS LPQOS  pqos  )  [inline]
 

Sets quality of service information.

Parameters:
[out] pqos - Quality of service information.
Returns:
Returns non-zero if success.

BOOL CWinSocket::SetRecvBufferSize DWORD  size  )  [inline]
 

Sets the receive buffer size on the socket.

Parameters:
[in] size - The new size of the receive buffer
Returns:
Returns non-zero if success

BOOL CWinSocket::SetSendBufferSize DWORD  size  )  [inline]
 

Sets the send buffer size on the socket.

Parameters:
[in] size - The new size of the send buffer
Returns:
Returns non-zero if success

BOOL CWinSocket::WaitEvent long  lEventId = 0xffffffff,
LPWSANETWORKEVENTS  pWne = NULL,
UINT  uTimeout = INFINITE
[inline]
 

Waits for a socket event to occur.

Parameters:
[in] lEventId - Mask identifying event(s) to wait for.
[out] pWne - Information about event.
[in] uTimeout - Maximum time to wait in milli- seconds.
Returns:
Non-zero if a socket event occurs before the timeout.
See also:

BOOL CWinSocket::WaitEvent WSAEVENT  hEvent,
long  lEventId = 0xffffffff,
LPWSANETWORKEVENTS  pWne = NULL,
UINT  uTimeout = INFINITE
 

Waits for a socket event to occur.

Parameters:
[in] hEvent - Handle to network event
[in] lEventId - Mask identifying event(s) to wait for.
[out] pWne - Information about event.
[in] uTimeout - Maximum time to wait in milli- seconds.
Returns:
Non-zero if a socket event occurs before the timeout.
See also:


The documentation for this class was generated from the following files:
  • inc/WinSocket.h
  • WinSocket.cpp
Copyright Robert Umbehant
This documentation is covered by the LGPL