PrevNextUpHome SophiaFramework UNIVERSE 5.3
SFXTCPSocket
Class for the TCP socket communication.
#include <SFXTCPSocket.h.hpp>
class SFXTCPSocket : public SFXStorage;
SFMTYPEDEFCLASS(SFXTCPSocket)

Inheritance diagram

 Inheritance diagram of SFXTCPSocketClass

Collaboration diagram

 Collaboration diagram of SFXTCPSocketClass

Description

The SFXTCPSocket class provides functions to implement both of the TCP client and the TCP server.

In this class, it is possible to send and receive data using the stream.

In case the stream is not used, data will be sent / received using the SFXTCPSocket::Write / SFXTCPSocket::Read function.

[Note] Note

The SFXTCPSocket class encapsulates BREW API ISocket interface to provide high level functions for the TCP socket communication.

How to implement the TCP client

The TCP client can be implemented by taking the following procedures with the SFXTCPSocket class.

  1. Create the SFXTCPSocket instance, that is, the TCP socket.
  2. Open the TCP socket using the SFXTCPSocket::Open funtion.
  3. Bind the local IP address and port number to the TCP socket with the SFXTCPSocket::Bind function, if necessary. If binding is omitted, default port number will be bound to the TCP socket.
  4. Connect(or send connection request) to the TCP server using the SFXTCPSocket::Connect function.
  5. The connection result will be notified to the callback function registered with the SFXTCPSocket::Connect function.
  6. To receive data, get the input stream. When the stream is not used, data can be received with the SFXTCPSocket::Read function.
  7. To send data, get the output stream. When the stream is not used, data can be sent with the SFXTCPSocket::Write function.
  8. Step 6. and 7. are repeated until no other data needs to be sent or received.
  9. C Close the TCP socket using the SFXTCPSocket::Close function.

Example 845. Implementation of the TCP Client

// get the time from the NTP server
// The _socket variable is defined as class member variable since used in the callback function
class NetworkTime {
private:
    SFXTCPSocket _socket;
    SFXBinaryStreamReader _reader;
    SFXTimer _timer;

public:
    Void Start(Void); // start to connect to the NTP server
    Void Stop(Void);  // stop connecting to the NTP server
    
    // declare callback function
    XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)
    XALLBACK_DECLARE_SFXBINARYSTREAMREADER(OnFetch)
    XALLBACK_DECLARE_SFXTIMER(OnTimer)
};


#define     NTP_SERVER              ("www.example.com:37")
#define     TIMER_INTERVAL          (5000)


// start to connect to the NTP server
Void NetworkTime::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);
    // set the NTP server address
    SFXSocketAddress address(NTP_SERVER);

    // set the timer callback function
    _timer.Set(XALLBACK_INTERNAL(OnTimer));

    // open socket
    if ((error = _socket.Open()) == SFERR_NO_ERROR) {

        // connect to the NTP server
        // * the connection result will be notified to the OnConnect function
        if ((error = _socket.Connect(address, XALLBACK_INTERNAL(OnConnect))) == SFERR_NO_ERROR) {
            
            TRACE("connecting...");

        }
        else {

            // if an error occurs, close TCP socket
            _socket.Close();
        }
    }
	
    if (error != SFERR_NO_ERROR) { 

        // if an error occurs
        // error handling
        ...

        // set the timer. after the TIMER_INTERVAL milliseconds,  the OnTimer function will be called
        _timer.Schedule(TIMER_INTERVAL);
    }
    return;
}

// stop connecting to the NTP server
Void NetworkTime::Stop(Void)
{
    // finalization
    _reader.Release();
    _socket.Close();
    return;
}

// callback function notified of the connection result
XALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get input stream for receiving data from TCP socket
        if ((error = _socket.GetStreamReader(64, &_reader)) == SFERR_NO_ERROR) {

            // perform fetch: receive 4 bytes of data from TCP socket into to input stream buffer actually
            // * the fetch result will be notified to the OnFetch function
            if ((error = _reader.Fetch(4, XALLBACK_INTERNAL(OnFetch))) == SFERR_NO_ERROR) { 

                TRACE("fetching...");
            }
            if (error != SFERR_NO_ERROR) { 

                // if an error occurs
                // release resource
                _reader.Release();
            }
        }
    }
	
    if (error != SFERR_NO_ERROR) {  

        // if an error occurs
        _socket.Close();
    }
    return;
}

// callback function notified of the fetch result
XALLBACK_IMPLEMENT_SFXBINARYSTREAMREADER(NetworkTime, OnFetch, error)
{
    SFXDate date; // date class
    UInt32 time;

    if (error == SFERR_NO_ERROR) {

        // read data as big endian
        _reader.SetEndian(SFXBinaryStreamReader::ENDIAN_BIG);
        
        // read data as the UInt32 type from input stream buffer into the time variable
        if ((error = _reader.ReadUInt32(&time)) == SFERR_NO_ERROR) {
            
            // set the SFXDate instance
            date.Set(time);
            
            // adjust time from January 1, 1900
            date -= SFXDateDuration::Offset19000101();
            
            // convert time into local time
            date += SFXDateDuration::LocalTimeOffset();

            // output time in specified format
            TRACE("%s", date.Format("YYYY/MM/DD hh:mm:ss").GetCString());
        }
    }
	
    if (error != SFERR_NO_ERROR) {

        // if an error occurs
        // error handling
        ...
    }

    // finalization
    _reader.Release();
    _socket.Close();
    return;
}

// timer callback function
XALLBACK_IMPLEMENT_SFXTIMER(NetworkTime, OnTimer)
{
    Start();
    return;
}
[Note] Note

For more details on the above code, see the networktime applet in the Example/networktime folder of the SophiaFramework UNIVERSE package, which supports suspend and resume functions.

How to implement the TCP server

