设置定时器的回调函数没有调用,如何解决?
// SampClient.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "winsock2.h"
#include "stdio.h"
char Message[1024] ; // 发送短信
VOID CALLBACK SendTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );
WSADATA wsd;
WORD wVersion ;
SOCKET sClient;
struct sockaddr_in ServerAddr,ServerReplyAddr ;
int nRet,nAddrLen ;
int main(int argc, char* argv[])
{
wVersion = MAKEWORD(2,2);
nRet = WSAStartup(wVersion,&wsd);
if ( nRet != 0 )
{
printf("WSAStartup call error : code \n",WSAGetLastError());
return 1;
};
sClient = socket(AF_INET,SOCK_DGRAM,0);
if ( sClient == INVALID_SOCKET)
{
printf("socket() return invalid socket : error code \n",WSAGetLastError());
WSACleanup();
return 2 ;
};
ServerAddr.sin_family = AF_INET ;
ServerAddr.sin_port = htons(12345);
ServerAddr.sin_addr.S_un.S_addr = inet_addr("192.168.0.23") ;
UINT TimerID = SetTimer(NULL,1,1000,(TIMERPROC )SendTimerProc);
if ( TimerID == 0 )
printf("Create Timer Fail \n");
else
printf("create timer succeed : TimerId = %d !\n",TimerID);
while ( true )
{
char c ;
scanf("%c",&c);
};
if ( TimerID != 0 )
KillTimer(NULL,TimerID);
WSACleanup();
return 0;
}
VOID CALLBACK SendTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
printf("begin ...");
sprintf(Message,"wjd");
nAddrLen = sizeof(ServerAddr);
nRet = sendto(sClient,Message,1024,0,( struct sockaddr * ) & ServerAddr,nAddrLen);
if ( nRet == SOCKET_ERROR )
{
printf("call sendto error : code \n",WSAGetLastError());
return ;
}
else
printf("send %d byte message, all Message is %s \n",nRet,Message);
nRet = recvfrom(sClient,Message,1024,0,(struct sockaddr * ) & ServerReplyAddr,&nAddrLen);
if ( nRet == SOCKET_ERROR )
{
printf("recvfrom call error : code %d \n",WSAGetLastError());
return ;
}
else
printf("recvfrom message %s \n",Message);
}
程序运行后,并没有调用SendTimeProc,为什么呢?