Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



Location: es_sock.h
Link against:

Class RSocket

RSocket

Support

Supported from 5.0

Description

Provides a client endpoint to a protocol. It provides functions for socket creation, reading, writing, passive connection, active connection, setting addresses and querying addresses. Use this class as an endpoint for network type communications. It provides the following services:

Before using any of these services, a connection to a socket server session must have been made and the socket must be open.

Derivation

RSocketProvides a client endpoint to a protocol
RSubSessionBaseClient-side handle to a sub-session

Defined in RSocket:
Accept(), Bind(), CancelAccept(), CancelAll(), CancelConnect(), CancelIoctl(), CancelRead(), CancelRecv(), CancelSend(), CancelWrite(), Close(), Connect(), EImmediate, ENormal, EStopInput, EStopOutput, GetDisconnectData(), GetOpt(), Info(), Ioctl(), Listen(), LocalName(), LocalPort(), Name(), Open(), Read(), Recv(), RecvFrom(), RecvOneOrMore(), RemoteName(), Send(), SendTo(), SetLocalPort(), SetOpt(), Shutdown(), TShutdown, Transfer(), Write()

Inherited from RSubSessionBase:
CloseSubSession(), CreateSubSession(), SendReceive(), SubSessionHandle(), operator=()


Opening and closing sockets


Open()

TInt Open(RSocketServ& aServer,TUint addrFamily,TUint sockType,
        TUint protocol);
TInt Open(RSocketServ& aServer,const TDesC& aName);        
TInt Open(RSocketServ& aServer);        

Description

Opens a socket.

The first two variants are for opening channels to protocols. Protocols can either be identified by a tuple of constants or by name. If a socket is the first to be opened for a protocol it will have the additional effect of loading the protocol in the socket server.

The third variant, which has no additional arguments, is provided for opening a blank channel to the socket server which has no protocol associated. A socket opened in this manner is suitable as an argument to Accept(), which will marry the blank socket to a protocol when a connection is established to a remote endpoint.

Parameters

RSocketServ& aServer

The socket server session

TUint addrFamily

A valid address constant for a protocol family

TUint sockType

A valid socket type for the protocol

TUint protocol

A protocol constant which identifies a specific protocol

const TDesC& aName

Name of a protocol

Return value

TInt

KErrNone if successful, otherwise another of the system-wide error codes.


Close()

void Close();

Description

Closes a socket. If a socket has been opened using Open() then it should be closed using Close(). This will ensure all associated resources are released.

Closing serves two distinct purposes:

If a socket is connected, then calling close is equivalent to calling Shutdown() with an argument of RSocket::ENormal, synchronously waiting for the request to complete, and then closing the IPC channel. If asynchronous or alternative methods of disconnecting are required then Shutdown() should be called before Close().

Notes:

If the RSocketServ session on which a protocol was opened is closed, then all sockets associated with that session will be abortively closed and any further requests on the sockets will result in panics.

If a protocol has the flag KSIGracefulClose in its protocol information, when Close() is called on a connected socket, the socket will synchronously block until a response to a close request has been received or some other protocol condition causes the call to complete.

[Top]


Setting and getting the local address and port


Bind()

TInt Bind(TSockAddr& anAddr);

Description

Sets the local address of a socket. When a socket is opened it has no name associated with it, and binding is required so data can be routed to the socket. Bind() should be called before Listen() or Connect(). The address supplied should be a derived class specific to the particular protocol the socket was opened on.

Parameters

TSockAddr& anAddr

Desired local address of socket

Return value

TInt

KErrNone if successful, otherwise another of the system-wide error codes.


SetLocalPort()

TInt SetLocalPort(TInt aPort);

Description

Sets the local port of a socket. Setting the local port is equivalent to calling Bind() with only the port set in the address.

Parameters

TInt aPort

Desired local port of socket

Return value

TInt

KErrNone if successful, otherwise another of the system-wide error codes.


LocalName()

void LocalName(TSockAddr& anAddr);

Description

Gets the local address of a socket. The local address is set either by calling Bind() or it is automatically set when Connect() is called. If a socket is created through Accept() then a socket will inherit the port of its parent unless otherwise specified by a protocol’s behaviour. Depending on a protocol implementation, additional information may be gained through this call.

Parameters

TSockAddr& anAddr

Local address which is filled in on return


LocalPort()

TUint LocalPort();

Description

