|
楼主 |
发表于 2011-9-16 17:17:26
|
显示全部楼层
代码是在网上搜索的获取天气的:
unsigned char weather_URL[]={
"GET http://61.135.159.21/cgi-bin/weather?0755 HTTP/1.1\r\n"
"Host: 10.0.0.172:80\r\nUser-Agent: */*\r\nAccept:*/*\r\n"
"X-Online-Host: 61.135.159.21\r\nProxy-Connection: Keep-Alive\r\n\r\n"};
/*0755 深圳的区号,可以尝试用浏览器打开http://61.135.159.21/cgi-bin/weather?0755,会发生什么呢?*/
//*{"GET http://www.google.com/ig/api?hl=zh_cn&weather=shenzhen HTTP/1.1\r\nHost:www.google.com:80\r\nUser-Agent: */*\r\nAccept:*/*\r\nX-Online-Host:www.weather.com.cn\r\nProxy-Connection: Keep-Alive\r\n\r\n"};这是谷歌的接口,shenzhen是城市*/
unsigned char weather_ip[]={"61.135.159.21"};
static kal_uint8 g_weather_soc_id;
static sockaddr_struct g_weather_ip_addr;
#define SOCKET_BUF_SIZE 1500
void get_network_time_socket_send(void);
void get_network_time_socket_receive(void);
void get_network_time_socket_notify(void *msg_ptr);
void mmi_get_network_time(void)
{
kal_uint8 val;
kal_int8 soc_ret;
U8 emSocketAccountId = custom_get_csd_profile_num();
#ifdef WIN32
soc_init_win32();
#endif
// MOD_CH_NRT 是我的自定义的模块 ID,使用 MMI task 时改为 MOD_MMI
g_weather_soc_id = soc_create(PF_INET, SOCK_STREAM, 0, MOD_CH_NRT, emSocketAccountId);
if(g_weather_soc_id<0)
{
kal_printf("create socket, error!");
return;
}
val = 1;
if(soc_setsockopt(g_weather_soc_id, SOC_NBIO, &val, sizeof(val))<0)
{
kal_printf("set socket SOC_NBIO error!");
soc_close(g_weather_soc_id);
return;
}
val = SOC_READ|SOC_ASYNC|SOC_CLOSE|SOC_CONNECT;
if(soc_setsockopt(g_weather_soc_id, SOC_ASYNC, &val, sizeof(val))<0)
{
kal_printf("set socket SOC_ASYNC error!");
soc_close(g_weather_soc_id);
return;
}
memset(&g_weather_ip_addr, 0, sizeof(sockaddr_struct));
g_weather_ip_addr.addr[0] = 61;
g_weather_ip_addr.addr[1] = 135;
g_weather_ip_addr.addr[2] = 159;
g_weather_ip_addr.addr[3] = 21;
g_weather_ip_addr.addr_len = 4;
g_weather_ip_addr.port = 80;
g_weather_ip_addr.sock_type = SOCK_STREAM;
SetProtocolEventHandler(get_network_time_socket_notify, MSG_ID_APP_SOC_NOTIFY_IND);
soc_ret = soc_connect(g_weather_soc_id, &g_weather_ip_addr);
if(soc_ret>=0)
{
get_network_time_socket_send();
return;
}
else if(soc_ret == SOC_WOULDBLOCK)
{
kal_printf("connect socket, wait...\n");
kal_printf("MSG_ID_APP_SOC_NOTIFY_IND[%d]\n",MSG_ID_APP_SOC_NOTIFY_IND);
SetProtocolEventHandler(get_network_time_socket_notify, MSG_ID_APP_SOC_NOTIFY_IND);
return ;
}
}
void get_network_time_socket_send(void)
{
//unsigned char buf[200];
unsigned char wbuf[400];
kal_int32 ret,len;
/*soc_getsockaddr(g_weather_soc_id, 1, & g_weather_ip_addr);
sprintf((char *)buf, "IP:%d.%d.%d.%d,PORT:%d",
g_weather_ip_addr.addr[0], g_weather_ip_addr.addr[1], g_weather_ip_addr.addr[2],g_weather_ip_addr.addr[3], g_weather_ip_addr.port);
app_asc_str_to_ucs2_str((kal_int8*)wbuf, (kal_int8 *)buf);
kal_printf(wbuf);如果用另一台手机连接上这个IP,两者之间就可以发送数据,我用J2ME编一个程序放到手机上实现过,不知这里行不行?*/
len = strlen(weather_URL);
ret = soc_send(g_weather_soc_id, weather_URL, len, 0);
if(ret > 0)
{
get_network_time_socket_receive();
}
else
{
kal_printf("MSG_ID_APP_SOC_NOTIFY_IND[%d]\n",MSG_ID_APP_SOC_NOTIFY_IND);
SetProtocolEventHandler(get_network_time_socket_notify, MSG_ID_APP_SOC_NOTIFY_IND);
}
} |
|