by Mike VanZant [mvanzant at gatewaysystems dot com] posted on 2004/07/23 |
|
I am using the SecureSocket class and am processing the Receive() in a loop until the return value is zero.
The application is designed to automatically navigate a HTTPS web site to eventually retrieve a data file.
The problem is that the call to (in a loop):
value = socket.Receive(buffer);
is fast until the final call returning a value of zero. On some web sites this can take 15 seconds or more.
On other web sites, all of the calls are fast.
I don't understand why this is happening and would appreciate anyone's insight into how I can fix this. Maybe some setting? Thanks!
Below are the relevant functions of the source code I'm using:
private void Connect(){
this.TryCallBackProgress(0, 0);
IPHostEntry lResolvedServer;
IPEndPoint lServerEndPoint;
SecureProtocol sp;
try{
AppLog.Log.DebugMessage("mServerName: " + mServerName);
lResolvedServer = Dns.Resolve(mServerName);
foreach( IPAddress addr in lResolvedServer.AddressList){
AppLog.Log.DebugMessage("IP: " + addr.ToString());
lServerEndPoint = new IPEndPoint(addr, this.URL.Port);
if(mIsHTTPS){
sp = SecureProtocol.Ssl3|SecureProtocol.Tls1;
}else{
sp = SecureProtocol.None;
}
SecurityOptions options = new SecurityOptions(sp);
options.Certificate = null;
options.Entity = ConnectionEnd.Client;
options.CommonName = this.URL.Host;
options.VerificationType = CredentialVerification.AutoWithoutCName;
options.Flags = SecurityFlags.Default;
options.AllowedAlgorithms = SslAlgorithms.ALL; //SslAlgorithms.SECURE_CIPHERS;
mSocket = new SecureSocket(addr.AddressFamily, SocketType.Stream, ProtocolType.Tcp, options);
try{
mSocket.Connect(lServerEndPoint);
AppLog.Log.DebugMessage("Connected socket.");
}
catch(Exception e){
AppLog.Log.Error("Connection of socket failed.", e);
//Connect failed so try the next one.
//Make sure to close the socket we opened.
if(mSocket != null){
mSocket.Close();
}
continue;
}
break;
}
}catch(Exception e){
AppLog.Log.Error("Client connection failed.",e);
throw;
}
}
private void ReceiveData(){
mContentLengthBytesForCallBack = 0;
mContentStartByte = 0;
this.TryCallBackProgress(this.mContentLengthBytesForCallBack, 0);
StringBuilder sbDataReceived = new StringBuilder();
byte[] lReceiveBuffer;
int lRCode;
string lstr = "";
int lTotalBytesReceived = 0;
this.mContentLength = 0;
try{
while(true){
lReceiveBuffer = new byte[16384]; //byte[102400]; //byte[10240]; //byte[1024];
try{
this.TimingLog("Doing socket receive with buffer size " + lReceiveBuffer.Length.ToString() + "...");
lRCode = mSocket.Receive(lReceiveBuffer);
this.TimingLog("Finished socket receive with return code of " + lRCode.ToString() + ".");
lTotalBytesReceived += lRCode;
if(lRCode > 0){
lstr = Encoding.ASCII.GetString(lReceiveBuffer,0,lReceiveBuffer.Length);
sbDataReceived.Append(lstr);
this.ParsePartialResponseForCallBackProgress(sbDataReceived.ToString());
}
else if(lRCode == 0){
break;
}
else{
Log.DebugMessage("socket receive returned negative code: " + lRCode);
break;
}
}
catch(ObjectDisposedException e){
Log.Error("Socket was closed.", e);
break;
}
catch(SocketException e){
Log.Error("SocketException", e);
break;
}
}
}
catch(Exception e){
Log.Error("Receive Failed.", e);
throw;
}
finally{
this.ParseReply(sbDataReceived.ToString());
}
}
|