Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Networking Module Reference Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

RWSocket

Module:  Essential Networking Module   Package:  Networking


Does not inherit

Local Index

Members

Non-Members

Header File

#include <rw/network/RWSocket.h>

Description

RWSocket is a wrapper for the C concept of a socket. Its member functions correspond exactly to the C functions in the Berkeley sockets API. Typically, RWSocket member functions have the same names as the corresponding C API functions, but the arguments and return values may be different to reflect the C++ environment. For example, many member functions have default arguments to handle the most common cases. Some member functions take alternate parameter types that simplify the interface (for example, using the RWSockAddrBase class instead of the sockaddr structure). Sometimes, multiple overloadings of a member function exist to provide alternate APIs for different occasions.

Almost all of the Berkeley sockets calls that use a single socket are encapsulated in this class. Omitted calls include:

In addition to the functions that match the sockets API, some convenience functions are included. They are id(), isValid(), getSocket(), recvAtLeast(), and sendAtLeast().

The socket is not shut down by a destructor. It must be explicitly shut down by calling close(), closesocket(), or shutdown(). Use the RWPortal layer for objects that close the portal automatically using a destructor.

RWSocket has no state. All state (such as whether the socket blocks) is kept in the communications channel.

The Networking package provides a C++ wrapper for the native socket API. As a result, return values from socket API calls are returned to the caller unaltered.


NOTE -- This can result in different return values and, in some cases, different behavior between calls to the same member functions on different platforms (BSD or Winsock, for instance).

For example, a connect call on a non-blocking socket returns with EINPROGRESS from BSD, but returns with WSAVEWOULDBLOCK from Winsock. You should consult your implementation-specific user's guide to identify expected behavior.

Typedefs

SOCKET

Public Constructors

RWSocket();
RWSocket(const RWSocket&);
RWSocket(const RWSockType& socketType);
RWSocket(SOCKET existingSocket);

Public Member Operators

bool 
operator==(const RWSocket& s) const;

Public Member Functions

RWSocket 
accept(RWSockAddr *addr =0) const;
void 
bind(const RWSockAddrBase& address);

NOTE -- When the bind() member function is invoked on an uninitialized socket, the function initializes the socket and sets the SO_REUSEADDR option on the socket. If you do not want this option to be set, call the socket() member function to initialize the socket before you invoke bind().
void 
close();
void 
closesocket();
void 
connect(const RWSockAddrBase& address);
RWSockAddr 
getpeername() const;
SOCKET 
getSocket() const;
RWSockAddr 
getsockname() const;
void 
getsockopt(int level, int option, void *optval, 
RWSockLenType *optlen) const; int getsockopt(int option) const;
RWSockType
getsocktype() const;
RWCString
id(unsigned level=0) const;
void 
ioctl(long cmd, void *arg) const;
void 
ioctl(long cmd, int arg) const;
int  
ioctl(long cmd) const;
void 
ioctlsocket(long cmd, void *arg) const;
void 
ioctlsocket(long cmd, int arg) const;
int  
ioctlsocket(long cmd) const;
bool 
isValid() const;
void 
listen(int backlog=5) const;
void
listen(const RWSockAddrBase& addr, int backlog=5);
RWNetBuf 
recv(int flags=0) const;
int
recv(char *buf, int len, int flags=0,RWNetBuf::State* s=0)
     const;
RWNetBuf 
recvAtLeast(int n) const;
int
recvAtLeast(char *buf, int len, int n, RWNetBuf::State* s=0)
const;
RWNetBuf 
recvfrom(RWSockAddr *addr=0, int flags=0) const;
int
recvfrom(char *buf, int len, RWSockAddr *addr=0, 
         int flags=0, RWNetBuf::State* state=0) const;
int
recvmsg(msghdr *msg, int flags=0, RWNetBuf::State* s=0) const;
int 
send(const RWCString& buf, int flags=0) const;
int 
send(const char *buf, int len, int flags=0) const;

NOTE -- When calling send(), the application must check the number of bytes sent, and resend if necessary. The sendAtLeast() method guarantees the transmission of the number of bytes specified (or the entire buffer if no size is specified).

NOTE -- Sending data on a closed socket may result in the generation of a SIGPIPE signal from the underlying socket library. Avoid this by checking that the socket's sock_attr_canwrite attribute is true before each call to send(). Refer to RWSocketAttribute for details on checking socket attributes. Alternatively, you can use a signal handler to explicitly handle or ignore SIGPIPE.
void 
sendAtLeast(const RWCString& buf) const;
int 
sendAtLeast(const RWCString& buf, int n) const;
void 
sendAtLeast(const char* buf, int len) const;
int 
sendAtLeast(const char* buf, int len, int n) const;
int
sendto(const RWCString& buf, const RWSockAddrBase& to, 
       int flags=0) const;
int 
sendto(const char *buf, int len, const RWSockAddrBase& to, 
       int flags=0) const;
int 
sendmsg(msghdr *msg, int flags=0) const;
void 
setsockopt(int level, int option, void *optval, int optlen) 
           const;
void 
setsockopt(int option, int optval) const;
void 
shutdown(int how=2) const;
void 
shutdownread() const;
void
shutdownwrite() const;
void 
socket(const RWSockType& type);

Protected Data Member

SOCKET 
socket_;

Protected Member Functions

void 
clearError() const;
static void 
doRaise(int err, const char *funcName);
int 
lastError() const;
void 
raise(const char *funcName) const;
void 
raiseUnlessWouldBlock(const char *funcName) const;
void 
raise(const char *funcName, int err) const;
void 
raiseUnlessWouldBlock(const char *funcName, int err) const;

Global Operator

ostream&
operator<<(ostream& strm, const RWSocket& x);

NOTE -- This class does not have an extraction (>>) operator.

Associated Macros

SET_BLOCKING(RWSocket)
SET_NON_BLOCKING(RWSocket)


Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.