The TCP server can be implemented by taking the following procedures with the SFXTCPSocket class.

  1. Create the SFXTCPSocket instance.
  2. Open the TCP Socket with the SFXTCPSocket::Open funtion.
  3. Bind the local IP address and port number to the TCP socket with the SFXTCPSocket::Bind function.
  4. Start to wait for the connection request from the TCP client with the SFXTCPSocket::Listen function.
  5. Get one from the connection request queue of this TCP socket and connect to the TCP client with the SFXTCPSocket::Accept function. At this time, the TCP socket for sending and receiving data after this step will be returned to the argument of this function.
  6. To receive data, get the input stream. When the stream is not used, data can be received with the SFXTCPSocket::Read function.
  7. To send data, get the output stream. When the stream is not used, data can be sent with the SFXTCPSocket::Write function.
  8. Step 6. and 7. are repeated until no other data needs to be sent or received.
  9. C Close the TCP socket using the SFXTCPSocket::Close function.

Reference

SFXUDPSocket | SFXSSLSocket | SFXSocketAddress | TCP Socket Communication | BREW API ISocket

Member

Constructor/Destructor
SFXTCPSocket( Void )
Constructor of the SFXTCPSocket class.
~SFXTCPSocket( Void )
Destructor of the SFXTCPSocket class.
Public Functions
SFCError Accept( SFXTCPSocketPtr result )
Accept(Or get one from the connection request queue of this TCP socket in order to connect to the TCP client).
SFCError AsSFBAStream( SFBAStreamSmpPtr result )
Convert the SFBSocket instance internally managed by this SFXTCPSocket storage into the SFBAStream instance.
SFCError AsSFBSource( SFBSourceSmpPtr result )
Convert the SFBSocket instance internally managed by this SFXTCPSocket storage into the SFBSource instance.
SFCError Bind( SFXSocketAddressConstRef address )
Bind(Or bind the specified local IP address and port number to this socket.
Void Cancel( Void )
Cancel this TCP socket communication.
Void Close( Void )
Close this TCP socket.
SFCError Connect( SFXSocketAddressConstRef address , CallbackSPP spp , VoidPtr reference )
Send the connection request to the specified server in order to establish the connection.
SFCError GetLocalAddress( SFXSocketAddressPtr result )
Get the local IP address and port number of this TCP socket.
SFCError GetRemoteAddress( SFXSocketAddressPtr result )
Get the remote IP address and port number of the peer of this TCP socket.
SFBNetMgrSmpConstRef GetSFBNetMgr( Void )
Get the SFBNetMgr instance managed internally by this socket.
SFBSocketSmpConstRef GetSFBSocket( Void )
Get the SFBSocket instance managed internally by this socket.
SFCError GetStreamReader( UInt32 size , SFXStreamReaderPtr result )
Get the input stream for reading data from this TCP socket.
SFCError GetStreamReader( SFXStreamReaderPtr result )
Get the input stream for reading data from this TCP socket.
SFCError GetStreamWriter( UInt32 size , SFXStreamWriterPtr result )
Get the output stream for writing data onto this TCP socket.
SFCError GetStreamWriter( SFXStreamWriterPtr result )
Get the output stream for writing data onto this TCP socket.
SFCError Listen( SInt16 backlog = 1 )
Listen(Or start to wait for the connection request from the TCP client).
SFCError Open( SInt32 linger = -1 )
Open this TCP socket.
SFCError Read( VoidPtr buffer , UInt32Ptr size )
Read data from this TCP socket without input stream.
SFCError ScheduleAccept( CallbackSPP spp , VoidPtr reference )
Schedule to accept(or get one from the connection request queue of this TCP socket in order to connect to the TCP client).
SFCError ScheduleBind( CallbackSPP spp , VoidPtr reference )
Schedule to bind the local IP address and port number to this TCP socket.
SFCError ScheduleListen( CallbackSPP spp , VoidPtr reference )
Schedule to listen.
SFCError ScheduleRead( CallbackSPP spp , VoidPtr reference )
Schedule to read data from this TCP socket without input stream.
SFCError ScheduleWrite( CallbackSPP spp , VoidPtr reference )
Schedule to write data into this TCP socket without output stream.
SFCError Write( VoidConstPtr buffer , UInt32Ptr size )
Write data into this TCP socket without output stream.
Types
CallbackSPP (inherits from SFXStorage)
Type of the callback function for the Storage class.

SFXTCPSocket::SFXTCPSocket
Constructor of the SFXTCPSocket class.
[ public, explicit ]
SFXTCPSocket(Void);

Description

This constructor does nothing.


SFXTCPSocket::~SFXTCPSocket
Destructor of the SFXTCPSocket class.
[ public, virtual ]
virtual ~SFXTCPSocket(Void);

Description

This destructor calls the SFXTCPSocket::Close function.

[Note] Note

Registered callback functions will be canceled.

Reference

SFXTCPSocket::Close


SFXTCPSocket::Accept
Accept(Or get one from the connection request queue of this TCP socket in order to connect to the TCP client).
[ public, const ]
SFCError Accept(
    SFXTCPSocketPtr result   // pointer to the new socket for communicating with the client
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this socket is not "under listening": SFERR_INVALID_STATE
  • If the result argument is null: SFERR_INVALID_PARAM
  • If accepting is blocked: AEE_NET_WOULDBLOCK
  • If other network error occurs: error value obtained by calling the BREW API ISOCKET_GetLastError function or AEE_NET_ERROR

Description

This function accepts(or gets one from the connection request queue of this TCP socket in order to connect to the TCP client).

This function gets one from the connection request queue of this TCP socket in order to connect to the TCP client, and creates the new connected TCP socket into the result argument. Using this socket, the communication with the TCP client will be performed.

* If this function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with the SFXTCPSocket::ScheduleAccept function where accepting will be performed with this function.

[Note] Note

This function internally calls the SFBSocket::Accept function.

[Note] Prerequisite

This socket needs to be under listening with the SFXTCPSocket::Listen function before this function is called.

Reference

SFXTCPSocket::ScheduleAccept | SFXTCPSocket::Listen | SFXTCPSocket::Bind | SFBSocket::Accept | BREW API ISOCKET_Accept | BREW API ISOCKET_GetLastError


SFXTCPSocket::AsSFBAStream
Convert the SFBSocket instance internally managed by this SFXTCPSocket storage into the SFBAStream instance.
[ public, virtual, const ]
SFCError AsSFBAStream(
    SFBAStreamSmpPtr result   // pointer to the SFBAStream instance
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this SFXTCPSocket storage has been closed: SFERR_INVALID_STATE
  • If the result argument is null: SFERR_INVALID_PARAM

Description

This function converts the SFBSocket instance internally managed by this SFXTCPSocket storage into the SFBAStream instance.

As a result, a pointer to the SFBAStream instance will be returned into the result argument.

[Note] Note
The contents of the SFXTCPSocket storage can be handled through the SFBAStream instance.

Reference

SFBSocket | SFBAStream


SFXTCPSocket::AsSFBSource
Convert the SFBSocket instance internally managed by this SFXTCPSocket storage into the SFBSource instance.
[ public, virtual, const ]
SFCError AsSFBSource(
    SFBSourceSmpPtr result   // pointer to the SFBSource instance
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this SFXTCPSocket storage has been closed: SFERR_INVALID_STATE
  • If the result argument is null: SFERR_INVALID_PARAM

Description

This function converts the SFBSocket instance internally managed by this SFXTCPSocket storage into the SFBSource instance.

As a result, a pointer to the SFBSource instance will be returned into the result argument.

[Note] Note

This function internally calls the BREW API ISOURCEUTIL_SourceFromSocket function.

[Note] Note
The contents of the SFXTCPSocket storage can be handled as the SFBSource instance.

Reference

SFBSocket | SFBSource | BREW API ISOURCEUTIL_SourceFromSocket


SFXTCPSocket::Bind
Bind(Or bind the specified local IP address and port number to this socket.
[ public ]
SFCError Bind(
    SFXSocketAddressConstRef address   // local IP address and port number: Either SFXInetAddress::LoopbackInetAddress() or SFXInetAddress.AnyInetAddress() can be specified as local IP address
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this socket has not been opened: SFERR_INVALID_STATE
  • If binding is blocked: AEE_NET_WOULDBLOCK
  • If other network error occurs: error value obtained by calling the BREW API ISOCKET_GetLastError function or AEE_NET_ERROR

Description

This function binds(or binds the specified local IP address and port number to this TCP socket).

In case of loopback communication within the device only, specify SFXInetAddress::LoopbackInetAddress() as the local IP address. Otherwise, i.e., when communicating with an external server, specify SFXInetAddress::AnyInetAddress(). For the limitation on BREW SDK, values other than these two are not supported.

* If this function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with the SFXTCPSocket::ScheduleBind function where binding will be performed with this function.

[Note] Note

In the client side, you don't necessarily have to bind. If binding is omitted, default port number will be bound to this TCP socket.

[Note] Note

This function internally calls the SFBSocket::Bind function.

[Note] Prerequisite

This socket needs to be opened with the SFXTCPSocket::Open function before this function is called.

Reference

SFXTCPSocket::Open | SFXTCPSocket::Listen | SFXTCPSocket::Accept | SFXTCPSocket::ScheduleBind | SFXInetAddress::LoopbackInetAddress | SFXInetAddress::AnyInetAddress | SFXSocketAddress | SFBSocket::Bind | BREW API ISOCKET_Bind | BREW API ISOCKET_GetLastError | BREW API AEE_BREW_LOOPBACK | BREW API AEE_INADDR_LOOPBACK | BREW API AEE_INADDR_ANY


SFXTCPSocket::Cancel
Cancel this TCP socket communication.
[ public, virtual ]
Void Cancel(Void);

Description

This function cancels various operations on this TCP socket.

Concretely, the registered callback function for each operation will be canceled and the status will be returned to before that operation is performed.

The operations to be canceled and its details are in the following table:

Table 198. Operations to be canceled and its details

Operations to be canceled Details
Connecting to the server with the SFXTCPSocket::Connect function. Cancel the callback function and restore the status to just after the SFXTCPSocket::Open function is executed.
Scheduling with the SFXTCPSocket::ScheduleBind function. Cancel the callback function and restore the status to just after the SFXTCPSocket::Open function is executed.
Scheduling with the SFXTCPSocket::ScheduleListen function. Cancel the callback function and restore the status to just after the SFXTCPSocket::Bind function is executed.
Scheduling with the SFXTCPSocket::ScheduleAccept function. Cancel the callback function and restore the status to just after the SFXTCPSocket::Listen function is executed.
Scheduling with the SFXTCPSocket::ScheduleRead function. Cancel the callback function and restore the status to just after the SFXTCPSocket::Connect function is executed.
Scheduling with the SFXTCPSocket::ScheduleWrite function. Cancel the callback function and restore the status to just after the SFXTCPSocket::Connect function is executed.
[Note] Note

This function is called in the SFXTCPSocket::Close function.

Reference

SFXTCPSocket::Open | SFXTCPSocket::Connect | SFXTCPSocket::ScheduleBind | SFXTCPSocket::ScheduleListen | SFXTCPSocket::ScheduleAccept | SFXTCPSocket::ScheduleRead | SFXTCPSocket::ScheduleWrite | SFXTCPSocket::Close


SFXTCPSocket::Close
Close this TCP socket.
[ public ]
Void Close(Void);

Description

This function closes this TCP socket.

Concretely, this function calls the SFXTCPSocket::Cancel function and releases the SFBNetMgr and SFBSocket instances that this TCP socket internally manages.

[Note] Note

The registered callback functions will be canceled.

[Note] Note

This function is called in the SFXTCPSocket::~SFXTCPSocket destructor.

[Tip] Tip

When suspending, resources should be released by calling this function.

Reference

SFXTCPSocket::Open | SFXTCPSocket::Cancel | SFXTCPSocket::~SFXTCPSocket | SFBNetMgr | SFBSocket | BREW API INetMgr | BREW API ISocket


SFXTCPSocket::Connect
Send the connection request to the specified server in order to establish the connection.
[ public ]
SFCError Connect(
    SFXSocketAddressConstRef address   // domain (or IP address) and port number of the server
    CallbackSPP spp                    // callback function that the the connection result will be notified to
    VoidPtr reference                  // data passed to callback function
);

Argument

address

Specify the domain (or IP address) and port number of the server. The domain will be automatically resolved, if necessary.

spp

Specify the callback function notified of the connection result.

reference

Specify the data passed to the callback function.

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this socket is not opened or it has already been connected (including "Under connecting"): SFERR_INVALID_STATE
  • If domain name is not found: SFERR_INVALID_STATE
  • If failed to create the SFBNetMgr instance: SFERR_FAILED
  • If insufficient memory: SFERR_NO_MEMORY
  • If other network error occurs: error value obtained by calling the BREW API ISOCKET_GetLastError function or AEE_NET_ERROR

Description

This function sends the connection request to the specified server in order to establish the connection. After the connection is established, communication with the server will be performed using this TCP socket.

This function can not gets the result (error code) of the connection establishment as the return value. The the connection result will be notified to the callback function specified in the first argument as follows:

  • AEE_NET_EADDRINUSE: Local address requested via a prior call to ISOCKET_Bind() was not available.
  • AEE_NET_EBADF: Invalid socket descriptor is specfied.
  • AEE_NET_ECONNREFUSED: Connection attempt refused.
  • AEE_NET_EIPADDRCHANGED: IP address changed due to PPP resync.
  • AEE_NET_EINVAL: Invalid IP address and/or port specified.
  • AEE_NET_EISCONN: The socket is already connected (result was already received by a previous call to Connect()).
  • AEE_NET_EMFILE: Not enough network resources to complete this operations (too many sockets in use).
  • AEE_NET_ENETDOWN: Network is not available (e.g. handset outside of coverage area).
  • AEE_NET_EOPNOTSUPP: Socket type is not STREAM.
  • AEE_NET_ETIMEDOUT: Connection attempt timed out.
  • AEE_NET_SUCCESS(SFERR_NO_ERROR): Connect completed successfully; socket is prepared for reading and/or writing.

Until the callback function is called, the state of this TCP socket is "Under connecting". When the callback function is called, it will be "Connected" if the connection is established. Otherwise, it will be restored to "Opened" just before this function is called.

And, if the SFXTCPSocket::Cancel function is called before the result of the connection establishment is notified to the callback function, the connection establishment will be canceled and the state of this TCP socket will be restored to "Opened" just before this function is called.

* If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.

[Note] Note

This function internally calls the SFBSocket::Connect function.

[Note] Prerequisite

This socket needs to be opend by calling the SFXTCPSocket::Open function.

Example

// define _socket, SFXTCPSocket instance, as member variable
class TcpSample {
private:
    SFXTCPSocket _socket;
public:
    Void Start(Void);                       
    XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) 
};

// start to connect TCP server 
Void TcpSample::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // set address of TCP server
    SFXSocketAddress address("www.example.com:37");

    // open TCP socket 
    if ((error = _socket.Open()) == SFERR_NO_ERROR) {
        
        // connect to TCP server
        // the connection result will be notified to the OnConnect function 
        if ((error = _socket.Connect(address,XALLBACK_INTERNAL(OnConnect))) == SFERR_NO_ERROR) {

            ...
        }else {

            // if an error occurs
            _socket.Close();
        }
    }
	
    if (error != SFERR_NO_ERROR) { 

        // if an error occurs
        // error handling
        ...
    }    
}

// callback function notified of the connection result
XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        ...
    }
}

Reference

SFXTCPSocket::Open | SFXTCPSocket::Cancel | SFBSocket::Connect | SFXSocketAddress | SFBNetMgr | SFXStorage::CallbackSPP | BREW API INetMgr | BREW API ISOCKET_Connect | BREW API ISOCKET_GetLastError | BREW API Network AEE error codes


SFXTCPSocket::GetLocalAddress
Get the local IP address and port number of this TCP socket.
[ public, const ]
SFCError GetLocalAddress(
    SFXSocketAddressPtr result   // pointer to the local IP address and port number of this TCP socket
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this socket is not bound nor connected: SFERR_INVALID_STATE
  • If the result argument is null: SFERR_INVALID_PARAM
  • If other network error occurs: error value obtained by calling the BREW API ISOCKET_GetLastError function or AEE_NET_ERROR

Description

This function gets the local IP address and port number of this TCP socket.

The actual physical IP address will be obtained from the socket for sending and receiving data after the connection is established. Until then, the value set with the SFXTCPSocket::Bind function will be return.

[Tip] Tip

The actual physical IP address can be also obtained by calling the SFXInetAddress::LocalInetAddress function.

[Note] Prerequisite

To get the local IP address and port number, this socket needs to be bound with the SFXTCPSocket::Bind function or connected with the SFXTCPSocket::Connect function.

Reference

SFXTCPSocket::Bind | SFXTCPSocket::Connect | SFXInetAddress::LocalInetAddress | SFXSocketAddress | BREW API ISOCKET_GetLastError


SFXTCPSocket::GetRemoteAddress
Get the remote IP address and port number of the peer of this TCP socket.
[ public, const ]
SFCError GetRemoteAddress(
    SFXSocketAddressPtr result   // pointer to the remote IP address and port number of the peer of this TCP socket
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this socket is not connected(or connection is not established): SFERR_INVALID_STATE
  • If the result argument is null: SFERR_INVALID_PARAM
  • If other network error occurs: error value obtained by calling the BREW API ISOCKET_GetLastError function or AEE_NET_ERROR

Description

This function gets the remote IP address and port number of the peer of this TCP socket.

[Note] Note

This function internally calls the SFBSocket::GetPeerName function.

[Note] Prerequisite

To get the remote IP address and port number, this socket needs to be connected one returned by the SFXTCPSocket::Accept function or connected with the SFXTCPSocket::Connect function.

Reference

SFBSocket::GetPeerName | SFXTCPSocket::Accept | SFXTCPSocket::Connect | SFXSocketAddress | BREW API ISOCKET_GetPeerName | BREW API ISOCKET_GetLastError


SFXTCPSocket::GetSFBNetMgr
Get the SFBNetMgr instance managed internally by this socket.
[ public, const ]
SFBNetMgrSmpConstRef GetSFBNetMgr(Void);

Return value

SFBNetMgr instance managed internally by this socket.

Description

This function gets the SFBNetMgr instance managed internally by this socket.

Reference

SFBNetMgr | BREW API INetMgr


SFXTCPSocket::GetSFBSocket
Get the SFBSocket instance managed internally by this socket.
[ public, const ]
SFBSocketSmpConstRef GetSFBSocket(Void);

Return value

SFBSocket instance managed internally by this socket.

Description

This function gets the SFBSocket instance managed internally by this socket.

Reference

SFBSocket | BREW API ISocket


SFXTCPSocket::GetStreamReader
Get the input stream for reading data from this TCP socket.
[ public, virtual ]
SFCError GetStreamReader(
    UInt32 size                 // buffer size
    SFXStreamReaderPtr result   // pointer to the stream
);
[ public, virtual ]
SFCError GetStreamReader(
    SFXStreamReaderPtr result   // pointer to the stream
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null: SFERR_INVALID_PARAM
  • If the connection has not been established: SFERR_INVALID_STATE
  • If insufficient memory: SFERR_NO_MEMORY

Description

This function gets the input stream for reading data from this TCP socket.

If the size argument is specified, the buffer size of the input stream will be fixed with the specified value. Otherwise, the buffer size will be variable and the SFXElasticStreamReader class will be used internally.

[Tip] Tip
You have to use the SFXBinaryStreamReader, SFXAnsiStringStreamReader, or SFXWideStringStreamReader class for the input stream properly depending on the type of data to be read.
[Note] Prerequisite

To get the input stream, this socket needs to be connected one returned by the SFXTCPSocket::Accept function or connected with the SFXTCPSocket::Connect function.

Example

The code to get the input stream for reading data from the TCP socket using the SFXBinaryStreamReader class is as follows:

// define _socket, the SFXTCPSocket instance, as the member variable
class NetworkTime {
private:
    SFXTCPSocket _socket;
    SFXBinaryStreamReader _reader;
public:
    // declare the callback function
    XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)
};

// callback function notified of the connection result
XALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get the input stream for reading data from the TCP socket
        if ((error = _socket.GetStreamReader(64, &_reader)) == SFERR_NO_ERROR) {
            ...
        }
    }
    return;
}

Reference

SFXTCPSocket::GetStreamWriter | SFXBinaryStreamReader | SFXAnsiStringStreamReader | SFXWideStringStreamReader SFXTCPSocket::Accept | SFXTCPSocket::Connect | Stream Buffer


SFXTCPSocket::GetStreamWriter
Get the output stream for writing data onto this TCP socket.
[ public, virtual ]
SFCError GetStreamWriter(
    UInt32 size                 // buffer size
    SFXStreamWriterPtr result   // pointer to the output stream
);
[ public, virtual ]
SFCError GetStreamWriter(
    SFXStreamWriterPtr result   // pointer to the output stream
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null: SFERR_INVALID_PARAM
  • If the connection has not been established: SFERR_INVALID_STATE
  • If insufficient memory: SFERR_NO_MEMORY

Description

This function gets the output stream for writing data onto this TCP socket.

If the size argument is specified, the buffer size of the output stream will be fixed with the specified value. Otherwise, the buffer size will be variable and the SFXElasticStreamWriter class will be used internally.

[Tip] Tip
You have to use the SFXBinaryStreamWriter, SFXAnsiStringStreamWriter, or SFXWideStringStreamWriter class for the output stream properly depending on the type of data to write.
[Note] Prerequisite

To get the output stream, this socket needs to be connected one returned by the SFXTCPSocket::Accept function or connected with the SFXTCPSocket::Connect function.

Example

The code to get the output stream for writing data onto the TCP socket using the SFXBinaryStreamWriter class is as follows:

// define _socket, the SFXTCPSocket instance, as the member variable
class NetworkTime {
private:
    SFXTCPSocket _socket;
    SFXBinaryStreamwriter _writer;
public:
    // declare the callback function
    XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)
};
    
// callback function notified of the connection result
XALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get the output stream for writing data onto the TCP socket
        if ((error = _socket.GetStreamWriter(64, &_writer)) == SFERR_NO_ERROR) {
            ...
        }
    }
    return;
}

