非阻塞TCP套接字和发送后立即刷新? [英] Non-blocking TCP socket and flushing right after send?
问题描述
我为我的应用程序使用Windows套接字(winsock2.h)。由于阻塞套接字不允许我控制连接超时,我使用非阻塞的。发送命令后我使用关闭命令刷新(我不得不)。我的超时是50毫秒,我想知道的是,如果要发送的数据是这么大,有发送只有一部分数据或根本不发送的风险?先感谢...
I am using Windows socket for my application(winsock2.h). Since the blocking socket doesn't let me control connection timeout, I am using non-blocking one. Right after send command I am using shutdown command to flush(I have to). My timeout is 50ms and the thing I want to know is if the data to be sent is so big, is there a risk of sending only a portion of data or sending nothing at all? Thanks in advance...
hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
u_long iMode=1;
ioctlsocket(hSocket,FIONBIO,&iMode);
connect(hSocket, (sockaddr*)(&sockAddr),sockAddrSize);
send(hSocket, sendbuf, sendlen, 0);
shutdown(hSocket, SD_BOTH);
Sleep(50);
closesocket(hSocket);
推荐答案
感谢EJP和Martin,第二个线程检查。另外在代码我张贴在我的问题,我添加了counter = 0;线后发送线和删除关机。它的工作就像我想要的。它从不等待超过50ms :)真的很感谢
Thanks to EJP and Martin, now I have created a second thread to check. Also in the code I posted in my question, I added "counter=0;" line after the "send" line and removed shutdown. It works just as I wanted now. It never waits more than 50ms :) Really big thanks
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
while(1)
{
counter++;
if (counter > 49)
{
closesocket(hSocket);
counter = 0;
printf("\rtimeout");
}
Sleep(1);
}
return 0;
}
这篇关于非阻塞TCP套接字和发送后立即刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!