MTK Socket HTTP connection mode flowchart

Source: Internet
Author: User
Tags http request set socket socket

MTK Socket HTTP connection mode flowchart

1. Create a socket connection

Kal_int8 soc_create (kal_uint8 domain,

Socket_type_enum type,

Kal_uint8 protocol,

Module_type mod_id,

Kal_uint32 nwk_account_id)

2. Set socket properties, set socket non-blocking mode

Kal_int8 soc_setsockopt (kal_int8 s, kal_uint16 option, Kal_uint8 *val, Kal_uint8 val_size)

Kal_uint8 val = 1;

Soc_setsockopt (Socket_id,soc_nbio,&val, sizeof (VAL))

val = Soc_read | Soc_write | Soc_close | Soc_connect;

Soc_setsockopt (Socket_id,soc_async,&val,sizeof (val))

3. Connect to China Mobile gateway or cmnet server public network IP

Kal_int8 Soc_connect (kal_int8 s, sockaddr_struct *addr)

4. Callback function

Setprotocoleventhandler (psfuncptr funcptr, U16 EventID)

Funcptr in response to socket events

This function runs through the SOKCET connection, sends, and receives the process. Reasonable design and application of this function is the key to successfully complete a socket communication

5. Sending an HTTP request

Kal_int32 Soc_send (kal_int8 s, kal_uint8 *buf, Kal_int32 len, kal_uint8 flags)

where the BUF string needs to adhere to the HTTP connection protocol, for example, the format for accessing the weather is as follows:

unsigned char weather_url[]={"GET http://61.135.159.21/cgi-bin/weather?0755 http/1.1\r\nhost:61.135.159.21:80\r\ Nuser-agent:*/*\r\naccept:*/*\r\nx-online-host:61.135.159.21\r\nproxy-connection:keep-alive\r\n\r\n "}

6. Receiving data

Kal_int32 Soc_recv (kal_int8 s, kal_uint8 *buf, Kal_int32 len, kal_uint8 flags)

BUF is used to save the received string, it is important to note that the length of the BUF can not exceed 2000, otherwise on the real machine when the application of memory space will panic, here I use the loop to receive information and write to the file.

7 Closing the Socket connection

Kal_int8 Soc_close (kal_int8 s)

Return value of parameter passed in Soc_create

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.