Reference

SFXTCPSocket::GetStreamReader | SFXBinaryStreamWriter | SFXAnsiStringStreamWriter | SFXWideStringStreamWriter SFXTCPSocket::Accept | SFXTCPSocket::Connect | Stream Buffer


SFXTCPSocket::Listen
Listen(Or start to wait for the connection request from the TCP client).
[ public ]
SFCError Listen(
    SInt16 backlog = 1   // queue size to store the waiting connection requests
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this socket is not bound or under listening, or listening has already been scheduled: SFERR_INVALID_STATE
  • If listening is blocked : AEE_NET_WOULDBLOCK
  • If other network error occurs: error value obtained by calling the BREW API ISOCKET_GetLastError function or AEE_NET_ERROR

Description

This function listens(or starts to wait for the connection request from the TCP client).

The backlog parameter indicates the maximum length for the queue of pending connections. If backlog is less than one, it will be silently increased to one. If backlog is larger than the system maximum, it will be silently reduced to the system maximum.

After this function is executed, the connection can be established by calling the SFXTCPSocket::Accept function to one from the connection request queue of this TCP socket in order to connect to the TCP client.

* If this function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with the SFXTCPSocket::ScheduleListen function where listening will be performed with this function.

[Note] Note

This function internally calls the SFBSocket::Listen function.

[Note] Prerequisite

This socket needs to be bound with the SFXTCPSocket::Bind function before this function is called.

Reference

SFXTCPSocket::Bind | SFXTCPSocket::Accept | SFXTCPSocket::ScheduleListen | SFBSocket::Listen | BREW API ISOCKET_Listen | BREW API ISOCKET_GetLastError


SFXTCPSocket::Open
Open this TCP socket.
[ public ]
SFCError Open(
    SInt32 linger = -1   // linger time ( linger time is not set up when linger = -1  )
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this socket has already been opened: SFERR_INVALID_STATE
  • If failed to create the SFBNetMgr instance: SFERR_FAILED
  • If failed to create the SFBSocket instance: return value of INETMGR_GetLastError()
  • If a value greater than UINT16_MAXIMUM is set as the linger time: SFERR_INVALID_PARAM

Description

This function opens (or initializes) this this TCP socket.

Concretely, this function creates the SFBNetMgr instance and calls the BREW API INETMGR_OpenSocket function to create the SFBSocket instance of the AEE_SOCK_STREAM type. These instances are internally managed by this TCP socket.

The SFBNetMgr / SFBSocket instance can be obtained with the SFXTCPSocket::GetSFBNetMgr / SFXTCPSocket::GetSFBSocket function. More detailed setting can be realized by using these instances.

[Note] Linger time(waiting time for connecting a network)

The linger time(waiting time for connecting a network) set up by the BREW API INETMGR_SetLinger function, can be specified by the linger argument of this function. Default value of this argument is -1. At this time the linger time is not set up. For more details on the linger time settings, refer to BREW API INETMGR_SetLinger.

[Tip] Tip

If the SFXTCPSocket::Close function is called, this TCP socket will be closed.

Example

// define _socket, SFXTCPSocket instance, as member variable
class TcpSample {
private:
    SFXTCPSocket _socket;
public:
    Void Start(Void); 
    XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)
};

// start to connect TCP server
Void TcpSample::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // set address of TCP server 
    SFXSocketAddress address("www.example.com:37");

    // open TCP socket
    if ((error = _socket.Open()) == SFERR_NO_ERROR) { 
        
        // connect to TCP server
        // the connection result will be notified to the OnConnect function 
        if ((error = _socket.Connect(address,XALLBACK_INTERNAL(OnConnect))) == SFERR_NO_ERROR) { 
            ...
        }else {
            // if an error occurs
            _socket.Close();
        }
    }
	
    if (error != SFERR_NO_ERROR) {
        // if an error occurs
        // error handling
        ...
    }    
}