Gets the local port of a socket. Getting the local port is similar to getting the local name—see LocalName() for a description.

Return value

TUint

The local port of a socket

[Top]


Connecting to a remote host


Connect()

void Connect(TSockAddr& anAddr,TRequestStatus& aStatus);
void Connect(TSockAddr& anAddr,const TDesC8& aDataOut,TDes8& aDataIn,
        TRequestStatus& aStatus);

Description

Connects to a remote host. The address provided specifies the address of the remote host. Some protocols allow data to be sent in connect request packets which may be provided in the data-out descriptor. Some protocols may allow data to be sent in connect responses which may be collected in the data-in descriptor. A socket may only have one connect operation outstanding at any one time. Once the connect is completed, the socket is ready to send or receive data. If a socket is unbound — i.e. Bind() has not been called yet — then it will automatically have a local address allocated.

Notes:

Connect() is always required for protocols which do not have the KSIConnectionLess flag in their protocol information. If a protocol has the KSIConnectionLess flag, thenConnect() may be used to set the address for all data sent from the socket, in which case Send()/Write() may be used in addition to SendTo().

To use data in connection a protocol must have the flagKSIConnectData in its protocol information.

To cancel a connect use CancelConnect().

Parameters

TSockAddr& anAddr

Address of remote host.

TRequestStatus& aStatus

On completion, will contain an error code, see the system-wide error codes.

TDesC8& aDataOut

A descriptor containing data to be sent

TDes8& aDataIn

A descriptor to receive data.


CancelConnect()

void CancelConnect();

Description

Cancels an outstanding connect operation. Calling it will cause any outstanding connect operation to complete prematurely. The state of a socket after a connect is cancelled is defined by the characteristics of the protocol.

[Top]


Listening and accepting connect requests


Listen()

TInt Listen(TUint qSize);
TInt Listen(TUint qSize,const TDesC8& aDataOut);

Description

Sets up a socket to listen for incoming connections. Before calling this procedure a socket should be opened on a specific protocol usingOpen() and the socket should be bound to a local address usingBind(). Listen() creates a queue to hold incoming connections which can be married with blank sockets usingAccept(). The call also allows data to be sent back to connecting peers if a protocol allows data to be passed in connect responses. Once a listen queue has been created it will continue to allow peers to connect until it is full, at which point it will reject any incoming connections as specified by protocol behaviour. When a socket is accepted by the client a space is made available in the queue.

Notes:

To use data-in listening, a protocol must have the flagKSIConnectData in its protocol information.

Parameters

TUint qSize

Size of listen queue

TDesC8& aDataOut

A descriptor containing data to be sent in connect responses

Return value

TInt

KErrNone if successful, otherwise another of the system-wide error codes.


Accept()

void Accept(RSocket& aBlankSocket,TRequestStatus& aStatus);
void Accept(RSocket& aBlankSocket,TDes8& aConnectData,
        TRequestStatus& aStatus);

Description

Facilitates a client/server connection from a remote socket. The call extracts the first pending connection on a queue of sockets, the queue size being previously specified by Listen(). On successful completion the blank socket is given the handle of the new socket and it may then be used to transfer data. After completion the accept socket may be used to make further connections with new blank sockets (see Open() on how to open a blank socket).

The second variant provides an additional descriptor argument to receive data which may have been sent in a connect request. If there is a pending connection in the listen queue when Accept() is called, the call will complete immediately. Otherwise it will wait until a socket becomes available in the queue and complete asynchronously.

Notes:

Accept() may be used for protocols which do not have theKSIConnectionLess flag in their protocol information.

To receive data-in accepting, a protocol must have the flagKSIConnectData in its protocol information.

Parameters

RSocket& aBlankSocket

A socket opened as a blank socket

TRequestStatus& aStatus

On completion, will contain an error code: see the system-wide error codes.

TDes8& aConnectData

Data which may have been received in connection


CancelAccept()

void CancelAccept()

Description

Cancels an outstanding accept operation. Calling it will cause any outstanding accept operation to complete prematurely.

[Top]


Getting the remote name


RemoteName()

void RemoteName(TSockAddr& anAddr);

Description

Gets the remote name of a socket. The remote name of a socket is associated with the remote host a socket is connected to. The remote name is only valid for a connected socket. A socket is either connected through callingConnect() or Accept().

Parameters

TSockAddr& anAddr

Remote address which is filled in on return

