Delphi 和 c# 之间的 TCP 通信问题

本文关键字:TCP 通信 问题 之间 Delphi | 更新日期: 2023-09-27 18:30:35

我正在尝试使用 TCP 将文本行从 c# 客户端发送到 delphi 服务器。两者都只是在本地主机上运行。我希望客户端在合适的时间发送文本行,并且服务器能够在适合的时候从传入流中一次一行地读取和处理它们。

下面的代码通过 delphi 备忘录显示"某行文本 1"来实现一次。之后,c# 返回并异常,指出连接被强制关闭。

如果我关闭客户端连接并在每次发送一行文本时重新建立新连接,我可以达到预期的效果。但这非常缓慢,对于我的预期用途不可行。

我是TCP的新手,不知道我在做什么!任何有助于实现预期结果的帮助将不胜感激。

德尔福服务器代码是....

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Lines.Clear;
  //Server initialization
  TcpServer1 := TTcpServer.Create(Self);
  TcpServer1.OnAccept := TcpServer1Accept;
  TcpServer1.LocalPort := IntToStr(DEFAULT_PORT);
  TcpServer1.Active := true;

end; //TForm1.FormCreate procedure ends

procedure TForm1.TcpServer1Accept(Sender: TObject;
  ClientSocket: TCustomIpClient);
var
somestring : string;
begin
    somestring := ClientSocket.Receiveln('#$D#$A');
    Memo1.Lines.Add(somestring);
end; //TForm1.TcpServer1Accept ends

C#代码是............。

public static void Main (string[] args)
{
    bool connectionEstablished = false;
    int messageNum = 1;
    TcpClient theclient = new TcpClient();
    //first try establish a successful connection before proceeding
    Console.WriteLine("Waiting for server......");
    while (connectionEstablished == false) {
        connectionEstablished = true;
        try {
            Int32 port = 2501;
            string server = "127.0.0.1"; //the ip of localhost
            theclient = new TcpClient(server, port);
        } catch {
            Console.WriteLine("Could not find AI server");
            connectionEstablished = false;  
        }

    } //while (connectionEstablished == false) ends
    Console.WriteLine("Connected to server");
    ////////////////////////////////////////
    while (true) {
      try 
      {
        string message = "some line of text " + messageNum.ToString() + "#$D#$A";   
        // Translate the passed message into ASCII and store it as a Byte array.
        Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);         
        NetworkStream stream = theclient.GetStream();
        stream.Write(data, 0, data.Length); //working
        messageNum = messageNum + 1;
      }
      catch (ArgumentNullException e) 
      {
        Console.WriteLine("ArgumentNullException: {0}", e);
        Console.WriteLine("'n Press Enter to continue...");
        Console.Read();                             
      } 
      catch (SocketException e) 
      {
        Console.WriteLine("SocketException: {0}", e);
        Console.WriteLine("'n Press Enter to continue...");
        Console.Read();         
      }

        System.Threading.Thread.Sleep(2500);
    } //while (true) ends
    ////////////////////////////////////////////////

}

    }

Delphi 和 c# 之间的 TCP 通信问题

创建 TcpClient 实例后,实际上需要Connect .

您还可以使用 IPAddress

a = IPAddress.Loopback; 作为环回适配器,这样就不需要解析它。

try 
{
    Int32 port = 2501;
    string server = "127.0.0.1"; //the ip of localhost
    theclient = new TcpClient();
    theclient.Connect(server,port);
}
我不知道

Delphi 中套接字类的确切行为,但在我看来,如果OnAccept过程TForm1.TcpServer1Accept结束,服务器会关闭连接。因此,您还应该有一个循环,也许带有结束标准:

procedure TForm1.TcpServer1Accept(Sender: TObject; ClientSocket: TCustomIpClient);
var
  somestring : string;
begin
  do
    begin
      somestring := ClientSocket.Receiveln('#$D#$A');
      Memo1.Lines.Add(somestring);
    end;
  while(somestring.length > 0);
end; //TForm1.TcpServer1Accept ends

这将不是有效的德尔福代码,因为我很久没有使用德尔福/帕斯卡了,但我想你会明白的......