Question

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?

Was it helpful?

Solution

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

OTHER TIPS

The TClientsocket component (which is deprecated for a while already) uses the asynchronous communication model, so it is possible that the exception is not thrown in the Open method but in the message / event handling method which receives the incoming data.

update: I can reproduce this with Delphi 6 and the given code, if I enter an invalid IP address like 1.2.3.4

To fix it I would move to a TCP/IP library like Indy or Ararat Synapse (both have a generic TCP client component).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top