[Top]


Getting socket protocol information


Info()

TInt Info(TProtocolDesc& aProtocol);

Description

Gets information in the protocol description for the protocol which a socket is opened on.

Parameters

TProtocolDesc& aProtocol

An integer constant which identifies an option

Return value

TInt

KErrNone if successful, otherwise another of the system-wide error codes.

[Top]


Setting and getting options


SetOpt()

TInt SetOpt(TUint anOptionName,TUint anOptionLevel,
        const TDesC8& anOption=TPtrC8(NULL,0));
TInt SetOpt(TUint anOptionName,TUint anOptionLevel,TInt anOption);

Description

Sets a socket option. The socket server has options which are generic to all sockets and protocols may add specific options.

Options available for all protocols can be set withanOptionLevel set to KSOLSocket. See individual protocol notes for other socket options.

Parameters

TUint anOptionName

An integer constant which identifies an option

TUint anOptionLevel

An integer constant which identifies level of an option: i.e. an option level groups related options together.

const TDesC8& anOption

Option value packaged in a descriptor

TInt anOption

Option value as an integer

Return value

TInt

KErrNone if successful, otherwise another of the system-wide error codes.

See also:


GetOpt()

TInt GetOpt(TUint anOptionName,TUint anOptionLevel,TDes8& anOption);
TInt GetOpt(TUint anOptionName,TUint anOptionLevel,TInt& anOption);

Description

Gets a socket option. The socket server has options which are generic to all sockets and protocols may add specific options.

Options available for all protocols can be got withanOptionLevel set to KSOLSocket. See individual protocol notes for other socket options.

Parameters

TUint anOptionName

An integer constant which identifies an option

TUint anOptionLevel

An integer constant which identifies level of an option

TDes8& anOption

On return, option value packaged in a descriptor

TInt& anOption

On return, option value as an integer

Return value

TInt

KErrNone if successful, otherwise another of the system-wide error codes.

See also:

[Top]


Sending data


Write()

void Write(const TDesC8& aDesc,TRequestStatus& aStatus);

Description

Sends data to a remote host.

Write() should only be used with connected sockets.

Parameters

const TDesC8& aDesc

A constant descriptor

TRequestStatus& aStatus

On completion, will contain an error code: see the system-wide error codes. Note that KErrEof indicates that the socket has been shutdown with option EStopOutput.


Send()

void Send(const TDesC8& aDesc,TUint someFlags,TRequestStatus& aStatus);
void Send(const TDesC8& aDesc,TUint someFlags,TRequestStatus& aStatus,
        TSockXfrLength& aLen);

Description

Sends data to a remote host with options set by protocol specific flags.

The length of the descriptor indicates the amount of data to be sent. You can use a TSockXfrLength argument to find the amount of data actually sent.

A socket may only have one send operation in progress at any one time.

Send() should only be used with connected sockets.

Note:

If a protocol’s information flag is marked withKSIUrgentData, then KSockWriteUrgent may be provided as a flag to Send(). All other flags are protocol specific.

Parameters

const TDesC8& aDesc

A constant descriptor

TUint someFlags

Flags which are passed through to protocol

TRequestStatus& aStatus

On completion, will contain an error code: see the system-wide error codes. Note that KErrEof indicates that the socket has been shutdown with option EStopOutput.

TSockXfrLength& aLen

Filled in with amount of data sent before completion


SendTo()

void SendTo(const TDesC8& aDesc,TSockAddr& anAddr,TUint flags,
        TRequestStatus& aStatus);
void SendTo(const TDesC8& aDesc,TSockAddr& anAddr,TUint flags,
        TRequestStatus& aStatus,TSockXfrLength& aLen);

Description

Sends data to a remote host through an unconnected socket to a specified destination address.

Flags are provided to add protocol specific information.

The length of the descriptor indicates the amount of data to be sent.

A socket may only have one send operation in progress at any one time.

An additional TSockXfrLength argument can be provided to get the amount of data sent.

Parameters

const TDesC8& aDesc

A constant descriptor

TSockAddr& anAddr

A remote destination address for unconnected sends

TUint flags

Flags which are passed through to protocol

TRequestStatus& aStatus

On completion, will contain an error code: see the system-wide error codes. Note that KErrEof indicates that the socket has been shutdown with option EStopOutput.

TSockXfrLength& aLen

Filled in with amount of data sent before completion


