即使我使用 try..except 也会出现异步套接字错误 10049

发布于 2024-09-12 12:23:35 字数 210 浏览 0 评论 0原文

当我运行我的程序(在调试器/IDE之外)时,我收到错误异步套接字错误10049,我不应该收到消息对话框:“错误”吗?请参阅下面的代码

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

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪少女 2024-09-19 12:23:36

您应该做的是处理 TClientSocket 的 Error 事件,因为这是您能够捕获套接字错误的地方。

ErrorCode 参数是包含 WinSock 错误代码的参数
如果你想消除错误,你可以将 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;

我希望这对

Gaetan Siry有帮助

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

画▽骨i 2024-09-19 12:23:36

TClientsocket 组件(已弃用 暂时已经)使用异步通信模型,所以有可能异常不是在Open方法中抛出而是在消息中抛出/ 接收传入数据的事件处理方法。

更新:如果我输入一个无效的 IP 地址(如 1.2.3.4),我可以使用 Delphi 6 和给定的代码重现此问题。

要修复它,我将移动到 TCP/IP 库,如 Indy 或 Ararat Synapse(两者都有通用的 TCP 客户端组件)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文