简体   繁体   English

Indy 9 到 Indy 10 应用程序:连接被拒绝

[英]Indy 9 to Indy 10 App: Connection refused

I'm porting and old Delphi 2009 app with Indy 9 to Delphi 11 with Indy 10:我正在将带有 Indy 9 的旧 Delphi 2009 应用程序移植到带有 Indy 10 的 Delphi 11:

// *************** Indy Version 9:
IdTCPClient1.Host := '123.456.789.012';
IdTCPClient1.Port := 123;
IdTCPClient1.Connect;
IdTCPClient1.OpenWriteBuffer;
IdTCPClient1.Write(Data);
i :=  IdTCPClient1.ReadInteger(True);
x := IdTCPClient1.ReadLn('</root>', 20000, 1500);
// Works OK

// *************** Indy Version 10
XMLStream := TMemoryStream.Create;
XMLDoc1.XML.SaveToStream(XMLStream);
IdTCPClient1.Host := '123.456.789.012';
IdTCPClient1.Port := 123;
IdTCPClient1.Connect;
IdTCPClient1.Socket.Open;
IdTCPClient1.Socket.WriteBufferOpen;
IdTCPClient1.Socket.Write(XMLStream, XMLStream.Size);
i := IdTCPClient1.Socket.ReadInt32(True);
x := IdTCPClient1.Socket.ReadLn('</root>', 20000, 1500);
// Socket Error # 10061 Connection refused.

Both versions are executing in the same PC/User... Any help will be appreciated.两个版本都在同一台 PC/用户中执行...任何帮助将不胜感激。 Francisco弗朗西斯科

    //Still receiving Socket Error # 10061: Connection Refused
    IdTCPClient1.Host := '123.456.789.0';
    IdTCPClient1.Port := 55065;
    IdTCPClient1.Connect;
    IdTCPClient1.Socket.WriteBufferOpen;
    IdTCPClient1.Socket.Write(XMLStream, XMLStream.Size);
    i := IdTCPClient1.Socket.ReadInt32(True);
    sResp := IdTCPClient1.Socket.ReadLn('</root>', 20000, 1500);
    IdTCPClient1.Socket.WriteBufferFlush;
    IdTCPClient1.Socket.Close;
    IdTCPClient1.Disconnect;
    // I omitted some code like try, except

Your translation to Indy 10 is not quite correct.您对 Indy 10 的翻译不太正确。 You should not be calling Socket.Open() at all, as Connect() already handles that internally, and that Open() call is what actually creates the socket and establishes the TCP connection.您根本不应该调用Socket.Open() ,因为Connect()已经在内部处理了它,而Open()调用实际上创建了套接字并建立了 TCP 连接。 So, you are trying to open a 2nd connection, which won't work.因此,您正在尝试打开第二个连接,但这是行不通的。 Get rid of that call completely:完全摆脱那个电话:

IdTCPClient1.Connect;
// IdTCPClient1.Socket.Open; // <-- get rid of this!

Also, in both of your codes, you are opening Indy's internal write buffer, but where are you flushing/closing it?另外,在你的两个代码中,你都打开了 Indy 的内部写缓冲区,但是你在哪里刷新/关闭它? As long as the buffer is open, any writes you perform will store their bytes in the buffer only and not transmit them over the socket (unless you assign a buffer threshold value, which your example is not).只要缓冲区处于打开状态,您执行的任何写入操作都只会将它们的字节存储在缓冲区中,而不会通过套接字传输它们(除非您分配了缓冲区阈值,而您的示例不是)。

// *************** Indy Version 9
IdTCPClient1.Connect;
IdTCPClient1.OpenWriteBuffer;
IdTCPClient1.Write(Data);
...
// where is the call to (Flush/Close/Cancel)WriteBuffer?
// *************** Indy Version 10
IdTCPClient1.Connect;
IdTCPClient1.Socket.WriteBufferOpen;
IdTCPClient1.Socket.Write(XMLStream, XMLStream.Size);
...
// where is the call to WriteBuffer(Flush/Close/Cancel)?

In general, you don't need to use Indy's write buffer unless you are sending LOTS of data and would benefit from the performance boost that the buffering might offer you.通常,您不需要使用 Indy 的写缓冲区,除非您正在发送大量数据并且会受益于缓冲可能为您提供的性能提升。 TCP already does a decent job of buffering data in the socket's own internal buffer by default, so you typically don't need to use Indy's write buffering on top of that, too.默认情况下,TCP 已经在套接字自己的内部缓冲区中很好地缓冲数据,因此您通常也不需要在此之上使用 Indy 的写缓冲。

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

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