CSimpleSocket

Implements a socket class with additional features: timer, text protocol...

[ SimpleEmailClient | Source | Keywords | Summary | Ancestors | All Members | Descendants ]

Quick Index

DESCRIPTION
USAGE
EXAMPLE
ADMINISTRATIVE
SEE ALSO

Class Summary

class CSimpleSocket : public CSocket

{

public:
BOOL Connect( LPCTSTR lpszHostAddress, UINT nHostPort, UINT uTimeOut );
CSimpleSocket(UINT uTimeOut );
~CSimpleSocket();
int ReceiveString(CString& str, int nFlags );
int ReceiveLine(CString& szLine, int nFlags );
int Receive(void* lpBuf, int nBufLen, int nFlags );
int Send(const void* lpBuf, int nBufLen, int nFlags );
int Send(CString &szString, int nFlags );
int Send(LPCTSTR lpszString, int nFlags );
void SetTimeOut(UINT uTimeOut );
UINT GetTimeOut() const;
protected:
}; // CSimpleSocket

Back to the top of CSimpleSocket


DESCRIPTION

This class provides an API for accessing Windows Socket similar to the MFC CSocket class, but with additional features:

CAUTION: do not share instances of this class between threads. This class is derived from CSocket is suffers the same limitations as its base class (as described in MSDN articles Q175668 and Q140527).

Back to the top of CSimpleSocket


USAGE

To use this class:

Back to the top of CSimpleSocket


EXAMPLE

    // Create an instance
    CSimpleSocket oSocket;

// Create the socket if (oSocket.Create()) { // Connect to the server if (oSocket.Connect(m_szServerName, m_uPortNumber)!=0) { // Set timeout value to 30 seconds oSocket.SetTimeOut(30000);

CString strResponse; // Read response from the server if (oSocket.ReceiveLine(strResponse) != SOCKET_ERROR) {

// Send data to server oSocket.Send("QUIT\r\n");

} } }

// socket is closed automatically when the object oSocket is deleted.

Back to the top of CSimpleSocket


ADMINISTRATIVE

Author Emmanuel KARTMANN

Date Tuesday 11/17/98

Back to the top of CSimpleSocket


SEE ALSO

CSocket CAsyncSocket
MSDN articles Q175668 and Q140527

Back to the top of CSimpleSocket


BOOL Connect( LPCTSTR lpszHostAddress, UINT nHostPort, UINT uTimeOut );

Purpose: establish a connection to an unconnected stream or datagram socket.

Parameters:

in lpszHostAddress
network address of the socket to which this object is connected: a machine name such as "ftp.microsoft.com", or a dotted number such as "128.56.22.8".
in nHostPort
The port identifying the socket application
in uTimeOut
timeout value (in milliseconds)

Return value : BOOL = TRUE for success, FALSE otherwise.

Description : overrides CAsyncSocket::Connect (implements timer with multithreading)

    BOOL Connect( LPCTSTR lpszHostAddress, UINT nHostPort, UINT uTimeOut = INFINITE);

Back to the top of CSimpleSocket


CSimpleSocket(UINT uTimeOut );

Purpose: create an instance of the class

Parameters:

in uTimeOut
default timeout value (in milliseconds)

Return value : none (C++ constructor)

Description :

    CSimpleSocket(UINT uTimeOut = 0);

Back to the top of CSimpleSocket


~CSimpleSocket();

Purpose: delete an instance of the class

Parameters: none (C++ destructor)

Return value : none (C++ destructor)

Description :

    ~CSimpleSocket();

Back to the top of CSimpleSocket


int ReceiveString(CString& str, int nFlags );

Purpose: receives data (string only) from the socket.

Parameters:

in nFlags
option flag (same as for CSocket::Receive())
out str
the string received from socket

Return value : int = number of bytes received in case of success, SOCKET_ERROR otherwise.

Description : This function times out after (m_uTimeOut) milliseconds

    int ReceiveString(CString& str, int nFlags = 0);

Back to the top of CSimpleSocket


int ReceiveLine(CString& szLine, int nFlags );

Purpose: receives next line (string data only) from the socket.

Parameters:

in nFlags
option flag (same as for CSocket::Receive())
out szLine
the line received from socket