CancelWrite()

void CancelWrite();

Description

Cancels an outstanding Write() operation.

Calling the function will cause any outstanding Write() operation to complete prematurely. The state of a socket after a send is cancelled is defined by the characteristics of the protocol.


CancelSend()

void CancelSend();

Description

Cancels an outstanding send operation.

Calling the function will cause any outstanding send operation to complete prematurely. The state of a socket after a send is cancelled is defined by the characteristics of the protocol.

[Top]


Receiving data


Read()

void Read(TDes8& aDesc,TRequestStatus& aStatus);

Description

Receives data from a remote host and completes when a passed buffer is full.

The function only complete when the full amount of requested data has been received (or the connection breaks). This means when the descriptor has been filled to its maximum length (not its current length).

Read() should only be used with connected sockets.

A socket may only have one receive operation outstanding at any one time.

Parameters

TDesC8& aDesc

A descriptor where data read will be placed.

TRequestStatus& aStatus

On completion, will contain an error code: see the system-wide error codes. Note that KErrEof indicates either that a remote connection is closed, and that no more data is available for reading, or the socket has been shutdown with option EStopInput.


Recv()

void Recv(TDes8& aDesc,TUint flags,TRequestStatus& aStatus);
void Recv(TDes8& aDesc,TUint flags,TRequestStatus& aStatus,TSockXfrLength& aLen);

Description

Receives data from a remote host and completes when a passed buffer is full, allowing flags for protocol specific information.

The function only completes when the full amount of requested data has been received (or the connection breaks). This means when the descriptor has been filled to its maximum length.

Recv() should only be used with connected sockets.

A socket may only have one receive operation outstanding at any one time.

Note:

If a protocol’s information flag is marked withKSIPeekData, then KSockReadPeek may be provided as a flag to Recv(). All other flags are protocol specific.

Parameters

TDesC8& aDesc

A descriptor where data received will be placed.

TUint flags

Flags for protocol specific information.

TRequestStatus& aStatus

Reference to the request status object. On completion, will contain an error code: see the system-wide error codes. Note that KErrEof indicates either that a remote connection is closed, and that no more data is available for reading, or the socket has been shutdown with option EStopInput.

TSockXfrLength& aLen

On return, a length which indicates how much data was read. This is the same as length of the returned aDesc.


RecvOneOrMore()

void RecvOneOrMore(TDes8& aDesc,TUint flags,TRequestStatus& aStatus,
        TSockXfrLength& aLen);

Description

Receives data from a remote host and completes when data is available.

The function reads at least one byte of data, but will complete as soon as any data is available. The amount of data received is returned via the TSockXfrLength argument.

RecvOneOrMore() should only be used with connected sockets.

A socket may only have one receive operation outstanding at any one time.

Note:

Parameters

TDesC8& aDesc

A descriptor where data read will be placed.

TUint flags

Flags which are passed through to protocol.

TRequestStatus& aStatus

On completion, will contain an error code: see the system-wide error codes. Note that KErrEof indicates either that a remote connection is closed, and that no more data is available for reading, or the socket has been shutdown with option EStopInput.

TSockXfrLength& aLen

On return, a length which indicates how much data was read. This is the same as length of the returned aDesc.


RecvFrom()

void RecvFrom(TDes8& aDesc,TSockAddr& anAddr,TUint flags,
        TRequestStatus& aStatus);
void RecvFrom(TDes8& aDesc,TSockAddr& anAddr,TUint flags,
        TRequestStatus& aStatus,TSockXfrLength& aLen);

Description

Receives data from a remote host with unconnected sockets where a source address is returned.

Flags are provided to add protocol specific information.

A socket may only have one receive operation outstanding at any one time.

Parameters

TDesC8& aDesc

A descriptor where data read will be placed

TSockAddr& anAddr

A remote source address for unconnected receives

TUint flags

Flags which are passed through to protocol

TRequestStatus& aStatus

On completion, will contain an error code: see the system-wide error codes. Note that KErrEof indicates either that a remote connection is closed, and that no more data is available for reading, or the socket has been shutdown with option EStopInput.

TSockXfrLength& aLen

On return, a length which indicates how much data was read. This is the same as length of the returned aDesc.


CancelRead()

void CancelRead();

Description

Cancels an outstanding Read() operation.

Calling this function will cause any outstanding Read() operation to complete prematurely. The state of a socket after a receive is cancelled is defined by the characteristics of the protocol.


