![]() ![]() ![]()
|
BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1 |


SFXTCPSocket class encapsulates BREW API ISocket interface and provides high level TCP socket communication functions.
How to use the TCP socket
Example 495. Sample code for the TCP socket communication
// define _tcp, instance of SFXTCPSocket class, as member variable class NetworkTime { private: SFXTCPSocket _tcp; SFXBinaryStreamReader _reader; public: Void Start(Void); // start to connect to NTP server Void Stop(Void); // stop connecting to NTP server // callback function declaration CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) CALLBACK_DECLARE_SFXBINARYSTREAMREADER(OnFetch) }; // start to connect to NTP server Void NetworkTime::Start(Void) { SFCError error(SFERR_NO_ERROR); // set NTP server address SFXSocketAddress address("www.example.com:37"); // initialize connection to NTP server if ((error = _tcp.Open()) == SFERR_NO_ERROR) { // connect to NTP server // connection establishment will be notified to OnConnect function if ((error = _tcp.Connect(address, CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) { ... } else { // if error occurs _tcp.Close(); } } if (error != SFERR_NO_ERROR) { // if error occurs // error handling ... } return; } // stop connecting to NTP server Void NetworkTime::Stop(Void) { // release resource _reader.Release(); _tcp.Close(); return; } // callback function notified of connection establishment CALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error) { if (error == SFERR_NO_ERROR) { // get input stream for reading data if ((error = _tcp.GetStreamReader(64, &_reader)) == SFERR_NO_ERROR) { // receive data actually (completion of receiving data actually will be notified to OnFetch callback function ) if ((error = _reader.Fetch(4, CALLBACK_FUNCTION(OnFetch))) == SFERR_NO_ERROR) { ... } if (error != SFERR_NO_ERROR) { // if error occurs _reader.Release(); } } } if (error != SFERR_NO_ERROR) { // if error occurs _tcp.Close(); } return; } // callback function notified of completion of receiving data actually CALLBACK_IMPLEMENT_SFXBINARYSTREAMREADER(NetworkTime, OnFetch, error) { SFXDate date; // date UInt32 time; if (error == SFERR_NO_ERROR) { // set to read data as Big Endian _reader.SetEndian(SFXBinaryStreamReader::ENDIAN_BIG); // read data as UInt32 type from input stream into time variable if ((error = _reader.ReadUInt32(&time)) == SFERR_NO_ERROR) { // set time (in seconds) date.Set(time); // addjust time since from January 1, 1900 date -= SFXDateDuration::Offset19000101(); // convert into local time date += SFXDateDuration::LocalTimeOffset(); // format and out put TRACE("%s", date.Format("YYYY/MM/DD hh:mm:ss").GetCString()); } } if (error != SFERR_NO_ERROR) { // if error occurs // error handling ... } // release resource _reader.Release(); _tcp.Close(); return; }
| Constructor/Destructor |
|---|
|
SFXTCPSocket( Void ) Constructor of SFXTCPSocket class.
|
|
~SFXTCPSocket( Void ) Destructor of SFXTCPSocket class.
|
| Public Functions | |
|---|---|
| SFCError |
Accept(
SFXTCPSocketPtr result
) Wait for connecting to the TCP server.
|
| SFCError |
AsSFBAStream(
SFBAStreamSmpPtr result
) Convert the instance of SFBSocket class internally managed into the instance of SFBAStream class.
|
| SFCError |
AsSFBSource(
SFBSourceSmpPtr result
) Convert the instance of SFBSocket class internally managed into the instance of SFBSource class.
|
| SFCError |
Bind(
SFXSocketAddressConstRef address
) Bind the local IP address and port number to the socket.
|
| Void |
Cancel( Void ) Cancel the TCP socket communication.
|
| Void |
Close( Void ) Close the TCP Socket.
|
| SFCError |
Connect(
SFXSocketAddressConstRef address
, CallbackSPP spp
, VoidPtr reference
) Connect to the TCP server.
|
| SFCError |
GetLocalAddress(
SFXSocketAddressPtr result
) Get the local IP address and port number.
|
| SFCError |
GetRemoteAddress(
SFXSocketAddressPtr result
) Get the remote IP address and port number.
|
| SFBSocketSmpConstRef |
GetSFBSocket( Void ) Get the socket that is internally used.
|
| SFCError |
GetStreamReader(
UInt32 size
, SFXStreamReaderPtr result
) GetStreamReader( SFXStreamReaderPtr result ) Get the input stream for reading data from the TCP socket.
|
| SFCError |
GetStreamWriter(
UInt32 size
, SFXStreamWriterPtr result
) GetStreamWriter( SFXStreamWriterPtr result ) Get the output stream for writing data to the TCP socket.
|
| SFCError |
Listen(
SInt16 backlog = 1
) Start to wait for the TCP connection.
|
| SFCError |
Open( Void ) Open the TCP socket.
|
| SFCError |
Read(
VoidPtr buffer
, UInt32Ptr size
) Receive data.
|
| SFCError |
ScheduleAccept(
CallbackSPP spp
, VoidPtr reference
) Schedule to accept.
|
| SFCError |
ScheduleBind(
CallbackSPP spp
, VoidPtr reference
) Schedule to bind.
|
| SFCError |
ScheduleListen(
CallbackSPP spp
, VoidPtr reference
) Schedule to listen.
|
| SFCError |
ScheduleRead(
CallbackSPP spp
, VoidPtr reference
) Schedule to receive data.
|
| SFCError |
ScheduleWrite(
CallbackSPP spp
, VoidPtr reference
) Schedule to send data.
|
| SFCError |
Write(
VoidConstPtr buffer
, UInt32Ptr size
) Send data.
|
| Types |
|---|
|
CallbackSPP
(inherits from SFXStorage)
The prototype of the callback function for the SFXStorage class.
|
[ public, explicit ] SFXTCPSocket(Void);
[ public, virtual ] virtual ~SFXTCPSocket(Void);
The SFXTCPSocket::Close function is called in the destructor.
[ public, const ] SFCError Accept( SFXTCPSocketPtr result // pointer to TCP socket );
When the return value of the SFXTCPSocket::Accept function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleAccept function, and then schedule the SFXTCPSocket::Accept in the callback function again.
[ public, virtual, const ] SFCError AsSFBAStream( SFBAStreamSmpPtr result // pointer to the result SFBAStream class instance );
[ public, virtual, const ] SFCError AsSFBSource( SFBSourceSmpPtr result // pointer to the result SFBSource class instance );
The SFXTCPSocket::AsSFBSource function creates and releases the instance of SFBSourceUtil class used internally.
[ public ] SFCError Bind( SFXSocketAddressConstRef address // IP address and port number );
When the return value of SFXTCPSocket::Bind function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleBind function, and then schedule the SFXTCPSocket::Bind function in the callback function again.
[ public, virtual ] Void Cancel(Void);
[ public ] Void Close(Void);
Cancel the TCP socket communication, and then the ISocket and INetMgr interfaces used internally are released.
When the BREW application is suspended, call the SFXTCPSocket::Close function to release the resources.
[ public ] SFCError Connect( SFXSocketAddressConstRef address // domain (or IP address) and port number CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
Specify domain (or IP address) and port number. The SFXTCPSocket class will automatically resolve the domain when needed.
Specify the callback function notified of connection establishment.
Specify the data passed to the callback function.
The connection establishment is notified to the callback function registered using the SFXTCPSocket::Connect function.
![]() |
Errors during the connection |
|---|---|
The return value does not include the errors while connecting to the TCP server, which are notified to the callback function. | |
// define _tcp, instance of SFXTCPSocket class, as member variable class TcpSample { private: SFXTCPSocket _tcp; public: Void Start(Void); CALLBACK_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 = _tcp.Open()) == SFERR_NO_ERROR) { // connect to TCP server if ((error = _tcp.Connect(address,CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) { ... }else { // if error occurs _tcp.Close(); } } if (error != SFERR_NO_ERROR) { // if error occurs // error handling ... } } // callback function notified of connection establishment CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error) { if (error == SFERR_NO_ERROR) { ... } }
[ public, const ] SFCError GetLocalAddress( SFXSocketAddressPtr result // pointer to the local IP address and port number );
![]() |
Change of Local IP address |
|---|---|
The local IP address might be changed between before and after the TCP socket connection. | |
[ public, const ] SFCError GetRemoteAddress( SFXSocketAddressPtr result // pointer to the remote IP address and port number );
![]() |
Change of Remote IP address |
|---|---|
The remote IP address might be changed between before and after the TCP socket connection. | |
[ public, const ] SFBSocketSmpConstRef GetSFBSocket(Void);
Get the socket (instance of SFBSocket class) that is used internally.
[ public, virtual ] SFCError GetStreamReader( UInt32 size // buffer size SFXStreamReaderPtr result // pointer to the stream );
[ public, virtual ] SFCError GetStreamReader( SFXStreamReaderPtr result // pointer to the stream );
The buffer size of the input stream can be specified. Use the SFXBinaryStreamReader, SFXAnsiStringStreamReader, or SFXWideStringStreamReader class for the input stream properly according to the type of data to read.
Using SFXBinaryStreamReader.
// define _tcp, instance of SFXTCPSocket class, as member variable class NetworkTime { private: SFXTCPSocket _tcp; SFXBinaryStreamReader _reader; public: // callback function declaration CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) }; // callback function notified of connection establishment CALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error) { if (error == SFERR_NO_ERROR) { // get input stream for receiving data if ((error = _tcp.GetStreamReader(64, &_reader)) == SFERR_NO_ERROR) { ... } } return; }
SFXTCPSocket::GetStreamWriter | SFXBinaryStreamReader | SFXAnsiStringStreamReader | SFXWideStringStreamReader
[ 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 );
The buffer size of the output stream can be specified. Use the SFXBinaryStreamWriter, SFXAnsiStringStreamWriter, or SFXWideStringStreamWriter class for the output stream properly according to the written data type
Using SFXBinaryStreamwriter.
// define _tcp, instance of SFXTCPSocket class, as member variable class NetworkTime { private: SFXTCPSocket _tcp; SFXBinaryStreamwriter _writer; public: // callback function declaration CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) }; // callback function notified of connection establishment CALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error) { if (error == SFERR_NO_ERROR) { // get output stream for sending data if ((error = _tcp.GetStreamWriter(64, &_writer)) == SFERR_NO_ERROR) { ... } } return; }
SFXTCPSocket::GetStreamReader | SFXBinaryStreamWriter | SFXAnsiStringStreamWriter | SFXWideStringStreamWriter
If the return value of the SFXTCPSocket::Listen function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleListen function, and then schedule the SFXTCPSocket::Listen function in the callback function again.
[ public ] SFCError Open(Void);
Create the instance of SFBNetMgr class and open the TCP socket(instance of SFBSocket class).
// define _tcp, instance of SFXTCPSocket class, as member variable class TcpSample { private: SFXTCPSocket _tcp; public: Void Start(Void); CALLBACK_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 = _tcp.Open()) == SFERR_NO_ERROR) { // connect to TCP server if ((error = _tcp.Connect(address,CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) { ... }else { // if error occurs _tcp.Close(); } } if (error != SFERR_NO_ERROR) { // if error occurs // error handling ... } } // callback function notified of connection establishment CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error) { if (error == SFERR_NO_ERROR) { ... } }
[ public, virtual ] SFCError Read( VoidPtr buffer // buffer that stores the received data UInt32Ptr size // buffer size );
Specify the buffer to store the received data.
Specify the buffer size when calling the SFXTCPSocket::Read function. After the SFXTCPSocket::Read function is executed, the size of received data is stored.
To receive data without using the input stream, use the SFXTCPSocket::Read function.
When the return value of SFXTCPSocket::Read function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleRead function, and then schedule the SFXTCPSocket::Read function in the callback function again.
// define _tcp, instance of SFXTCPSocket class, as member variable class TcpSample { private: SFXTCPSocket _tcp; public: // callback function declaration CALLBACK_DECLARE_SFXTCPSOCKET(OnWrite) CALLBACK_DECLARE_SFXTCPSOCKET(OnRead) }; // callback function notified of sending data CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error) { UInt32 size; static ACharConst data[] = "GET / HTTP/1.0\n\n"; // write data size = sizeof(data) - 1; switch (error = _tcp.Write(data, &size)) { case SFERR_NO_ERROR: // schedule to receive data _tcp.ScheduleRead(CALLBACK_FUNCTION(OnRead)); // register callback function break; case AEE_NET_WOULDBLOCK: // schedule to send data _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite)); // register callback function break; } } // callback function notified of completion of receiving data actually CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnRead, error) { SFXBuffer buffer; UInt32 size; // read data buffer.SetSize(32); size = buffer.GetSize(); switch (_tcp.Read(buffer.GetBuffer(), &size)) { case SFERR_NO_ERROR: // check whether data of specified size is read or not if (size == buffer.GetSize()) { // display received data buffer.SetSize(buffer.GetSize() + 1); buffer[buffer.GetSize() - 1] = '\0'; TRACE("%s", SFXAnsiString(buffer).GetCString()); // close connection _tcp.Close(); } else { ... } break; case AEE_NET_WOULDBLOCK: // schedule to receive data _tcp.ScheduleRead(CALLBACK_FUNCTION(OnRead)); // register callback function break; } return; }
[ public ] SFCError ScheduleAccept( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
The SFXTCPSocket::ScheduleAccept function is used to register the callback function in which the SFXTCPSocket::Accept function is used to wait for connecting to the TCP server.
![]() |
Note |
|---|---|
| When the return value of SFXTCPSocket::Accept function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleAccept function, and then schedule the SFXTCPSocket::Accept function in the callback function again. | |
[ public ] SFCError ScheduleBind( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
The SFXTCPSocket::ScheduleBind function is used to register the callback function in which Bind is done using the SFXTCPSocket::Bind function.
![]() |
Note |
|---|---|
| When the return value of SFXTCPSocket::Bind function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleBind function, and then schedule the SFXTCPSocket::Bind function in the callback function again. | |
[ public ] SFCError ScheduleListen( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
The SFXTCPSocket::ScheduleListen function is used to register the callback function in which the SFXTCPSocket::Listen function is used to wait(Listen) for the TCP connection.
![]() |
Note |
|---|---|
| When the return value of SFXTCPSocket::Listen function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleListen, and then schedule the SFXTCPSocket::Listen function in the callback function again. | |
[ public, virtual ] SFCError ScheduleRead( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
The SFXTCPSocket::ScheduleRead function is used to register the callback function in which the SFXTCPSocket::Read function is used to receive data.
![]() |
Note |
|---|---|
| When the return value of SFXTCPSocket::Read function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleRead function, and then schedule the SFXTCPSocket::Read function in the callback function again. | |
// define _tcp, instance of SFXTCPSocket class, as member variable class TcpSample { private: SFXTCPSocket _tcp; public: // callback function declaration CALLBACK_DECLARE_SFXTCPSOCKET(OnWrite) CALLBACK_DECLARE_SFXTCPSOCKET(OnRead) }; // callback function notified of completion of sending data CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error) { UInt32 size; static ACharConst data[] = "GET / HTTP/1.0\n\n"; // send data size = sizeof(data) - 1; switch (error = _tcp.Write(data, &size)) { case SFERR_NO_ERROR: // schedule to receive data _tcp.ScheduleRead(CALLBACK_FUNCTION(OnRead)); // register callback function break; case AEE_NET_WOULDBLOCK: // schedule to send data _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite)); // register callback function break; } } //callback function notified of completion of receiving data actually CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnRead, error) { SFXBuffer buffer; UInt32 size; // read data buffer.SetSize(32); size = buffer.GetSize(); switch (_tcp.Read(buffer.GetBuffer(), &size)) { case SFERR_NO_ERROR: // check whether data with specified size is received or not if (size == buffer.GetSize()) { // display received data buffer.SetSize(buffer.GetSize() + 1); buffer[buffer.GetSize() - 1] = '\0'; TRACE("%s", SFXAnsiString(buffer).GetCString()); _tcp.Close(); // close TCP socket } else { ... } break; case AEE_NET_WOULDBLOCK: // schedule to receive data _tcp.ScheduleRead(CALLBACK_FUNCTION(OnRead)); // register callback function break; } return; }
[ public, virtual ] SFCError ScheduleWrite( CallbackSPP spp // callback function VoidPtr reference // pointer to callback function );
The SFXTCPSocket::ScheduleWrite function is used to register the callback function in which the SFXTCPSocket::Write function is used to send data.
![]() |
Note |
|---|---|
| When the return value of SFXTCPSocket::Write function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleWrite function, and then schedule the SFXTCPSocket::Write function in the callback function again. | |
// define _tcp, instance of SFXTCPSocket class, as member variable class TcpSample { private: SFXTCPSocket _tcp; public: // callback function declaration CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) CALLBACK_DECLARE_SFXTCPSOCKET(OnWrite) }; // callback function notified of connection establishment CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error) { if (error == SFERR_NO_ERROR) { _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite)); } } // callback function notified of completion of sending data CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error) { UInt32 size; static ACharConst data[] = "GET / HTTP/1.0\n\n"; // send data size = sizeof(data) - 1; switch (error = _tcp.Write(data, &size)) { case SFERR_NO_ERROR: ... break; case AEE_NET_WOULDBLOCK: // schedule to send data _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite)); // register callback function break; } }
[ public, virtual ] SFCError Write( VoidConstPtr buffer // data to write UInt32Ptr size // size of data to write );
Data to write.
Specify the buffer size when calling the SFXTCPSocket::Write function. After the SFXTCPSocket::Write function is executed, the size of actually written data is stored.
To write data without using the output stream, use the SFXTCPSocket::Write function.
When the return value of SFXTCPSocket::Write function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleWrite function, and then call the SFXTCPSocket::Write function in the callback function again.
// define _tcp, instance of SFXTCPSocket class, as member variable class TcpSample { private: SFXTCPSocket _tcp; public: // callback function declaration CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) CALLBACK_DECLARE_SFXTCPSOCKET(OnWrite) }; // callback function notified of connection establishment CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error) { if (error == SFERR_NO_ERROR) { _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite)); } } // callback function notified of completion of sending data CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error) { UInt32 size; static ACharConst data[] = "GET / HTTP/1.0\n\n"; // write data size = sizeof(data) - 1; switch (error = _tcp.Write(data, &size)) { case SFERR_NO_ERROR: ... break; case AEE_NET_WOULDBLOCK: // schedule to send data _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite)); // register callback function break; } }
|
Copyright (C) 2002 - 2009 Sophia Cradle, Inc. All Rights Reserved. |
![]() ![]() ![]()
|