// callback function notified of the connection result
XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
        ...
    }
}

Reference

SFXTCPSocket::Close | SFXTCPSocket::GetSFBNetMgr | SFXTCPSocket::GetSFBSocket | SFBNetMgr | SFBSocket | BREW API INetMgr | BREW API ISocket | BREW API INETMGR_SetLinger | BREW API INETMGR_OpenSocket


SFXTCPSocket::Read
Read data from this TCP socket without input stream.
[ public, virtual ]
SFCError Read(
    VoidPtr buffer   // buffer to read data into
    UInt32Ptr size   
// before this function is called: size of the buffer to read data into; 
just after this function is returned: size of the data to be read actually

);

Argument

buffer

Specify the buffer to read data into.

size

Before calling this function, specify the size of the buffer to read data into. Just after this function is returned, the size of data to be read actually will be stored into this argument.

Return value

  • If succeeds: SFERR_NO_ERROR
  • If connection of this socket has not been established: SFERR_INVALID_STATE
  • If the size argument is null: SFERR_INVALID_PARAM
  • If retry is necessary: AEE_NET_WOULDBLOCK
  • If other network error occurs: error value obtained by calling the BREW API ISOCKET_GetLastError function or AEE_NET_ERROR

Description

This function reads data from this TCP socket without input stream.