CancelRecv()

void CancelRecv();

Description

Cancels an outstanding receive operation.

Calling this function will cause any outstanding receive operation to complete prematurely. The state of a socket after a receive is cancelled is defined by the characteristics of the protocol.

[Top]


Ioctls


Ioctl()

void Ioctl(TUint aCommand,TRequestStatus& aStatus,TDes8* aDesc=NULL,
        TUint aLevel=KLevelUnspecified);

Description

Applies an asynchronous control operation on a socket. Data may be passed and received if a descriptor address is provided as an argument. Only oneIoctl() operation may be outstanding for each socket.

Commands available for all protocols can be set with aLevelset to KSOLSocket. See individual protocol notes for other commands.

Parameters

TUint aCommand

Ioctl command.

TRequestStatus& aStatus

On completion, will contain an error code: see the system-wide error codes.

TDes8* aDesc

Pointer to a descriptor in which data may be sent and received on completion

TUint aLevel

Control operation level

See also:


CancelIoctl()

void CancelConnect()

Description

Cancels an outstanding Ioctl operation. Calling it will cause any outstanding Ioctl operation to complete prematurely. The state of a socket after a connect is cancelled is defined by the characteristics of the protocol.

[Top]


Socket transfer


Name()

TInt Name(TName& aName);

Description

Gets a unique system name for the socket. The purpose of this is to identify the socket in a call to Transfer().

Parameters

TName& aName

System name for the socket

Return value

TInt

KErrNone if successful, otherwise another of the system-wide error codes.


Transfer()

TInt Transfer(RSocketServ& aServer, const TDesC& aName);

Description

Transfers a socket from one socket server session to another. It creates the socket in the target session, and removes the socket from the source session. The call is made on an uninitialised RSocket object. The socket system name is used to identify the socket to transfer.

If the call fails, the socket that is being transferred remains with the original session. Success or failure can be checked on the originating socket by calling RSocket::Info(), which returns KErrNone if the transfer failed, and KErrBadHandle if it succeeded.

Parameters

RSocketServ& aServer

The session into which to transfer the socket.

const TDesC& aName

The system name, as obtained from a call to Name(), of the socket that you want to transfer.

Return value

TInt

KErrNone if successful, otherwise another of the system-wide error codes.

[Top]


Shutting down a connection


Shutdown()

void Shutdown(TShutdown aHow,TRequestStatus& aStatus);
void Shutdown(TShutdown aHow,const TDesC8& aDataOut,TDes8& aDataIn,TRequestStatus& aStatus);

Description

Shuts down a connected socket — asynchronous. The shutdown method allows input and output to be individually stopped for a protocol endpoint. For protocols which support data-in disconnect message, additional arguments are provided.

Notes:

Shutdown() can be used for protocols which do not have theKSIConnectionLess flag in their protocol information.

To use data in disconnection a protocol must have the flagKSIConnectData in its protocol information.

There is no way to cancel a socket shutdown once it has started.

Parameters

TShutdown aHow

Shutdown option. All variants complete when a socket is disconnected.

TRequestStatus& aStatus

On completion, will contain an error code: see the system-wide error codes.

const TDesC8& aDataOut

A descriptor containing data to be sent

TDes8& aDataIn

A descriptor to receive data.


GetDisconnectData()

TInt GetDisconnectData(TDes8& aDesc);

Description

Collects pending data which has been received in a protocol disconnect message.

Notes:

To use data in disconnection a protocol must have the flagKSIConnectData in its protocol information.

Parameters

TDesC8& aDesc

A descriptor to receive data.

Return value

TInt

KErrNone if successful, otherwise another of the system-wide error codes.

[Top]


Cancel all outstanding operations


CancelAll()

void CancelAll()

Description

Cancels all outstanding operations. Calling it will cause all outstanding operations to complete prematurely. Outstanding operations for a socket include: read, write, Ioctl, connect, accept and shutdown. All of these operations will be completed by this call except shutdown which may not be cancelled.

[Top]


Enumerations


Enum TShutdown

TShutdown

Description

Used in structure TProtocolDesc to describes the endianness of a protocol.

ENormal

Complete when socket output/input stopped.

EStopInput

Stop socket input and complete when output is stopped.

EStopOutput

Stop socket output and complete when input is stopped.

EImmediate

Stop socket input/output and complete (abortive close).