Synchronous socket usage in Symbian

Source: Internet
Author: User
Tags inif

// The following Code describes the usage of synchronous socket:

// The required lib may not be complete. Please refer to the SDK help to add it yourself

Esock. Lib

// The required header file may not be complete. Please refer to the SDK help to add it yourself
# Include <in_sock.h>
# Include <nifman. h>
# Include <es_sock.h>

# If defined _ wins __
_ Connection (kconnectip, "192.168.0.1"); // change the connection IP address to the IP address used for testing.
# Else
_ Forward (kconnectip, "10.0.0.172"); // move the cmwap Proxy Gateway
# Endif

Const int kconnectport = 80; // Port

Hbufc8 * socketgeturl (tdesc8 & geturl );

Void testsocket ()
{
_ Lit8 (ksenddata, "post http: // www.google.com.cn HTTP/1.1/R/n/R/N ");
Tbuf8 <128> senddata (ksenddata );
Hbufc8 * Body = socketgeturl (senddata );
If (Body)
{

// Process the data returned by the server

Delete body;
}

}

Hbufc8 * socketgeturl (tdesc8 & geturl)
{
Rnif inif;
Rsocket isocket;
Trequeststatus istatus = krequestpending;
Tbuf8 <512> writebuf (geturl );
Tbuf8 <64> recvbuf;
Hbufc8 * ibuffer = NULL;

Inif. open ();
{
Rsocketserv isocketserv;
User: leaveiferror (isocketserv. Connect ());
Tinetaddr iaddress;
If (iaddress. Input (kconnectip) = kerrnone)
{
User: leaveiferror (isocket. Open (isocketserv, kafinet, ksockstream, kprotocolinettcp ));
Iaddress. setport (kconnectport );

Istatus = krequestpending;
Isocket. Connect (iaddress, istatus );
User: waitforrequest (istatus );
If (istatus. INT () = kerrnone)
{
Istatus = krequestpending;
Isocket. Write (writebuf, istatus );
User: waitforrequest (istatus );
If (istatus. INT () = kerrnone)
{
Tsockxfrlength idummylength;
Tint recvsize = 0;

For (;;)
{
Istatus = krequestpending;
Isocket. recvoneormore (recvbuf, 0, istatus, idummylength );
User: waitforrequest (istatus );

If (istatus. INT () = kerrnone)
{
Recvsize + = recvbuf. Length ();
If (ibuffer = NULL)
Ibuffer = hbufc8: newl (recvsize );
Else
Ibuffer = ibuffer-> realloc (recvsize );
Ibuffer-> des (). append (recvbuf );
}
Else
{
Break;
}
}
}
}
Isocket. Close ();
}

Isocketserv. Close ();
}
Inif. Close ();
Return ibuffer;
}

This code is just a brief introduction to socket usage, without timeout processing. If you want to perform timeout processing, add it yourself.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.