When the peer closes the connection, this function will return SFERR_NO_ERROR and 0 will be returned into the area that the size argument points to.

* If this function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with the SFXTCPSocket::ScheduleRead function where data will be read with this function.

[Note] Note

This function internally calls the SFBAStream::Read function.

[Note] Prerequisite

The connection of this socket needs to be established with the SFXTCPSocket::Connect function before this function is called.

Example

// define _socket, SFXTCPSocket instance, as member variable   
class TcpSample {
private:
    SFXTCPSocket _socket;
public:
    // callback function declaration
    XALLBACK_DECLARE_SFXTCPSOCKET(OnWrite)  
    XALLBACK_DECLARE_SFXTCPSOCKET(OnRead)  
};

// callback function notified that data can be sent
XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error)
{
    UInt32 size;
    static ACharConst data[] = "GET / HTTP/1.0\n\n";
    
    // write data
    size = sizeof(data) - 1;
    switch (error = _socket.Write(data, &size)) {
        case SFERR_NO_ERROR:
             // schedule to read data
            _socket.ScheduleRead(XALLBACK_INTERNAL(OnRead)); // register callback function
            break;
        case AEE_NET_WOULDBLOCK:
            // schedule to write data 
            _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite)); // register callback function
            break;
    }
}

// callback function notified that data is ready to be read
XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnRead, error)
{
    SFXBuffer buffer;
    UInt32 size;

    // read data
    buffer.SetSize(32);
    size = buffer.GetSize();
    switch (_socket.Read(buffer.GetBuffer(), &size)) {
        case SFERR_NO_ERROR:

           // check whether or not the specified size of data is read
           if (size == buffer.GetSize()) {

                // display the read data
                buffer.SetSize(buffer.GetSize() + 1);
                buffer[buffer.GetSize() - 1] = '\0';
                TRACE("%s", SFXAnsiString(buffer).GetCString());
                
               // close the connection
                _socket.Close();
            }
            else {
                ...
            }
            break;

        case AEE_NET_WOULDBLOCK:
            // schedule to read data 
            _socket.ScheduleRead(XALLBACK_INTERNAL(OnRead)); // register callback function
            break;
    }
    return;
}

