转 http://blog.csdn.net/maple_wei/article/details/7564739
一、添加新文件:新建TicketSocket.c 文件到..\plutommi\mmi\TicketApp\TicketAppSrc目录下
二、将文件添加工程里:
在文件..\make\plutommi\mmi_app中添加如下文件信息plutommi\mmi\TicketApp\TicketAppSrc\TicketSocket.c。
三、MTK 实现socket连接,需要用到一下函数:
用到的头文件有:
#include "Soc_consts.h"
#include "Soc_api.h"
#include "app2soc_struct.h"
#include "TimerEvents.h"
1、 soc_cteat创建一个socket
kal_int8 soc_creat(kal_uint8 domain, //协议,现在只支持SOC_PF_INET
socket_type_enum type, 在该demain下的类型,现在有SOC_SOCK_STREAM(TCP), SOC_SOCK_DGRAM(UDP), SOC_SOCK_SMS, SOC_SOCK_RAM
kal_uint8 protocol, //协议类型,除了type为SOC_SOCK_RAM,其他都为0
module_type mod_id, //当前模块ID,主要用于接收消息
kal_uint32 nwk_account_id) //接入点,cmwap为10, cmnet为14
2、 设置socket非阻塞模式
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、 连接中国移动网关
Kal_int8 soc_connect(kal_int8 s, sockaddr_struct *daar)
手机上网与pc上网不同,选择cmwap,手机端需要首先连接移动网关才能进行网络通信,
选择cmnet可以直接连接internet,这里需要填写结构体sockaddr_struct的addr即port成员。
4、 发送http请求
Kal_int32 soc_send(kal_int8 s, kal_uint8 *buf, kal_int32 len, kal_uint8 flags)
其中buf子串需要遵守http连接协议。
5、 接收数据
Kal_int32 soc_recv(kal_int8 s, kal_uint8 *buf, kal_int32 len, kal_uint8 flags)
Buf用来保存接收到的字符串,这里需要注意buf的长度不能超过2000,否则上真机的时候到申请内存空间时就会死机,可以用循环来接收信息。
6、 关闭socket连接
Kal_int8 soc_close(kal_int8 s)
参数传入soc_create的返回值。
在程序中的代码在真机中实现了插入sim卡可以获取北京的今明两天的天气情况,在模拟器中调试时可以连接tcp调试助手进行socket同,互发、收数据。
下面为具体代码:
#include "MMI_features.h"
#include "PixtelDataTypes.h"
#include "gdi_include.h"
#include "wgui.h"
#include "gui.h"
#include "Globaldefs.h"
#include "CustDataRes.h"
#include "gui_themes.h"
#include "wgui_categories.h"
#include "wgui_categories_util.h"
#include "wgui_softkeys.h"
#include "HistoryGprot.h"
#include "OrganizerDef.h"
#include "..\plutommi\mmi\TicketApp\TicketAppInc\TicketAppDefs.h"
#include "..\plutommi\mmi\TicketApp\TicketAppInc\TicketAppResDef.h"
#include "..\plutommi\mmi\TicketApp\TicketAppInc\TicketAppProt.h"
#include "MainMenudef.h"
#include "Unicodexdcl.h"
#include "Gui.h"
#include "Conversions.h"
#include "Wgui_categories_list.h"
#include "uart_sw.h"
#include "uart_hw.h"
#include "Soc_consts.h"
#include "Soc_api.h"
#include "app2soc_struct.h"
#include "TimerEvents.h"
#define BUF_SIZE 1024
kal_int8 ticket_socket_id;//创建socket连接的返回值//创建socket连接的返回值(设置全局变量,定义在邋錞icketSocket.c中)
kal_int8 ret;
kal_int8 ticket_soc_init()
{
kal_uint8 val = 1;
sockaddr_struct server_addr;
server_addr.addr[0] = 61;//192;//220;//10;
server_addr.addr[1] = 135;//168;//181;//0;
server_addr.addr[2] = 159;//0;//6;//0;
server_addr.addr[3] = 21;//128;//175;//172;
server_addr.port = 80;
server_addr.addr_len = 4;
server_addr.sock_type = SOC_SOCK_STREAM;
//soc_init_win32();
//创建socket连接成功返回socket_id
ticket_socket_id = soc_create( SOC_PF_INET, //协议 现在只支持SOC_PF_INEF
SOC_SOCK_STREAM, //类型为 TCP
0, //协议类型
MOD_MMI, //当前模块id,主要用于接受消息
14 //接入点 MTK的网络接入点是cmwap,值为10;cmnet,值为14
);
dbg_print("creat socket: ticket_socket_id = %x\r\n", ticket_socket_id);
if(ticket_socket_id >= 0)
{
dbg_print("set socket...\r\n var = %x\r\n", val);
if (soc_setsockopt(ticket_socket_id, SOC_NBIO, &val, sizeof(val)) < 0)
{
dbg_print("set socket error1\r\n");
soc_close(ticket_socket_id);//关闭socket
return -0x11;
}
val = SOC_READ | SOC_WRITE | SOC_CLOSE | SOC_CONNECT;
if (soc_setsockopt(ticket_socket_id, SOC_ASYNC, &val, sizeof(val)) < 0)
{
dbg_print("set socket error2\r\n");
soc_close(ticket_socket_id);//关闭socket
return -0x11;
}
ret = soc_connect(ticket_socket_id, &server_addr);
dbg_print("connect: ret = %x\r\n var = %x\r\n", ret, val);
return ret;
}
//dbg_print("zhanweizhao6\r\n");
else
return -0x11;
}
void close_sock()
{
soc_close(ticket_socket_id);//关闭socket
}
kal_uint32 ticketcount;
void timeout()
{
ticketcount=1;
StopTimer(TicketTimerId);//关闭定时器
}
void sock_send()
{
kal_uint8 wbuf[100] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,};
//kal_uint8 rbuf[1024];
kal_int32 ret;
//kal_uint8 weather_URL[] = { "GET http://61.135.159.21/cgi-bin/weather?010 HTTP/1.1\r\n Host: 10.0.0.172:80\r\n User-Agent:*/* \r\n Accept:*/* \r\n X-Online-Host: 61.135.159.21\r\n Proxy-Connection: Keep-Alive\r\n\r\n"};
kal_uint8 weather_URL[] = { "GET http://61.135.159.21/cgi-bin/weather?010 HTTP/1.1\r\nHost:61.135.159.21:80\r\nUser-Agent:*/*\r\nAccept:*/*\r\nX-Online-Host:61.135.159.21\r\nConnection:Keep-Alive\r\n\r\n"};
EntryNewScreen(SCR_TICKET_BUY, NULL, NULL, NULL); //MAIN_MENU_SCREENID 主菜单屏幕ID
entry_full_screen(); //进入新程序时调用EntryNewScreen
clear_screen(); //将整个屏幕刷成白色
//ret = soc_send(ticket_socket_id, wbuf, sizeof(wbuf), 0);
dbg_print("ready to send...\r\n");
ret = soc_send(ticket_socket_id, weather_URL, sizeof(weather_URL), 0);
dbg_print("send: ret = %x\r\n", ret);
if(ret > 0)
{
SetProtocolEventHandler(soc_socket_notify, MSG_ID_APP_SOC_NOTIFY_IND);//注册钩子函数处理服务器端socket发来的数据
}
else if(ret == -12)//socket is not connect
show_chinese((kal_uint8 *)"请插入sim卡", 36, 25, 120, UI_COLOR_DARK_GREY);
gui_BLT_double_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);//用来刷新屏幕
SetKeyHandler(goback, KEY_RSK, KEY_EVENT_UP);
return;
}
U32 sock_receive()
{
kal_uint8 rbuf[1024];
kal_uint8 rbuf1[50];
kal_uint8 rbuf2[50];
kal_uint8 rbuf3[50];
kal_uint8 rbuf4[50];
kal_uint8 rbuf5[50];
kal_uint8 rbuf6[50];
kal_uint8 rbuf7[50];
kal_uint8 rbuf8[50];
kal_uint8 rbuf9[50];
kal_uint8 rbuf10[50];
kal_uint8 recvbuf[BUF_SIZE];
kal_uint32 ret, ret1;
int i, j;
//StopTimer(SAT_MESSAGE_WAIT_TIMER);
//StartTimer(SAT_MESSAGE_WAIT_TIMER, 5000, timeout);//启动定时器, 定时器ID TicketTimerId定义在文件TimerEvents.h的1803行
//while(1)
{
ret = soc_recv(ticket_socket_id, rbuf, sizeof(rbuf), 0);
dbg_print("receive ok: ret = %x \r\n", ret);
//close_sock();//关闭socket
for(i = 0; i<50; i++)
rbuf1[i] = rbuf[i];
for(i = 50, j = 0; i<100; i++, j++)
rbuf2[j] = rbuf[i];
for(i = 100, j = 0; i<150; i++, j++)
rbuf3[j] = rbuf[i];
for(i = 150, j = 0; i<159; i++, j++)
rbuf4[j] = rbuf[i];
for(i = 159, j = 0; i<208; i++, j++)
rbuf5[j] = rbuf[i];
for(i = 208, j = 0; i<260; i++, j++)
rbuf6[j] = rbuf[i];
for(i = 260, j = 0; i<310; i++, j++)
rbuf7[j] = rbuf[i];
for(i = 310, j = 0; i<360; i++, j++)
rbuf8[j] = rbuf[i];
if(ret > 100)
{
EntryNewScreen(SCR_TICKET_BUY, NULL, NULL, NULL); //MAIN_MENU_SCREENID 主菜单屏幕ID
entry_full_screen(); //进入新程序时调用EntryNewScreen
clear_screen(); //将整个屏幕刷成白色
//show_chinese(rbuf1, 50, 10, 10, UI_COLOR_DARK_GREY);
//show_chinese(rbuf2, 50, 10, 30, UI_COLOR_DARK_GREY);
//show_chinese(rbuf3, 50, 10, 50, UI_COLOR_DARK_GREY);
//show_chinese(rbuf4, 50, 10, 70, UI_COLOR_DARK_GREY);
show_chinese(rbuf5, 50, 10, 90, UI_COLOR_DARK_GREY);
show_chinese(rbuf6, 50, 10, 110, UI_COLOR_DARK_GREY);
show_chinese(rbuf7, 50, 10, 130, UI_COLOR_DARK_GREY);
/*show_chinese(rbuf8, 50, 10, 150, UI_COLOR_DARK_GREY);
show_chinese(rbuf9, 50, 10, 170, UI_COLOR_DARK_GREY);
show_chinese(rbuf10, 40, 10, 190, UI_COLOR_DARK_GREY);*/
gui_BLT_double_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);//用来刷新屏幕
SetKeyHandler(goback, KEY_RSK, KEY_EVENT_UP);
}
}
return ret;
}
app_soc_notify_ind_struct *soc_notify;
void soc_socket_notify(void *inMsg)//注册的钩子函数,响应socket服务器端来的数据
{
kal_uint8 connect[] = {0x11};
//app_soc_notify_ind_struct *soc_notify;
soc_notify = (app_soc_notify_ind_struct *)inMsg;
switch(soc_notify->event_type)
{
case SOC_WRITE:
sock_send();
break;
case SOC_READ:
//dbg_print("begin receive \r\n");
sock_receive();
break;
case SOC_CONNECT:
//dbg_print("SOC_CONNECT ok \r\n");
sock_send();
break;
case SOC_CLOSE:
soc_close(ticket_socket_id);
break;
default:
break;
}
}
-
顶
- 0