Main Page Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages
ssobjects::SocketInstance Class Reference
The socket that you use to connect with.
More...
#include <socketinstance.h>
Inheritance diagram for ssobjects::SocketInstance::
List of all members.
Public Methods |
| SocketInstance () |
SOCKET | getSocket () const |
void | setExceptionOnReadClose (const bool bThrowException=true) |
| Toggles exceptions on read closers. More...
|
void | cleanup () |
void | create (const int nType=SOCK_STREAM) |
void | close () |
void | bind (LPCSOCKADDR psa) |
void | listen () |
void | connect (LPCSOCKADDR psa) |
bool | accept (SocketInstance &s, LPSOCKADDR psa) |
bool | accept (SocketInstance *s, LPSOCKADDR psa) |
int | send (const char *pch, const int nSize, const int nSecs=DEFAULT_SOCKET_TIMEOUT) |
int | write (const char *pch, const int nSize, const int nSecs=DEFAULT_SOCKET_TIMEOUT) |
int | recv (char *pch, const int nSize, const int nSecs=DEFAULT_SOCKET_TIMEOUT) |
int | sendDatagram (const char *pch, const int nSize, LPCSOCKADDR psa, const int nSecs=10) |
int | receiveDatagram (char *pch, const int nSize, LPSOCKADDR psa, const int nSecs=10) |
void | getPeerAddr (LPSOCKADDR psa) |
void | getSockAddr (LPSOCKADDR psa) |
| operator SOCKET () |
SocketInstance & | operator= (const SocketInstance &s) |
| SocketInstance (const SocketInstance &s) |
Static Public Methods |
SockAddr | getHostByName (const char *pchName, const USHORT ushPort=0) |
const char * | getHostByAddr (LPCSOCKADDR psa) |
Public Attributes |
SOCKET | m_hSocket |
bool | m_bThrowExceptionOnReadClose |
Detailed Description
The socket that you use to connect with.
The SocketInstance object is the lowest level in the socket layer. It provides access methods to read and write to a socket un TCP/IP or UDP mode.
Typical client session would look like this:
...
SocketInstance sConnection;
try
{
//open connection
puts("Connecting to server...");
SockAddr saServer(szHost,iPort);
if(isalpha(szHost[0])) //check if szHost is in "host.com" format or "255.255.255.255"
saServer = SocketInstance::getHostByName(szHost,iPort);
sConnection.create();
sConnection.connect(saServer);
//we are connected, do something useful
...
sConnection.close();
}
catch(GeneralException* e)
{
sConnection.cleanup();
LOG("error s",e->getErrorMsg());
}
...
A server would look like this:
...
SocketInstance sClient;
SockAddr saClient;
SockAddr saServer(INADDR_ANY,atoi(argv[1]));
SocketInstance sListen;
try
{
sListen.create();
sListen.bind(saServer);
sListen.listen();
while(bWantMoreConnections)
{
sListen.accept(sClient,saClient);
//we got a connection ...
sClient.close();
}
sListen.close();
}
catch(GeneralException* e)
{
sConnection.cleanup();
LOG("error s",e->getErrorMsg());
}
...
Constructor & Destructor Documentation
ssobjects::SocketInstance::SocketInstance |
( |
|
) |
[inline] |
|
ssobjects::SocketInstance::SocketInstance |
( |
const SocketInstance & |
s |
) |
[inline] |
|
Member Function Documentation
bool SocketInstance::accept |
( |
SocketInstance * |
pConnect, |
|
|
LPSOCKADDR |
psa |
|
) |
|
|
bool SocketInstance::accept |
( |
SocketInstance & |
sConnect, |
|
|
LPSOCKADDR |
psa |
|
) |
|
|
void SocketInstance::cleanup |
( |
|
) |
|
|
void SocketInstance::close |
( |
|
) |
|
|
void SocketInstance::create |
( |
const int |
nType = SOCK_STREAM |
) |
|
|
const char * SocketInstance::getHostByAddr |
( |
LPCSOCKADDR |
psa |
) |
[static] |
|
SockAddr SocketInstance::getHostByName |
( |
const char * |
pchName, |
|
|
const USHORT |
ushPort = 0 |
|
) |
[static] |
|
void SocketInstance::getPeerAddr |
( |
LPSOCKADDR |
psa |
) |
|
|
void SocketInstance::getSockAddr |
( |
LPSOCKADDR |
psa |
) |
|
|
SOCKET ssobjects::SocketInstance::getSocket |
( |
|
) |
const [inline] |
|
void SocketInstance::listen |
( |
|
) |
|
|
ssobjects::SocketInstance::operator SOCKET |
( |
|
) |
[inline] |
|
SocketInstance & SocketInstance::operator= |
( |
const SocketInstance & |
s |
) |
|
|
int SocketInstance::receiveDatagram |
( |
char * |
pch, |
|
|
const int |
nSize, |
|
|
LPSOCKADDR |
psa, |
|
|
const int |
nSecs = 10 |
|
) |
|
|
int SocketInstance::recv |
( |
char * |
pch, |
|
|
const int |
nSize, |
|
|
const int |
nSecs = DEFAULT_SOCKET_TIMEOUT |
|
) |
|
|
|
Read in data from connected socket. If m_bThrowExceptionOnReadClose is set, and the socket is closed gracefully on the other end, an exception is throw. If not set, SocketInstance::recv will return 0 bytes. -
Exceptions:
-
|
int SocketInstance::send |
( |
const char * |
pch, |
|
|
const int |
nSize, |
|
|
const int |
nSecs = DEFAULT_SOCKET_TIMEOUT |
|
) |
|
|
int SocketInstance::sendDatagram |
( |
const char * |
pch, |
|
|
const int |
nSize, |
|
|
LPCSOCKADDR |
psa, |
|
|
const int |
nSecs = 10 |
|
) |
|
|
void SocketInstance::setExceptionOnReadClose |
( |
const bool |
bThrowException = true |
) |
|
|
|
Toggles exceptions on read closers.
When read exceptions are set, all socket errors, and closers will throw an exeption. Sometimes however it is desirable not to throw an exception when the connection was gracefully closed on the other end. In this case recv will return 0 bytes read. -
Parameters:
-
bThrowException |
true causes exceptions will be thrown. false causes recv to return 0 bytes read. |
|
int SocketInstance::write |
( |
const char * |
pch, |
|
|
const int |
nSize, |
|
|
const int |
nSecs = DEFAULT_SOCKET_TIMEOUT |
|
) |
|
|
Member Data Documentation
bool ssobjects::SocketInstance::m_bThrowExceptionOnReadClose
|
|
SOCKET ssobjects::SocketInstance::m_hSocket
|
|
The documentation for this class was generated from the following files:
Generated on Thu Nov 8 12:39:29 2001 for SimpleServerObjects by
1.2.11 written by Dimitri van Heesch,
© 1997-2001