Reference

SFXTCPSocket::ScheduleRead | SFXTCPSocket::Accept | SFBAStream::Read | BREW API ISOCKET_Read


SFXTCPSocket::ScheduleAccept
Schedule to accept(or get one from the connection request queue of this TCP socket in order to connect to the TCP client).
[ public ]
SFCError ScheduleAccept(
    CallbackSPP spp     // callback function
    VoidPtr reference   // data passed to callback function
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this socket is not under listening, or accepting has already been scheduled: SFERR_INVALID_STATE

Description

This function schedules to accept(or get one from the connection request queue of this TCP socket in order to connect to the TCP client).

Concretely, this function registers the callback function that will accept(or get one from the connection request queue of this TCP socket in order to connect to the TCP client) with the SFXTCPSocket::Accept function. The registered callback function will be called by BREW AEE when it is possible to accept.

The status of this socket is "under scheduling to accept" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "this socket is under listening".

*1. If the SFXTCPSocket::Accept function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with this function where accepting will be performed with the SFXTCPSocket::Accept function.

*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.

[Note] Note

This function internally calls the SFBSocket::Writeable function.

[Note] Prerequisite

Before this function is called, this socket needs to be under listening with the SFXTCPSocket::Listen function.

If accepting has already been scheduled, the SFERR_INVALID_STATE error will be returned too.

Reference

SFXTCPSocket::Accept | SFXTCPSocket::Listen | SFBAStream::Readable | SFXStorage::CallbackSPP | BREW API ISOCKET_Readable


SFXTCPSocket::ScheduleBind
Schedule to bind the local IP address and port number to this TCP socket.
[ public ]
SFCError ScheduleBind(
    CallbackSPP spp     // callback function
    VoidPtr reference   // data passed to callback function
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this socket has been closed, connected or under being connected, or binding has already been scheduled: SFERR_INVALID_STATE

Description

This function schedules to bind the local IP address and port number to this TCP socket.

Concretely, this function registers the callback function that will bind the local IP address and port number to this TCP socket with the SFXTCPSocket::Bind function. The registered callback function will be called by BREW AEE when it is possible to bind.

The status of this socket is "under scheduling to bind" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "this socket is opened".

If you call the SFXTCPSocket::Cancel function before the callback function is called, binding will be canceled and the status will be returned to the status before calling this function, i.e., "this socket is opened".

*1. If the SFXTCPSocket::Bind function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with this function where binding will be performed with the SFXTCPSocket::Bind function.

*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.

[Note] Note

This function internally calls the SFBSocket::Writeable function.

[Note] Prerequisite

Before this function is called, this socket needs to be opened with the SFXTCPSocket::Open function. However, if it is connected or under being connected, the SFERR_INVALID_STATE error will be returned.

In addition, if binding has already been scheduled, the SFERR_INVALID_STATE error will be returned too.

Reference

SFXTCPSocket::Bind | SFXTCPSocket::Cancel | SFXTCPSocket::Open | SFBSocket::Writeable | SFXStorage::CallbackSPP | BREW API ISOCKET_Writeable


SFXTCPSocket::ScheduleListen
Schedule to listen.
[ public ]
SFCError ScheduleListen(
    CallbackSPP spp     // callback function
    VoidPtr reference   // data passed to callback function
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this socket is not bound, or listening has already been scheduled: SFERR_INVALID_STATE

Description

This function schedules to listen(or start to wait for the connection request from the TCP client).

Concretely, this function registers the callback function that will listen(or start to wait for the connection request from the TCP client) with the SFXTCPSocket::Listen function. The registered callback function will be called by BREW AEE when it is possible to listen.

The status of this socket is "under scheduling to listen" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "this socket is bound".

*1. If the SFXTCPSocket::Listen function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with this function where listening will be performed with the SFXTCPSocket::Listen function.

*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.

[Note] Note

This function internally calls the SFBSocket::Writeable function.

[Note] Prerequisite

Before this function is called, this socket needs to be bound with the SFXTCPSocket::Bind function.

If listening has already been scheduled, the SFERR_INVALID_STATE error will be returned too.

Reference

SFXTCPSocket::Listen | SFBSocket::Writeable | SFXStorage::CallbackSPP | BREW API ISOCKET_Writeable


SFXTCPSocket::ScheduleRead
Schedule to read data from this TCP socket without input stream.
[ public, virtual ]
SFCError ScheduleRead(
    CallbackSPP spp     // callback function
    VoidPtr reference   // data passed to callback function
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the connection of this socket has not been established, or reading data has already been scheduled: SFERR_INVALID_STATE

Description

This function schedules to read data from this TCP socket without input stream.

Concretely, this function registers the callback function that will read data with the SFXTCPSocket::Read function. The registered callback function will be called by BREW AEE when it is possible to read data.

The status of this socket is "under scheduling to read data" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "the connection of this socket is established".

If you call the SFXTCPSocket::Cancel function before the callback function is called, reading data will be canceled and the status will be returned to the status before calling this function, i.e., "the connection of this socket is established".

*1. If the SFXTCPSocket::Read function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with this function where data will be read with the SFXTCPSocket::Read function.

*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.

[Note] Note

This function internally calls the SFBAStream::Readable function.

[Note] Prerequisite

The connection of this socket needs to be established with the SFXTCPSocket::Connect function before this function is called.

And if this socket has been already scheduled to read data, the SFERR_INVALID_STATE error will be returned.

Example

// define _socket, SFXTCPSocket instance, as member variable   
class TcpSample {
private:
    SFXTCPSocket _socket;
public:
    // callback function declaration
    XALLBACK_DECLARE_SFXTCPSOCKET(OnWrite)  
    XALLBACK_DECLARE_SFXTCPSOCKET(OnRead)  
};

// callback function notified that data can be sent
XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error)
{
    UInt32 size;
    static ACharConst data[] = "GET / HTTP/1.0\n\n";
    
    // write data
    size = sizeof(data) - 1;
    switch (error = _socket.Write(data, &size)) {
        case SFERR_NO_ERROR:
             // schedule to read data
            _socket.ScheduleRead(XALLBACK_INTERNAL(OnRead)); // register callback function
            break;
        case AEE_NET_WOULDBLOCK:
            // schedule to write data 
            _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite)); // register callback function
            break;
    }
}

// callback function notified that data is ready to be read
XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnRead, error)
{
    SFXBuffer buffer;
    UInt32 size;

    // read data
    buffer.SetSize(32);
    size = buffer.GetSize();
    switch (_socket.Read(buffer.GetBuffer(), &size)) {
        case SFERR_NO_ERROR:

           // check whether or not the specified size of data is read
           if (size == buffer.GetSize()) {

                // display the read data
                buffer.SetSize(buffer.GetSize() + 1);
                buffer[buffer.GetSize() - 1] = '\0';
                TRACE("%s", SFXAnsiString(buffer).GetCString());
                
               // close the connection
                _socket.Close();
            }
            else {
                ...
            }
            break;

        case AEE_NET_WOULDBLOCK:
            // schedule to read data 
            _socket.ScheduleRead(XALLBACK_INTERNAL(OnRead)); // register callback function
            break;
    }
    return;
}

