Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / MFC

CSPServer, State-based Protocol Server Class

Rate me:
Please Sign up or sign in to vote.
4.88/5 (14 votes)
11 Mar 20038 min read 146.1K   1.4K   71  
Class framework for creating client/server protocol servers
//***********************************************************************
// (c) Copyright 1999-2003 Santronics Software, Inc. All Rights Reserved.
//***********************************************************************
// File Name : socketio.h
// Subsystem : socket I/O wrapper
// Date      : 03/03/2003
// Author    : Hector Santos, Santronics Software, Inc.
// VERSION   : 1.00P
//
// Revision History:
// Version  Date      Author  Comments
// -------  --------  ------  -------------------------------------------
// v1.00P   03/03/03  HLS     Public Release version (non-SSL version)
//***********************************************************************

#ifndef __SOCKETIO_H
#define __SOCKETIO_H

#include <afx.h>
#include <winsock.h>

/////////////////////////////////////////////////////////////////////

class CSocketIO;

/**
 * class CSocketIO
 * this class abstracts a socket connection,
 */

class CSocketIO {
public:

    ~CSocketIO();

    /// plain vanila constructor for plain vanila sockets
    CSocketIO();

    /// plain socket constructor, takes a socket as argument
    CSocketIO(SOCKET s);

    ///  Connect to a remote address

    BOOL Connect(sockaddr_in src, 
                 sockaddr_in dest, 
                 const BOOL reservedport = FALSE);

    BOOL Connect(const TCHAR *address, 
                 int port,                   
                 const BOOL reservedport = FALSE);

    BOOL Connect(const TCHAR *faddress,
                 int fport,
                 const TCHAR *address,
                 int port,
                 const BOOL reservedport = FALSE);

    //  similar to connect() but resolves DNS address
    BOOL Open(const TCHAR *address,
              int port,
              const BOOL reservedport = FALSE);

    ///  Close socket
    virtual BOOL Close();

    ///  shutdown socket
    virtual BOOL Shutdown(int how = 1) { return shutdown(Socket,how); }

    ///  Is socket still connected?

    virtual BOOL Online() {return Socket != INVALID_SOCKET;};
    
    BOOL IsConnected(); // checks to see if socket is still valid
    int Select(const int secs = 0, const int usecs = 0);
    int getpeername(sockaddr *sAddr);
    int getsockname(sockaddr *sAddr);
    int setsockopt(int level, int optname, const char *optval, int optlen);
    int getsockopt(int level, int optname, char *optval, int *optlen);
    int GetSocketPort();

    ///  Read a character
    BOOL Recv(BYTE &c);
    
    ///  Read a block of data
    int Recv(BYTE *buf, int bufsize, DWORD timeoutms = 0);
    int recv(char *buf, int bufsize, DWORD flags = 0); // raw recv
    
    ///  Send a string
    DWORD Send(const char *s) { return Send(s, strlen(s)); }
    
    ///  Send a buffer
    DWORD Send(const char *buf, DWORD bufsize, DWORD flags=0);
    
    ///  send data over socket
    DWORD Sendf(const TCHAR *format, ...);  // 450.3b6 changed to DWORD return
    
    ///  Send a string
    DWORD Send(const wchar_t *s);
    int send(const char *buf, int len, int flags); // raw send
    
    ///  Like it says.  Wait for buffer to contain data
    DWORD WaitForReceivedData(DWORD timeoutms, HANDLE terminate=NULL);
    
    ///  get characters from socket until NULL is found
    BOOL RecvStr(char *buf, size_t maxlen, int timeout = 60);
    
    ///  get characters from socket until NULL is found
    BOOL RecvStr(wchar_t *buf, size_t maxlen, int timeout = 60);

    enum {MAX_OUTPUT = 2048};

    DWORD Peek();
    
    ///  return a block of data
    int GetBlock(char *data, 
                 int len, 
                 const char *state=NULL, 
                 int seconds=5*60, // 5 minutes
                 int milliseconds=0); 
    
    ///  are we connecting to ourselves?
    BOOL Ourselves();
    
    SOCKET GetSocket() { return Socket; }
    
    BOOL Listen(const char *address= NULL, WORD port = 0, WORD Backlog = 5);
    BOOL Accept();
    
    DWORD WSAGetLastError() { return ::WSAGetLastError();}

    virtual CheckIPSocketFilter(SOCKET SocketIn);
    
    CString GetPeerDomainName(const BOOL rdns = TRUE);
    
    DWORD GetLastStatus() { return LastStatus; }
    
    CString GetHostDomainName(const BOOL rdns = TRUE);
    
    void GetPeerInfo(CString &sDomain, CString &sIp, const BOOL rdns = TRUE);

protected:

    virtual BOOL CheckAbort() { return FALSE; }

    SOCKET Socket;        
    BOOL Connected;       
    int BufferIndex;      
    int BufferTop;        

private:
    DWORD LastStatus;     

    sockaddr_in sin;
    void Config(SOCKET s);
    enum {BUFFER_SIZE = 1024};
    BYTE Buffer[BUFFER_SIZE];
    BOOL FillBuffer(DWORD timeoutms);
    void InitSocketData();
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions