请参见我的上一个帖子http://community.csdn.net/Expert/topic/3839/3839571.xml?temp=.1526148,我测试了一下,的确可以了,但今天一测试,发现连接保持时间只有10秒,有没有办法让连接保持住,加了Keep-Alive也不行,我的请求类似如下
POST /Request.htm HTTP/1.1
Host: 127.0.0.1
Accept: */*
Connection: keep-alive
Content-Type: multipart/form-data
Content-Length: 100
100字节数据
服务器响应类似这样是
HTTP/1.1 200 OK
Date: Tue, 21 May 2002 12:34:56 GMT
Server: Microsoft-IIS/6.0
Connection: keep-alive
Last-Modified: Fri, 12 Nov 2004 02:56:20 GMT
Content-Type: application/zip
Content-Length: 100
100字节数据
POST /Request.htm HTTP/1.1
Host: 127.0.0.1
Accept: */*
Connection: keep-alive
Content-Type: multipart/form-data
Content-Length: 100
100字节数据
服务器响应类似这样是
HTTP/1.1 200 OK
Date: Tue, 21 May 2002 12:34:56 GMT
Server: Microsoft-IIS/6.0
Connection: keep-alive
Last-Modified: Fri, 12 Nov 2004 02:56:20 GMT
Content-Type: application/zip
Content-Length: 100
100字节数据
解决方案 »
- 如何查看windows有没有注册某个COM组件
- 向文件(TXT或CSV)写入字符串,怎样可以把新插入的放最前面,把原来的往后移?
- LoadIcon(hInstance,"ziyuanmingcheng");装载不成功?咋解决啊!
- string类型的字符串,能计算宽字符串的长度吗? string stirng_Test("我爱你"),长度老是6,我要3呀!
- 有谁用GDI+作过图形实时显示方面的系统。来讨论一下GDI+和GDI图形处理效率的问题。
- vc5.0环境,一下语句编译通不过,提示CToolbatCtrl没有SetImageList函数。可是在vc6.0下好像可以,请问如何在vc5.0下也可以呀?
- 利用VFW实现bmp转avi!!!
- 关于DELPHI的问题,我很急,请各位大虾帮帮我!谢谢!!!
- 好消息:病毒组织29a的病毒杂志第6期出来了,快去下载!
- 如何用Vc安全地终止进程?最好有源代码。
- to yintongshun(左岸思雨) ,SDI 中 CSplitterWnd 2 个 CFormView ,怎么把分隔的 2边(或者1边)变成平的,不要凹下去的那种?
- date and time picker 控件
哈哈。
在Server有Connect请求时,开辟线程StartClient。
代码如下:
void StartClient(void* pParam)
{
int iResult;
char strBuffer[20000];
CString tmpStr,SecondStr;
long pos1,pos2;
bool FindHost=false,FindBoundary = false,RecFinished=false;//,FindConnection=false;
// bool recFirst=true;
CString Host,boundary;
int Port=80; FD_SET localset,acceptset;;
timeval tv; struct sockaddr_in ClientAddr;
struct sockaddr_in addr;
SOCKET localsocket;
SOCKET ClientSocket = SOCKET_ERROR;
unsigned long ul = 0;
int timedout = 30000;
CString ClientIP;
int len;
len = sizeof(struct sockaddr_in); ClientAddr.sin_family=AF_INET;
ClientSocket=accept(Server,(sockaddr*)&ClientAddr,&len); //注意,Server是全局变量
if(ClientSocket==SOCKET_ERROR||paused)
{
goto End;
} localsocket = socket(AF_INET,SOCK_STREAM,0);
if(localsocket==INVALID_SOCKET||paused)
{
goto End;
} //Set non-blocking
if(ioctlsocket(localsocket, FIONBIO, (unsigned long*)&ul)==SOCKET_ERROR||paused)
{
goto End;
} //Check Client IP
ClientIP = (CString)inet_ntoa(ClientAddr.sin_addr);
if(ClientIP.Find("192.168.")==-1&&ClientIP!="127.0.0.1"||paused)
{
goto End;
} //Receive Parameters
tmpStr = "";
do
{
strset(strBuffer,'\0');
//select检测rec超时(30s)
FD_ZERO(&acceptset);
FD_SET(ClientSocket,&acceptset);
tv.tv_sec=60;
tv.tv_usec=0;
if (select(0,&acceptset,0,0,&tv)<=0||paused)
{
goto End;
}
iResult=recv(ClientSocket,strBuffer,sizeof(strBuffer),0);
CFile myfile;
try
{
myfile.Open("log.ini",CFile::modeWrite|CFile::modeCreate|CFile::shareDenyNone);
myfile.SeekToEnd();
myfile.Write(strBuffer,iResult);
myfile.Close();
}
catch(...)
{
myfile.Close();
}
if(iResult==SOCKET_ERROR||paused)
{
goto End;
} if(SecondStr.GetLength()<=40)
SecondStr=SecondStr+strBuffer;
else
SecondStr=SecondStr.Right(SecondStr.GetLength()-(int)strlen(strBuffer))+strBuffer; //if(!FindHost&&!FindBoundary&&!FindConnection)
if(!FindHost||!FindBoundary)//||!FindConnection)
{
tmpStr = tmpStr+strBuffer;
if(tmpStr.Left(1)=="G") FindBoundary = true; if(!FindHost)
{
pos1 = tmpStr.Find("Host:")+6;
if(pos1>=6)
{
pos2=tmpStr.Find("\r\n",pos1);
if(pos2>0)
{
FindHost = true;
Host = tmpStr.Mid(pos1,pos2-pos1).Trim().MakeUpper();
if(Host.Find(":")>=0)
{
Port = atoi(Host.Right(Host.GetLength()-Host.Find(":")-1).Trim());
if(Port==0) Port = 80;
Host = Host.Left(Host.Find(":")).Trim().MakeUpper();
if(Host=="")
goto End;
}
if(!GeneralHost.IsEmpty())
{
Host = GeneralHost;
Port = ListenPort;
}
//Send the tmpStr to Host:Port
addr.sin_family=AF_INET;
addr.sin_port=htons(Port);
//Get Host by Name,or by IP
if(Host.FindOneOf("ABCDEFGHIJKLMNOPQRSTUVWXYZ")>=0)
{
hostent *host;
host = gethostbyname(Host);
if(host==NULL||paused)
{
goto End;
}
memcpy(&(addr.sin_addr),host->h_addr,host->h_length);
}
else
{
addr.sin_addr.s_addr=inet_addr(Host);
}
//设置连接超时,10 Seconds
if((::setsockopt(localsocket,SOL_SOCKET,SO_SNDTIMEO,(char *)&timedout,sizeof(timedout))==SOCKET_ERROR)||paused)
{
goto End;
}
}
}
else //not found "Host:"
{
if(tmpStr.GetLength()>40000||paused)
goto End;
}
}
if(!FindBoundary)
{
pos1 = tmpStr.Find("boundary=")+9;
if(pos1>=9)
{
pos2=tmpStr.Find("\r\n",pos1);
if(pos2>0)
{
FindBoundary = true;
boundary = tmpStr.Mid(pos1,pos2-pos1).Trim();
}
else //not found "\r\n"
{
if(tmpStr.GetLength()>40000||paused)
goto End;
}
}
else //not found "boundary="
{
if(tmpStr.GetLength()>40000||paused)
goto End;
}
}
/*
if(!FindConnection)
{
pos1 = tmpStr.Find("Proxy-Connection: Keep-Alive\r\n");
if(pos1>=0)
{
FindConnection = true;
tmpStr.Replace("Proxy-Connection: Keep-Alive","Connection: close");
}
else //not found "Proxy-Connection"
{
if(tmpStr.GetLength()>40000||paused)
goto End;
}
}
*/
if(FindHost&&FindBoundary)//&&FindConnection)
{
if(connect(localsocket,(struct sockaddr*)&addr,sizeof(addr))!=0||paused)
{
goto End;
}
iResult=send(localsocket,tmpStr,iResult,0);
if(iResult==SOCKET_ERROR||paused)
{
goto End;
}
}
}
else //FindHost = true FindBoundary = true FindConnection = true
{
FD_ZERO(&localset);
FD_SET(localsocket,&localset);
tv.tv_sec=60;
tv.tv_usec=0;
if(select(0,NULL,&localset,NULL,&tv)<=0||paused)
{
goto End;
}
iResult=send(localsocket,strBuffer,iResult,0);
if(iResult==SOCKET_ERROR||paused)
{
goto End;
}
} if((tmpStr.Left(1)=="G")&&(SecondStr.Right(4)=="\r\n\r\n"))
RecFinished = true;
if((tmpStr.Left(1)=="P")&&(SecondStr.Find("--"+boundary+"--\r\n")!=-1))
RecFinished = true;
}
while(iResult!=0&&iResult!=SOCKET_ERROR&&!RecFinished&&!paused); if(iResult==SOCKET_ERROR||paused)
goto End; //Receive from Web
do
{
strset(strBuffer,'\0');
//select检测rec超时(30s)
FD_ZERO(&localset);
FD_SET(localsocket,&localset);
tv.tv_sec=300;
tv.tv_usec=0;
if (select(0,&localset,0,0,&tv)<=0||paused)
{
goto End;
}
iResult=recv(localsocket,strBuffer,sizeof(strBuffer),0);
if(iResult==SOCKET_ERROR||paused)
{
goto End;
} FD_ZERO(&acceptset);
FD_SET(ClientSocket,&acceptset);
tv.tv_sec=300;
tv.tv_usec=0;
if(select(0,NULL,&acceptset,NULL,&tv)<=0||paused)
{
goto End;
}
iResult=send(ClientSocket,strBuffer,iResult,0);
if(iResult==SOCKET_ERROR||paused)
{
goto End;
} }
while(iResult!=0&&iResult!=SOCKET_ERROR&&!paused); //Exit the Thread
End:
//wait for the last send operation over
FD_ZERO(&acceptset);
FD_SET(ClientSocket,&acceptset);
tv.tv_sec=120;
tv.tv_usec=0;
select(0,NULL,&acceptset,NULL,&tv); closesocket(localsocket);
closesocket(ClientSocket); _endthread();
}
试一下用Get的方法,总不让最后的\r\n\r\n出现,然后自己用循环塑造一些Get的地址,也就是每次循环只发一部分过去,隔一秒发下一截。
有可能是ISA服务器认为该TCP连接有十秒没有发送数据,链路已断了.
如果封了估计你可以和服务端保持两个Connection ,这样一个用来发包,一个用来收包,发包可以用选项类的HTTP头,另外这些数据可以用BASE64编码;收包嘛就无所谓了,还有好象Content-Length是可以不写的,动态生成(CGI)的很多是没有这个头的,所以两个都可以保持