Reference

SFXTCPSocket::Read | SFXTCPSocket::Accept | SFBAStream::Readable | SFXStorage::CallbackSPP | BREW API ISOCKET_Readable


SFXTCPSocket::ScheduleWrite
Schedule to write data into this TCP socket without output stream.
[ public, virtual ]
SFCError ScheduleWrite(
    CallbackSPP spp     // callback function
    VoidPtr reference   // data passed to callback function
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the connection of this socket has not been established, or writing data has already been scheduled: SFERR_INVALID_STATE

Description

This function schedules to write data into this TCP socket without output stream.

Concretely, this function registers the callback function that will write data with the SFXTCPSocket::Read function. The registered callback function will be called by BREW AEE when it is possible to write data.

The status of this socket is "under scheduling to write data" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "the connection of this socket is established".

If you call the SFXTCPSocket::Cancel function before the callback function is called, writing data will be canceled and the status will be returned to the status before calling this function, i.e., "the connection of this socket is established".

*1. If the SFXTCPSocket::Write function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with this function where data will be written with the SFXTCPSocket::Write function.

*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.

[Note] Note

This function internally calls the SFBSocket::Writeable function.

[Note] Prerequisite

The connection of this socket needs to be established with the SFXTCPSocket::Connect function or this socket needs to be the established socket returned via the return argument of the SFXTCPSocket::Accept function before this function is called.

And if this socket has been already scheduled to write data, the SFERR_INVALID_STATE error will be returned.

Example

 // define _socket, SFXTCPSocket instance, as member variable   
class TcpSample {
private:
    SFXTCPSocket _socket;
public:
    // callback function declaration
    XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)  
    XALLBACK_DECLARE_SFXTCPSOCKET(OnWrite)  
};

// callback function notified of the connection result
XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
        _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite));
    }
}

// callback function notified that data is ready to be written
XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error)
{
    UInt32 size;
    static ACharConst data[] = "GET / HTTP/1.0\n\n";
    
    // write data
    size = sizeof(data) - 1;
    switch (error = _socket.Write(data, &size)) {
        case SFERR_NO_ERROR:
            ...
            break;
        case AEE_NET_WOULDBLOCK:
            // schedule to write data 
            _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite)); // register callback function
            break;
    }
}

Reference

SFXTCPSocket::Write | SFBSocket::Writeable | SFXTCPSocket::Accept | SFXStorage::CallbackSPP | BREW API ISOCKET_Writeable


SFXTCPSocket::Write
Write data into this TCP socket without output stream.
[ public, virtual ]
SFCError Write(
    VoidConstPtr buffer   // buffer to write data into
    UInt32Ptr size        
// before this function is called: size of the buffer to write data into; 
just after this function is returned: size of the data to be written actually

);

Argument

buffer

Specify the buffer to write data into.

size

Before calling this function, specify the size of the buffer to write data into. Just after this function is returned, the size of data to be written into the buffer actually will be stored into this argument.

Return value

  • If succeeds: SFERR_NO_ERROR
  • If connection of this socket has not been established: SFERR_INVALID_STATE
  • If the size argument is null: SFERR_INVALID_PARAM
  • If retry is necessary: AEE_NET_WOULDBLOCK
  • If other network error occurs: error value obtained by calling the BREW API ISOCKET_GetLastError function or AEE_NET_ERROR

Description

This function writes data to this TCP socket without output stream.

* If this function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with the SFXTCPSocket::ScheduleWrite function where data will be written with this function.

[Note] Note

This function internally calls the SFBSocket::Write function.

[Note] Prerequisite

The connection of this socket needs to be established with the SFXTCPSocket::Connect function or be the established socket returned via the return argument of the SFXTCPSocket::Accept function before this function is called.

Example

 // define _socket, SFXTCPSocket instance, as member variable   
class TcpSample {
private:
    SFXTCPSocket _socket;
public:
    // callback function declaration
    XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)  
    XALLBACK_DECLARE_SFXTCPSOCKET(OnWrite)  
};

// callback function notified of the connection result
XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
        _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite));
    }
}

// callback function notified that data is ready to be written
XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error)
{
    UInt32 size;
    static ACharConst data[] = "GET / HTTP/1.0\n\n";
    
    // write data
    size = sizeof(data) - 1;
    switch (error = _socket.Write(data, &size)) {
        case SFERR_NO_ERROR:
            ...
            break;
        case AEE_NET_WOULDBLOCK:
            // schedule to write data 
            _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite)); // register callback function
            break;
    }
}

Reference

SFXTCPSocket::ScheduleWrite | SFXTCPSocket::Accept | SFBSocket::Write | BREW API ISOCKET_Write