Getting Indy Error "Could not bind socket. Address and port are already in use"

Posted by M Schenkel on Stack Overflow See other posts from Stack Overflow or by M Schenkel
Published on 2010-05-17T12:39:00Z Indexed on 2010/05/17 14:00 UTC
Read the original article Hit count: 500

Filed under:

I have developed a Delphi web server application (TWebModule). It runs as a ISAPI DLL on Apache under Windows. The application in turn makes frequent https calls to other web sites using the Indy TIdHTTP component. Periodically I get this error when using the TIdHTTP.get method:

Could not bind socket. Address and port are already in use

Here is the code:

  IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocketOpenSSL.create(nil);
  IdHTTP := TIdHTTP.create(nil);


  idhttp.handleredirects := True;
  idhttp.OnRedirect := DoRedirect;
  with IdSSLIOHandlerSocket1 do begin
    SSLOptions.Method := sslvSSLv3;
    SSLOptions.Mode :=  sslmUnassigned;
    SSLOptions.VerifyMode := [];
    SSLOptions.VerifyDepth := 2;
  end;
  with IdHTTP do begin
    IOHandler := IdSSLIOHandlerSocket1;
    ProxyParams.BasicAuthentication := False;
    Request.UserAgent := 'Test Google Analytics Interface';
    Request.ContentType := 'text/html';
    request.connection := 'keep-alive';
    Request.Accept := 'text/html, */*';
  end;
  try
    idhttp.get('http://www.mysite.com......');
  except
    .......
  end;
  IdHTTP.free;
  IdSSLIOHandlerSocket1.free;

I have read about the reusesocket method, which can be set on both the TIdHttp and TIdSSLLIOHandlerSocketOpenSSL objects. Will setting this to rsTrue solve my problems? I ask because I have not been able to replicate this error, it just happens periodically.

Other considerations:

  • I know multiple instances of TWebModule are being spawned.
  • Would wrapping all calls to TIdHttp.get within a TCriticalSection solve the problem?

© Stack Overflow or respective owner

Related posts about delphi