views:

154

answers:

2

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?

A: 

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).

mjustin
the code works fine when its connected to server(which is in other program) but when server is not there i get the 10049 error instead of what i want(a message box with a message 'error')
Omair Iqbal
why suggesting moving to a different library ? The solution to the user's problem is listed below.I have commercial projects that have been using TServerSocket and TClientSocket for years and they still work fine. I know that they do not work with Unicode, but apart from that, I do not see anything wrong with them and they are easy to use..
GX
Simple rule: if something is declared 'deprecated', start looking for a replacement early. (It might be unavailable or totally broken in the next release.)
mjustin
@mjustin: you are right that when TClientSocket works in non-blocking mode, the exception does not occur inside of Open() itself, but inside of a message handler that is triggered at a different time. However, TClientSocket can also operate in blocking mode, in which case the exception would occur inside of Open() instead.
Remy Lebeau - TeamB
@Remy: strange, I checked both modes (ClientType ctBlocking and ctNonBlocking) in Delphi 6 (Build 6.163) and the try except block had no effect either way.
mjustin
An error in resolving the Host WILL raise an exception from Open() in blocking mode. Open() does not exit until the connection to the server is successful, or until an error occurs. I have written plenty of code with TClientSocket in blocking mode, and all connect errors always raise exception before Open() exits.
Remy Lebeau - TeamB
+1  A: 

Hello Omair,

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

GX
thanks a heap....
Omair Iqbal