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