64,682
社区成员
发帖
与我相关
我的任务
分享
for(i = 0 ; i != dwCount ; ++i)
{
gets(sendbuf); //按下Ctrl+C的时候,程序应该是阻塞在这一条语句的
ret = sendto(s,sendbuf,dwLength,0,(sockaddr *)&recipent,sizeof(recipent));
printf("sendto message[%d]\n",++times); //但是这里的语句也被执行了
if(ret == SOCKET_ERROR)
{
printf("sendto() failed with error %d\n",WSAGetLastError());
break;
}
else if(ret == 0)
{
break;
}
}
/*********************************************************************
* FUNCTION: demoSetCtrlHandler(HANDLE hConOut) *
* *
* PURPOSE: demonstrate SetConsoleCtrlHandler by setting a ctrl+break *
* and ctrl+c handler. When the user hits either one of *
* these keys, a message is printed to the console *
* indicating the event. *
* *
* INPUT: the output handle to write to *
*********************************************************************/
void demoSetCtrlHandler(HANDLE hConOut)
{
BOOL bSuccess;
setConTitle(__FILE__);
hConsole = hConOut; /* set global console output handle for handler */
myPuts(hConOut, "Let's install a ctrl+c and ctrl+break handler for this\n"
"process. Hit ctrl+c and ctrl+break a few times to test\n"
"the handler. Hit enter to return...");
/* set handler for this process */
bSuccess = SetConsoleCtrlHandler(handler_routine, TRUE);
PERR(bSuccess, "SetConsoleCtrlHandler");
/* wait for user to hit enter */
while (myGetchar() != 0xd)
;
/* now let's generate some control events */
myPuts(hConOut, "Now we'll use GenerateConsoleCtrlEvent to generate a\n"
"ctrl+c and a ctrl+break event...\n");
bSuccess = GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
PERR(bSuccess, "GenerateConsoleCtrlEvent");
bSuccess = GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, 0);
PERR(bSuccess, "GenerateConsoleCtrlEvent");
Sleep(1000); /* give ctrl handle time to output messages */
myPuts(hConOut, "\nNow choose 'Close' then 'Cancel' from the system\n"
"menu of this console and note that we receive a\n"
"CTRL_CLOSE_EVENT...\n");
myPuts(hConOut, "\nHit enter to continue...");
myGetchar();
/* remove our handler from the list of handlers */
bSuccess = SetConsoleCtrlHandler(handler_routine, FALSE);
PERR(bSuccess, "SetConsoleCtrlHandler");
return;
}
/*********************************************************************
* FUNCTION: demoSetCtrlHandler(HANDLE hConOut) *
* *
* PURPOSE: demonstrate SetConsoleCtrlHandler by setting a ctrl+break *
* and ctrl+c handler. When the user hits either one of *
* these keys, a message is printed to the console *
* indicating the event. *
* *
* INPUT: the output handle to write to *
*********************************************************************/
void demoSetCtrlHandler(HANDLE hConOut)
{
BOOL bSuccess;
setConTitle(__FILE__);
hConsole = hConOut; /* set global console output handle for handler */
myPuts(hConOut, "Let's install a ctrl+c and ctrl+break handler for this\n"
"process. Hit ctrl+c and ctrl+break a few times to test\n"
"the handler. Hit enter to return...");
/* set handler for this process */
bSuccess = SetConsoleCtrlHandler(handler_routine, TRUE);
PERR(bSuccess, "SetConsoleCtrlHandler");
/* wait for user to hit enter */
while (myGetchar() != 0xd)
;
/* now let's generate some control events */
myPuts(hConOut, "Now we'll use GenerateConsoleCtrlEvent to generate a\n"
"ctrl+c and a ctrl+break event...\n");
bSuccess = GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
PERR(bSuccess, "GenerateConsoleCtrlEvent");
bSuccess = GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, 0);
PERR(bSuccess, "GenerateConsoleCtrlEvent");
Sleep(1000); /* give ctrl handle time to output messages */
myPuts(hConOut, "\nNow choose 'Close' then 'Cancel' from the system\n"
"menu of this console and note that we receive a\n"
"CTRL_CLOSE_EVENT...\n");
myPuts(hConOut, "\nHit enter to continue...");
myGetchar();
/* remove our handler from the list of handlers */
bSuccess = SetConsoleCtrlHandler(handler_routine, FALSE);
PERR(bSuccess, "SetConsoleCtrlHandler");
return;
}