Click here to Skip to main content
11,721,385 members (82,427 online)
Click here to Skip to main content
Add your own
alternative version

CSPServer, State-based Protocol Server Class

, 11 Mar 2003 109.8K 1.3K 70
Class framework for creating client/server protocol servers
cspserver_src.zip
spserver
file_id.diz
SampleServer.dsp
SampleServer.dsw
//***********************************************************************
// (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

Share

About the Author

hector santos
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150901.1 | Last Updated 12 Mar 2003
Article Copyright 2003 by hector santos
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid