httpsendrequest error 超时

csh20140320 2014-03-20 09:17:21
一个通信软件,A方用internetopeninternet ...internetreadfile一次一连,B方用sock模拟http包连接通信。同时A用internetwritefile长连接向B方发送文件,当长连接发文件时间长一点了,一次一连的通信会返回httpsendrequest error 超时,就连不上了,只有重启才行,求助!!!
一次一连的A段代码:
int SendData( )
{


HINTERNET hHttpIe;
HINTERNET hHttpHc;
HINTERNET h_tempSend;
DWORD cmdtype(0);
hHttpIe = InternetOpenA("Mozilla/4.0 (compatible; MSIE 6.0; ""Windows NT 5.0; .NET CLR 1.1.4322",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if(!hHttpIe)
{
cout<<"InternetOpen error = "<<GetLastError()<<endl;
return 0;
}
//
hHttpHc=InternetConnectA(hHttpIe,servname,m_uPort,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
if(!hHttpHc)
{
cout<<"InternetConnect error = "<<GetLastError()<<endl;
InternetCloseHandle(hHttpIe);
return 0;
}
h_tempSend = HttpOpenRequestA(hHttpHc,"POST",filename,NULL,NULL,NULL,INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_NO_CACHE_WRITE| INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD,NULL);
if(!h_tempSend)
{
cout<<"HttpOpenRequest error = "<<GetLastError()<<endl;
InternetCloseHandle(hHttpIe);
InternetCloseHandle(hHttpHc);

return -1;
}
//
char chData[8]={0};
int nLen(8);
((DWORD*)chData)[0]=num0;
((DWORD*)chData)[1]=EMPTY;
//
if(!HttpSendRequest(h_tempSend , NULL , 0 , (char *)chData, nLen ))
{
cout<<"HttpSendRequest error = "<<GetLastError()<<endl;
InternetCloseHandle(h_tempSend);
InternetCloseHandle(hHttpHc);
InternetCloseHandle(hHttpIe);
return -1;
}

//internetreadfile
int len=RecvFunc0(h_tempSend);

if ( len <= 0 )
{
InternetCloseHandle(h_tempSend);
InternetCloseHandle(hHttpHc);
InternetCloseHandle(hHttpIe);
return-1;
}
char *buf=new char[len];
memset(buf,0,len);
if ( RecvFunc1(h_tempSend,buf,len) <= 0 )
{
delete []buf;
buf=0;
InternetCloseHandle(h_tempSend);
InternetCloseHandle(hHttpHc);
InternetCloseHandle(hHttpIe);
return -1;
}
//
InternetCloseHandle(h_tempSend);
InternetCloseHandle(hHttpHc);
InternetCloseHandle(hHttpIe);

int m=GetOEMCP();
char cmidbuff[1000]={0};
DWORD lBytesRead(0);
//do
num0=((DWORD*)buf)[0];
cmdtype=((DWORD*)buf)[1];
//
switch (cmdtype)
{

case MMMM :
do;
break;
..........

default:
break;
}
//
delete []buf;
buf=0;
return 8;
}

长连接发文件的A段代码

DWORD WINAPI DownLoad(LPVOID lparameter)
{
cout<<"start download"<<endl;
DWORD dwRead(0);
int filelen(0),datalen(0),readlen(0),allreadywritelen(0),readytowritelen(0);
char sbuf[20]={0};
char filenameflag[200]={0};

//
FILE*fp;
if ( (fp=_wfopen(destpath,_T("rb")))==NULL )
{
return 1;
}
//
fseek(fp,0,SEEK_END);
filelen=ftell(fp);

fseek(fp,offset,SEEK_SET);
datalen=filelen-offset;
//
INTERNET_BUFFERS ib;
HINTERNET hInternet(NULL),hConnection(NULL),hRequest(NULL);
char * buf=new char[FILEMAXLEN];
do
{
CloseHandle(hRequest);
CloseHandle(hConnection);
CloseHandle(hInternet);

memset(buf,0,FILEMAXLEN);
readlen=fread(buf,1,FILEMAXLEN,fp);

memset(&ib, 0, sizeof(INTERNET_BUFFERS));
ib.dwStructSize = sizeof(INTERNET_BUFFERS);
ib.dwBufferTotal = readlen;
ib.dwBufferLength = 8192;
memset(filenameflag,0,200);
if (feof(fp))//last connect
{
strcat(filenameflag,"downlast");
}
else
{
strcat(filenameflag,"down");
}

hInternet = InternetOpenA("Mozilla/4.0 (compatible; MSIE 6.0; ""Windows NT 5.0; .NET CLR 1.1.4322",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if (!hInternet)
{
goto isenda;
}
hConnection = InternetConnectA(hInternet, servname, m_uPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (!hConnection)
{
goto isenda;
}
//1
hRequest = HttpOpenRequestA(hConnection,"POST",filenameflag,NULL, NULL,NULL, 0, INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT|INTERNET_FLAG_HYPERLINK);
//2
// hRequest = HttpOpenRequestA(hConnection,"POST",filenameflag,NULL,NULL,NULL,INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION|
// INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_RELOAD,NULL);
if (!hRequest)
{
goto isenda;
}
if (!HttpSendRequestEx(hRequest, &ib, 0, HSR_INITIATE, 0))
{
goto isenda;
}
//internetwritefile1
// if (InternetWriteFile(hRequest, buf, readlen, &dwRead)!=TRUE)
// {
// goto isenda;
// }
//internetwritefile2
allreadywritelen=0;
while(allreadywritelen<readlen)
{
if (!downstate)//end
{
downstate=TRUE;
goto isenda;
}
if (readlen-allreadywritelen>=8192)
{
readytowritelen=8192;
}
else
{
readytowritelen=readlen-allreadywritelen;
}

if (InternetWriteFile(hRequest, buf+allreadywritelen, readytowritelen, &dwRead)!=TRUE)
{
goto isenda;
}
allreadywritelen+=dwRead;
Sleep(200);

}
//wait
WaitForSingleObject(g_hEvent,INFINITE);
if (!downstate)//end
{
downstate=TRUE;
goto isenda;
}
} while (!feof(fp));

isenda:
fclose(fp);
delete[]buf;
CloseHandle(hRequest);
CloseHandle(hConnection);
CloseHandle(hInternet);
CloseHandle(g_hEvent);

return 0;
}


B端socket代码
		while(1)
{
SOCKADDR_IN addrClient;
len=sizeof(SOCKADDR);
ssss1=INVALID_SOCKET;
if ( ( ssss1=accept( sockSrv,(SOCKADDR *)&addrClient,&len ) ) != INVALID_SOCKET )
{
memset(ipoutside,0,sizeof(ipoutside));
strcpy(ipoutside,inet_ntoa(addrClient.sin_addr));
HANDLE hThread2;
hThread2=CreateThread(NULL,0,fRecvThread,LPVOID(ssss1),0,NULL);
CloseHandle(hThread2);
Sleep(5);
}
else
{
if (GetLastError()==10038)
{
cout<<"client not online"<<endl;
break;
}
}


}
fRecvThread()...........
...全文
148 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

10,612

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