Return value : int = number of bytes received in case of success, SOCKET_ERROR otherwise.

Description : Reads data up to the next CRLF character. This function times out after (m_uTimeOut) milliseconds

    int ReceiveLine(CString& szLine, int nFlags = 0);

Back to the top of CSimpleSocket


int Receive(void* lpBuf, int nBufLen, int nFlags );

Purpose: receive data from a socket (with timeout)

Parameters:

out lpBuf
A buffer for the incoming data
in nBufLen
The length of lpBuf in bytes
in nFlags
Specifies the way in which the call is made (see CAsyncSocket::Receive() for details)

Return value : int = If no error occurs, Receive returns the number of bytes received. If the connection has been closed, it returns 0. Otherwise, a value of SOCKET_ERROR is returned

Description : overrides method CAsyncSocket::Receive()

    virtual int Receive(void* lpBuf, int nBufLen, int nFlags = 0);

Back to the top of CSimpleSocket


int Send(const void* lpBuf, int nBufLen, int nFlags );

Purpose: send data to the socket (with timeout)

Parameters:

in lpBuf
A buffer containing the data to be transmitted
in nBufLen
The length of the data in lpBuf in bytes
in nFlags
Specifies the way in which the call is made (see CAsyncSocket::Send() for details)

Return value : int = If no error occurs, Send returns the total number of characters sent. Otherwise, a value of SOCKET_ERROR is returned.

Description : overrides method CAsyncSocket::Send()

    virtual int Send(const void* lpBuf, int nBufLen, int nFlags = 0);

Back to the top of CSimpleSocket


int Send(CString &szString, int nFlags );

Purpose: send string to the socket (with timeout)

Parameters:

in szString
A string to be transmitted
in nFlags
Specifies the way in which the call is made (see CAsyncSocket::Send() for details)

Return value : int = If no error occurs, Send returns the total number of characters sent. Otherwise, a value of SOCKET_ERROR is returned.

Description : overrides method CAsyncSocket::Send()

    virtual int Send(CString &szString, int nFlags = 0);

Back to the top of CSimpleSocket


int Send(LPCTSTR lpszString, int nFlags );

Purpose: send string to the socket (with timeout)

Parameters:

in lpszString
A pointer to a string to be transmitted
in nFlags
Specifies the way in which the call is made (see CAsyncSocket::Send() for details)

Return value : int = If no error occurs, Send returns the total number of characters sent. Otherwise, a value of SOCKET_ERROR is returned.

Description : overrides method CAsyncSocket::Send()

    virtual int Send(LPCTSTR lpszString, int nFlags = 0);

Back to the top of CSimpleSocket


void SetTimeOut(UINT uTimeOut );

Purpose: set the timeout value for all socket operations

Parameters:

in uTimeOut
timeout value, in milliseconds (0 for infinite wait)

Return value : none

Description : Send/Receive methods use the timeout value in their implementation.

    void SetTimeOut(UINT uTimeOut = 0);

Back to the top of CSimpleSocket


UINT GetTimeOut() const;

Purpose: returns the timeout value for all socket operations

Parameters: none

Return value : UINT = timeout value, in milliseconds (0 for infinite wait)

Description :

    UINT GetTimeOut() const;

Back to the top of CSimpleSocket


All Members

public:
BOOL Connect( LPCTSTR lpszHostAddress, UINT nHostPort, UINT uTimeOut );
int ReceiveString(CString& str, int nFlags );
int ReceiveLine(CString& szLine, int nFlags );
int Receive(void* lpBuf, int nBufLen, int nFlags );
int Send(const void* lpBuf, int nBufLen, int nFlags );
int Send(CString &szString, int nFlags );
int Send(LPCTSTR lpszString, int nFlags );
void SetTimeOut(UINT uTimeOut );
UINT GetTimeOut() const;
protected:

Back to the top of CSimpleSocket


Ancestors

Inheritance chain for CSimpleSocket:

Back to the top of CSimpleSocket


Descendants

Class is not inherited by any others.

Back to the top of CSimpleSocket


Generated from source by the Cocoon utilities on Tue Feb 08 17:47:03 2000 .

Report problems to jkotula@stratasys.com