hellosockc.c
来自「手机开发环境BREW实例」· C语言 代码 · 共 335 行
C
335 行
/*===========================================================================
FILE: hellosockc.c
===========================================================================*/
/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "AEEModGen.h" // Module interface definitions
#include "AEEAppGen.h" // Applet interface definitions
#include "AEEShell.h" // Shell interface definitions
#include "AEEFile.h" // File interface definitions
#include "AEENet.h" // Socket interface definitions
#include "AEEStdLib.h" // AEE Stb Lib Services
#include "hellosockc.bid"
typedef boolean BOOL;
typedef byte BYTE;
typedef uint32 DWORD;
#define SERVER_IP ("192.168.0.104")
#define SERVER_PORT (0x3014)//5168十六进制的网络字节序
#define SHOW_ERROR_MEG(pvParam, content)\
IDISPLAY_DrawText(((CNetElement*)pvParam)->a.m_pIDisplay,\
AEE_FONT_BOLD,\
L##content,\
-1,\
0,\
0,\
NULL,\
IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);\
IDISPLAY_Update(((CNetElement*)pvParam)->a.m_pIDisplay)
typedef struct
{
AEEApplet a; //强制为IApplet接口
INetMgr * pINetMgr; //INetMgr接口实例指针
ISocket * pISocket; //Socket接口实例指针
AEECallback psDNSCallBack; //回调函数的相关信息
AEEDNSResult sDNSResult; //IP地址的相关信息
BYTE strSBuff[50]; //发送缓冲区
BYTE btSendedLen; //发送的字节数
DWORD dwRecvedLen; //接收的字节数
BYTE strRBuff[50]; //接收缓冲区
}CNetElement;
boolean InitAppData(CNetElement* pNe)
{
//建立INetMgr接口实例
if( ISHELL_CreateInstance(pNe->a.m_pIShell, AEECLSID_NET,
(void**)&(pNe->pINetMgr)) != SUCCESS)
{
return FALSE;
}
//打开ISocket接口
if( (pNe->pISocket = INETMGR_OpenSocket(pNe->pINetMgr, AEE_SOCK_STREAM)) == NULL )
{
return FALSE;
}
return TRUE;
}
/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static boolean hellosockc_HandleEvent(IApplet * pi, AEEEvent eCode,
uint16 wParam, uint32 dwParam);
static void hellosockc_FreeAppData(IApplet* po)
{
CNetElement * pNe = (CNetElement *)po;
if(pNe->pISocket)
{
ISOCKET_Release(pNe->pISocket);
}
if(pNe->pINetMgr)
{
INETMGR_Release(pNe->pINetMgr);
}
if(pNe->psDNSCallBack.pfnCancel != NULL)
CALLBACK_Cancel(&pNe->psDNSCallBack);
}
void OnSockWrite(CNetElement* pNe)
{
int iRet;
//发送数据
iRet = ISOCKET_Write(pNe->pISocket, pNe->strSBuff + pNe->btSendedLen,
(uint16)(STRLEN(pNe->strSBuff) - pNe->btSendedLen));
//无可用的socket
if (iRet == AEE_NET_ERROR)
{
SHOW_ERROR_MEG(pNe, "Send error!");
return;
}
//发送失败,开始回调
if(iRet == AEE_NET_WOULDBLOCK)
{
ISOCKET_Writeable(pNe->pISocket, (PFNNOTIFY)OnSockWrite, pNe);
return;
}
//记录发送位子
pNe->btSendedLen += iRet;
//发送的数据包太长
if(pNe->btSendedLen > STRLEN(pNe->strSBuff))
{
SHOW_ERROR_MEG(pNe, "Send pack error!");
return;
}
//发送成功
if(pNe->btSendedLen == STRLEN(pNe->strSBuff))
{
;
}
else
{//还未发完这个指令,继续发
ISOCKET_Writeable(pNe->pISocket, (PFNNOTIFY)OnSockWrite, pNe);
}
}
void OnSockRead(CNetElement* pNe)
{
AECHAR wChar[50];
int iRet;
//每次读数据时都要清空缓冲区。
MEMSET(wChar,0,sizeof(wChar));
MEMSET((char *)(pNe->strRBuff),0,sizeof(pNe->strRBuff));
//接收数据
iRet = ISOCKET_Read(pNe->pISocket, ((BYTE*)(pNe->strRBuff)) + pNe->dwRecvedLen, sizeof(pNe->strRBuff) - pNe->dwRecvedLen);
//无可用的Socket.
if(iRet == AEE_NET_ERROR)
{
SHOW_ERROR_MEG(pNe, "Recv error!");
return;
}
//接收失败,开始回调。
if(iRet == AEE_NET_WOULDBLOCK)
{
ISOCKET_Readable(pNe->pISocket, (PFNNOTIFY)OnSockRead, pNe);
return;
}
//记录接受缓冲区已填入的的位子
pNe->dwRecvedLen += iRet;
//收到的数据包太长?
if(pNe->dwRecvedLen > STRLEN(pNe->strRBuff))
{
SHOW_ERROR_MEG(pNe, "Recv pack error!");
return;
}
//接收成功
if(pNe->dwRecvedLen == STRLEN(pNe->strRBuff))
{
pNe->dwRecvedLen = 0;
pNe->btSendedLen = 0;
}
//显示数据
STRTOWSTR((char *)pNe->strRBuff,wChar,sizeof(wChar));
IDISPLAY_ClearScreen(pNe->a.m_pIDisplay);
IDISPLAY_DrawText(pNe->a.m_pIDisplay,\
AEE_FONT_BOLD,\
wChar,\
-1,\
0,\
0,\
NULL,\
IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);\
IDISPLAY_Update(pNe->a.m_pIDisplay);
//回调此函数,继续接收。
ISOCKET_Readable(pNe->pISocket, (PFNNOTIFY)OnSockRead, pNe);
//发送数据。
OnSockWrite(pNe);
}
void OnConnect(void* pUser, int nError)
{
//连接到服务器端失败
if( nError != AEE_NET_SUCCESS )
{
SHOW_ERROR_MEG(pUser, "Connect error!");
return;
}
((CNetElement*)pUser)->dwRecvedLen = 0;
OnSockRead(pUser);
}
void OnHostName(void *pvParam)
{
CNetElement * pNe = (CNetElement*) pvParam;
//判断是否获得服务器的IP地址。
if( (pNe->sDNSResult.nResult <= 0) || (pNe->sDNSResult.nResult > AEEDNSMAXADDRS) )
{
SHOW_ERROR_MEG(pNe, "DNS error!");
return;
}
//连接服务器。
if( (ISOCKET_Connect(pNe->pISocket, pNe->sDNSResult.addrs[0], SERVER_PORT, OnConnect, pvParam)) != AEE_NET_SUCCESS )
{
SHOW_ERROR_MEG(pNe, "Start connect error!");
return;
}
}
/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */
/*===========================================================================
FUNCTION: AEEClsCreateInstance
DESCRIPTION
This function is invoked while the app is being loaded. All Modules must provide this
function. Ensure to retain the same name and parameters for this function.
In here, the module must verify the ClassID and then invoke the AEEApplet_New() function
that has been provided in AEEAppGen.c.
After invoking AEEApplet_New(), this function can do app specific initialization. In this
example, a generic structure is provided so that app developers need not change app specific
initialization section every time except for a call to IDisplay_InitAppData().
This is done as follows: InitAppData() is called to initialize AppletData
instance. It is app developers responsibility to fill-in app data initialization
code of InitAppData(). App developer is also responsible to release memory
allocated for data contained in AppletData -- this can be done in
IDisplay_FreeAppData().
PROTOTYPE:
int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
PARAMETERS:
clsID: [in]: Specifies the ClassID of the applet which is being loaded
pIShell: [in]: Contains pointer to the IShell object.
pIModule: pin]: Contains pointer to the IModule object to the current module to which
this app belongs
ppObj: [out]: On return, *ppObj must point to a valid IApplet structure. Allocation
of memory for this structure and initializing the base data members is done by AEEApplet_New().
DEPENDENCIES
none
RETURN VALUE
AEE_SUCCESS: If the app needs to be loaded and if AEEApplet_New() invocation was
successful
EFAILED: If the app does not need to be loaded or if errors occurred in
AEEApplet_New(). If this function returns FALSE, the app will not be loaded.
SIDE EFFECTS
none
===========================================================================*/
int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
{
*ppObj = NULL;
if(ClsId == AEECLSID_HELLOSOCKC){
if(AEEApplet_New(sizeof(CNetElement), ClsId, pIShell,po,(IApplet**)ppObj,
(AEEHANDLER)hellosockc_HandleEvent,(PFNFREEAPPDATA)hellosockc_FreeAppData)
== TRUE)
{
// Add your code here .....
if(InitAppData((CNetElement*)*ppObj))
{
return AEE_SUCCESS;
}
return (AEE_SUCCESS);
}
}
return (EFAILED);
}
/*===========================================================================
FUNCTION hellosockc_HandleEvent
DESCRIPTION
This is the EventHandler for this app. All events to this app are handled in this
function. All APPs must supply an Event Handler.
PROTOTYPE:
boolean hellosockc_HandleEvent(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)
PARAMETERS:
pi: Pointer to the AEEApplet structure. This structure contains information specific
to this applet. It was initialized during the AEEClsCreateInstance() function.
ecode: Specifies the Event sent to this applet
wParam, dwParam: Event specific data.
DEPENDENCIES
none
RETURN VALUE
TRUE: If the app has processed the event
FALSE: If the app did not process the event
SIDE EFFECTS
none
===========================================================================*/
static boolean hellosockc_HandleEvent(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
char szIP[] = SERVER_IP;
CNetElement * pAD = (CNetElement *)pi;
switch (eCode)
{
case EVT_APP_START:
((CNetElement*)pi)->btSendedLen = 0;
((CNetElement*)pi)->dwRecvedLen = 0;
//注册与建立回调
CALLBACK_Init(&(((CNetElement*)pi)->psDNSCallBack), OnHostName, pi);
INETMGR_GetHostByName(((CNetElement*)pi)->pINetMgr, &(pAD->sDNSResult), szIP, &(pAD->psDNSCallBack));
//指定发送的内容。
SPRINTF((char *)pAD->strSBuff,"%s","SR");
// Add your code here .....
return(TRUE);
case EVT_APP_STOP:
// Add your code here .....
return TRUE;
default:
break;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?