winCE 下 evc 的 Socket 通信

winCE下evc的Socket通信的例子

socket在evc下的和vc下的差不多,找一个vc下的例子改改就可以了!
不过,在需要注意的是:在WinCE下的socket在做Server时,在接收方面有点问题,时WinCE本身的bug。修改的办法是将WinSock.h中将变量m_bConnectCalled由private改成public,然后在onaccept()之后,将m_bConnectCalled设成true。更具体的办法请去msdn的新闻组寻找。

int EpgCore::initialize()
{


       int iPort = DEFAULT_PORT;
       WSADATA wsd;
struct sockaddr_in server;
unsigned long ul =1;
struct hostent *host = NULL; 
tm.tv_sec = 5;
tm.tv_usec = 0;

if(WSAStartup(MAKEWORD(2,2),&wsd) != 0)
{
MessageBox(NULL,_T("initializing socke failed"),MB_OK,NULL);
return -1;
}
sClient = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);

if(sClient == SOCKET_ERROR)
{
MessageBox(NULL,_T("socket() failed"),MB_OK,NULL);
return -1;
}

server.sin_family = AF_INET; 
server.sin_port = htons(iPort);
server.sin_addr.s_addr = inet_addr("192.168.136.102");//ip address
      
if(server.sin_addr.s_addr == INADDR_NONE)
{
host = gethostbyname("192.168.136.102"); 
if(host == NULL)
                     return -1;
CopyMemory(&server.sin_addr,host->h_addr_list[0],host->h_length);
}

if(connect(sClient,(struct sockaddr*)&server,sizeof(server)) == SOCKET_ERROR)
{
MessageBox(NULL,_T("connect() failed"),MB_OK,NULL);
   return -1;
}
   //set noblock model
   if (ioctlsocket(sClient, FIONBIO, &ul) == SOCKET_ERROR)
   {
   MessageBox(NULL,_T("ioctlsocket() failed"),MB_OK,NULL);
             return -1;
   }
return 0;
}

void EpgCore::tick()
{
//    SetingSelect();
   RecvMessage();
   FileOperate(RecvMessage());
//    RecvMessage();
      
}


int EpgCore::SetingSelect()
{
       FD_ZERO(&fdread);    
FD_SET(sClient,&fdread);
select(0,&fdread,NULL,NULL,NULL);
return 0;
}

char *EpgCore::RecvMessage()
{
FD_ZERO(&fdread);    
FD_SET(sClient,&fdread);
select(0,&fdread,NULL,NULL,NULL);
if(FD_ISSET(sClient,&fdread))
   {       
                  
                               RecvRet = recv(sClient,(char *)szBuff,DEFAULT_BUFFER,0);
                   if(RecvRet == 0)
   return '\0';
                   else if (RecvRet == SOCKET_ERROR) 
return '\0'; 
                   szBuff[RecvRet] = '\0';
//转化为unicode
WCHAR wchar[4098];
MessageBox(NULL,CharToWChar(wchar,szBuff),MB_OK,NULL);
  
}
return szBuff;
}


int EpgCore::SendMessage()
{
ret = send(sClient,"GETLIST",sizeof("GETLIST"),0);
if(ret == 0)
return -1;
if(ret == SOCKET_ERROR)
return -1;
return 0;
}

CImgSock::CImgSock()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(1,1),&wsaData);
m_bConnect = false;
}

CImgSock::~CImgSock()
{
if(IsConnect())
DisConnect();
WSACleanup();
}

bool CImgSock::Connect(char *pstrIp,int nPort)
{
SOCKADDR_IN addr;
strcpy(m_szRemoteIp,pstrIp);
m_nRemotePort = nPort;
m_Sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(m_Sock == INVALID_SOCKET)
return false;
BOOL sopt = TRUE;
setsockopt(m_Sock, IPPROTO_TCP, TCP_NODELAY, (char *)&sopt, sizeof(BOOL));
setsockopt(m_Sock, SOL_SOCKET, SO_DONTLINGER, (char *)&sopt, sizeof(BOOL));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(m_szRemoteIp);
addr.sin_port = htons(m_nRemotePort);
if(connect(m_Sock,(PSOCKADDR)&addr,sizeof(addr)) != 0)
{
closesocket(m_Sock);
return false;
}
m_bConnect = true;
return true;
}

void CImgSock::DisConnect()
{
if(m_Sock != INVALID_SOCKET)
{
closesocket(m_Sock);
}
m_bConnect = false;
}

bool CImgSock::IsConnect()
{
return m_bConnect;
}

int CImgSock::Recv(char *buf,int len)
{
int index;
TIMEVAL tv;
fd_set fdread;
index = 0;
if(m_bConnect)

tv.tv_sec = 1;
tv.tv_usec =0; 
FD_ZERO(&fdread);
FD_SET(m_Sock,&fdread);
if(select(0,&fdread,NULL,NULL,&tv))
{
index = recv(m_Sock,buf,len,0);
if(index == SOCKET_ERROR)
index = 0;
}
}
return index;
}

int CImgSock::Send(char *buf,int len)
{
int index;
TIMEVAL tv;
fd_set fdwrite;
index = 0;
if(m_bConnect)
{
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&fdwrite);
FD_SET(m_Sock,&fdwrite);
if(select(0,NULL,&fdwrite,NULL,&tv))
{
index = send(m_Sock,buf,len,0);
if(index == SOCKET_ERROR)
index = 0;
}
}
return index;
}

原文地址:

http://www.hmitech.com/bbs/viewthread.php?tid=119

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值