简体   繁体   English

即使我使用try..except,也会收到异步套接字错误10049

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

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''? 当我运行我的程序(在调试器/ ide之外)我得到错误异步套接字错误10049,我不应该收到消息dialoge:''错误''? see my code below 看下面的代码

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

what am i doing wrong? 我究竟做错了什么?

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. 你应该做的是处理TClientSocket的Error事件,因为这是你能够捕获套接字错误的地方。

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 ErrorCode参数是具有WinSock错误代码的参数如果要使Error静音,可以将ErrorCode设置为0,这将防止抛出异常,之后您可以识别错误是什么并处理它你想要的方式

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 Gaetan Siry

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. TClientsocket组件(已经弃用 了一段时间 )使用异步通信模型,因此可能不会在Open方法中抛出异常,而是在接收传入数据的消息/事件处理方法中抛出异常。

update: I can reproduce this with Delphi 6 and the given code, if I enter an invalid IP address like 1.2.3.4 更新:如果输入无效的IP地址(如1.2.3.4),我可以使用Delphi 6和给定的代码重现这一点

To fix it I would move to a TCP/IP library like Indy or Ararat Synapse (both have a generic TCP client component). 为了解决这个问题,我将迁移到像Indy或Ararat Synapse这样的TCP / IP库(两者都有一个通用的TCP客户端组件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM