getting asynchronous socket error 10049 even if i use try..except

Omair Iqbal picture Omair Iqbal · Aug 1, 2010 · Viewed 7.3k times · Source

when ever i run my program(outside the debugger/ide) i get error asynchronous socket error 10049, am i not supposed to recieve a message dialoge : ''error''? see my code below

begin
    try
       ClientSocket1.open;
    except
       showmessage('error');
    end;
end;

what am i doing wrong?

Answer

G-Man picture G-Man · Aug 1, 2010

What you should do is handle the Error event of the TClientSocket, because that is where you will be able to capture your socket errors.

The ErrorCode parameter is the one that will have the WinSock Error code If you want to silence the Error, you can set ErrorCode to 0, which will prevent the exception from being thrown, and after that you can identify what the error is and handle it the way you want it

procedure TForm1.ClientSocket1Error(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
var error : Integer; 
begin

   error := ErrorCode; {prevent exception from being thrown}

   ErrorCode := 0;

   if error = 10049 then
     showmessage('asynchronous socket error');
.
.
.


end;

I hope this helps

Gaetan Siry