#include"Soc_api.h"
#include"String.h"
#include"Med_utility.h"
#include"Soc_consts.h"
#include"Kal_trace.h"
#include"App2soc_struct.h"
#include"ProtocolEvents.h"
//#defineURL"GET/lenovo/data/mobile/test_zlib/contents.metaHTTP/1.1/nX-ONLINE-HOST:221.223.53.34:80/n/r/n"
#defineURL"GET/index.htmlHTTP/1.1/nHOST:10.0.0.172:80/n/r/n"
#defineBUF_SIZE2*1024
#defineMAX_RCV_BUF_SIZE1000
kal_uint8*buf;
kal_int8socket_id=-1;
sockaddr_structaddress;
kal_int8length=0;
voidsocket_send(void);
voidsocket_receive(void);
voidsocket_close(void);
//voidsoc_close_nwk_account(kal_uint16mod_id);
voidsocket_notify(void*message);
/*========================================================*/
voidTbmPly_enter_app(void)
{
kal_uint8val=KAL_TRUE;
kal_int8ret;
buf=med_alloc_ext_mem(BUF_SIZE);
if(buf==NULL)
{
kal_print("bufmallocerror!");
return;
}
socket_id=soc_create(SOC_PF_INET,SOC_SOCK_STREAM,0,MOD_MED,14);
if(socket_id<0)
{
kal_print("socketcreateerroe!/n");
med_free_ext_mem((void**)&buf);
return;
}
if(soc_setsockopt(socket_id,SOC_NBIO,&val,sizeof(val))<0)
{
kal_print("setsocketnonblockmodeerror!");
med_free_ext_mem((void**)&buf);
return;
}
val=SOC_READ|SOC_WRITE|SOC_CLOSE|SOC_CONNECT;
if(soc_setsockopt(socket_id,SOC_ASYNC,&val,sizeof(val))<0)
{
kal_print("setsocketasychronizemodeerror!");
med_free_ext_mem((void**)&buf);
return;
}
kal_print("socketcreateok!/n");
address.addr[0]=172;
address.addr[1]=0;
address.addr[2]=0;
address.addr[3]=10;
address.addr_len=4;
address.port=80;
address.sock_type=SOC_SOCK_STREAM;
kal_prompt_trace(MOD_WAP,"startconnect:%d.%d.%d.%dport:%d/n",
address.addr[0],address.addr[1],address.addr[2],
address.addr[3],address.port);
ret=soc_connect(socket_id,(sockaddr_struct*)&address);
kal_prompt_trace(MOD_WAP,"connectresult:%d",ret);
if(ret==SOC_SUCCESS)
{
kal_prompt_trace(MOD_WAP,"connectserversuccess!");
socket_send();
}
elseif(ret==SOC_WOULDBLOCK)
{
SetProtocolEventHandler(socket_notify,MSG_ID_APP_SOC_NOTIFY_IND);
return;
}
else
{
if(ret==SOC_ERROR)
{
kal_prompt_trace(MOD_WAP,"connecthostfail!");
socket_close();
return;
}
return;
}
return;
}
voidsocket_send(void)
{
kal_int32result;
kal_print("startsend!/n");
result=soc_send(socket_id,URL,strlen(URL),0);
kal_prompt_trace(MOD_WAP,"sendresult:%d/n",result);
if(result>0)
{
socket_receive();
return;
}
else
{
if(result==SOC_WOULDBLOCK)
{
SetProtocolEventHandler(socket_notify,MSG_ID_APP_SOC_NOTIFY_IND);
return;
}
else
{
if(result==SOC_ERROR)
{
socket_close();
return;
}
else
{
socket_close();
return;
}
}
}
}
voidsocket_receive(void)
{
kal_int32result;
kal_uint8rbuf[MAX_RCV_BUF_SIZE];
kal_print("startreceive!/n");
do{
result=soc_recv(socket_id,rbuf,MAX_RCV_BUF_SIZE,0);
kal_prompt_trace(MOD_WAP,"receiveresult:%d/n",result);
if(result>0)
{
kal_prompt_trace(MOD_WAP,"%s",rbuf);
memcpy((kal_uint8*)(buf length),rbuf,result);
length =result;
memset(rbuf,0,MAX_RCV_BUF_SIZE);
}
}while(result>0);
if(result==0)
{
kal_prompt_trace(MOD_WAP,"receiveover!");
SetProtocolEventHandler(socket_notify,MSG_ID_APP_SOC_NOTIFY_IND);
return;
}
if(result==SOC_WOULDBLOCK)
{
SetProtocolEventHandler(socket_notify,MSG_ID_APP_SOC_NOTIFY_IND);
return;
}
else
{
socket_close();
}
return;
}
voidsocket_close(void)
{
kal_print("startclose!/n");
med_free_ext_mem((void*)&buf);
soc_close(socket_id);
//soc_close_nwk_account(MOD_WAP);
kal_print("closeok!/n");
return;
}
/*
voidsoc_close_nwk_account(kal_uint16mod_id)
{
return;
}
*/
voidsocket_notify(void*message)
{
app_soc_notify_ind_struct*soc_notify=(app_soc_notify_ind_struct*)message;
kal_prompt_trace(MOD_WAP,"startsocketnotify!/n");
if(soc_notify->socket_id!=socket_id)
{
kal_print("nosocket!/n");
return;
}
kal_prompt_trace(MOD_WAP,"eventtype:%d,result:%d/n",soc_notify->event_type,soc_notify->result);
switch(soc_notify->event_type)
{
caseSOC_CONNECT:
{
if(soc_notify->result==KAL_TRUE)
{
kal_print("socketconnectok!/n");
socket_send();
}
else
socket_close();
break;
}
caseSOC_WRITE:
{
socket_send();
break;
}
caseSOC_READ:
{
socket_receive();
break;
}
caseSOC_CLOSE:
{
if(socket_id>=0)
socket_close();
break;
}
default:
{
socket_close();
break;
}
}
}
www.guaiguaier.com
贴子来源飞诺网(http://bbs.firnow.com) 详细出处参考:http://bbs.firnow.com/dview67t7429.html