📄 cms_wap2udp.c
字号:
/******************************************************************************
C M O D U L E F I L E
(c) Copyright MobileSoft Technology (NanJing) Co., LTD. 2001-2002
ALL RIGHTS RESERVED
*******************************************************************************
Project Name: WAP STACK Ver1.20
Written By :
File Name :
Last Modify :
******************************************************************************/
#include "Cms_WTP.h"
#include "Cms_WAPMain.h"
#include "windows.h"
#include <winsock.h>
SOCKET sock;
struct sockaddr_in client,remote;
unsigned char redirect_host[8] = {0,0,0,0,0,0,0,0};
unsigned short redirect_port = 0;
extern WTPMachine* WTPGlobalMachine;
CMS_U64 GetLongFromBIt(P_CMS_U8 szbit, CMS_S32 nlen);
CMS_VOID Cms_UDPInit(P_CMS_U8 host, CMS_U16 port);
CMS_VOID Cms_UDPSendData(P_CMS_U8 data, CMS_S32 len);
CMS_VOID Cms_UDPRecvData(P_CMS_U8*data, P_CMS_S32 len);
CMS_VOID Cms_UDPDone(CMS_VOID);
void mms_StartTimer(unsigned long TimerID,long v_MiliSecond);
void mms_StopTimer (unsigned long TimerID);
extern void timer_thread();
CMS_VOID Cms_ReConnectGateway(P_CMS_U8 ipdata);
extern CMS_S32 Cms_ConnectGateway(CMS_VOID);
CMS_U64 GetLongFromBIt(P_CMS_U8 szbit, CMS_S32 nlen)
{
CMS_U64 nlong=0;
CMS_S32 ns;
for(ns=0;ns<nlen;ns++)
{
if(nlong>0)
nlong=nlong<<8;
nlong+=*(szbit+ns);
}
return nlong;
}
CMS_VOID Cms_UDPInit(P_CMS_U8 host, CMS_U16 port)
{
int r=0;
struct hostent *hp=0;
WSADATA wsaData;
//char remoteHost[11] = "10.0.0.172";
int a1, a2, a3, a4;
char host_name[256];
/* initialize winsock */
WSAStartup(0x0101, &wsaData);
sock = socket(AF_INET,SOCK_DGRAM,0);
if (sock == INVALID_SOCKET)
{
WSACleanup();
return ;
}
/* clear out client struct */
memset((void *)&client, '\0', sizeof(struct sockaddr_in));
/* set client */
client.sin_family = AF_INET;
client.sin_port = htons(0);
/* Get host name of this computer */
gethostname(host_name, sizeof(host_name));
hp = gethostbyname(host_name);
/* Assign the address */
client.sin_addr.S_un.S_un_b.s_b1 = hp->h_addr_list[0][0];
client.sin_addr.S_un.S_un_b.s_b2 = hp->h_addr_list[0][1];
client.sin_addr.S_un.S_un_b.s_b3 = hp->h_addr_list[0][2];
client.sin_addr.S_un.S_un_b.s_b4 = hp->h_addr_list[0][3];
/* clear out remote struct */
memset((void *)&remote, '\0', sizeof(struct sockaddr_in));
/* set remote */
remote.sin_family = AF_INET;
remote.sin_port = htons(port);
/* set remote address */
sscanf_s((char*)host, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
remote.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
remote.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
remote.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
remote.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;
bind(sock, (struct sockaddr *)&client, sizeof(struct sockaddr_in));
return;
}
CMS_VOID Cms_UDPSendData(P_CMS_U8 data, CMS_S32 len)
{
CMS_S32 r=0;
r=sendto(sock,(char*)data,len,0,(struct sockaddr *)&remote,sizeof(struct sockaddr_in));
if (r != len)
{
return;
}
}
CMS_VOID Cms_UDPRecvData(P_CMS_U8 data, P_CMS_S32 len)
{
CMS_S8 buf[2048];
CMS_S32 l=0;
CMS_S32 r=0;
if (!data)
{
return;
}
*len=0;
memset(buf,0,2048);
l=sizeof(struct sockaddr_in);
r=recvfrom(sock,(char *)buf,sizeof(buf),0,(struct sockaddr *)&remote,&l);
if(r<=0)
{
return;
}
memcpy(data,buf,r);
*len=r;
}
CMS_VOID Cms_UDPDone(CMS_VOID)
{
closesocket(sock);
WSACleanup();
}
CMS_VOID Cms_ReConnectGateway(P_CMS_U8 ipdata)
{
CMS_S32 iplen;
iplen=(*ipdata) & 0x3F;
memcpy(redirect_host,ipdata+4,4);
redirect_port = (CMS_U16)GetLongFromBIt((P_CMS_U8)(ipdata+2),2);
Cms_ConnectGateway();
}
/* windows 平台多媒体时钟,用在控制台程序中 */
VOID CALLBACK TimeProc( UINT uID, UINT uMsg, DWORD dwUser,DWORD dw1, DWORD dw2)
{
timer_thread();
}
void mms_StartTimer(unsigned long TimerID,long v_MiliSecond)
{
MMRESULT nIDTimerEvent = timeSetEvent( 1000, 0, TimeProc, 0, (UINT)TIME_PERIODIC);
WTPGlobalMachine->timer_id = nIDTimerEvent;
}
void mms_StopTimer (unsigned long TimerID)
{
timeKillEvent(WTPGlobalMachine->timer_id);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -