无法从 ttcpserver 向 ttcpclientserver 发送文本(字符串)

发布于 2024-09-08 12:29:45 字数 1065 浏览 2 评论 0原文

我有两种形式,一种用于服务器,另一种用于客户端。 将 ttcpserver 放在服务器表单上并将其 localhost 属性设置为 127.0.0.1 并将 localport 属性设置为 55555 并将 Active 属性设置为 true 后,我编写了一个 button1(sendtextbutton) onclick 事件处理程序:

procedure TForm2.Button1Click(Sender: TObject);
begin
      TcpServer1.Sendln('message');
end;

然后在客户端表单上我放置了 1 ttcpclient 1 label 2 按钮,设置客户端远程主机属性为 127.0.0.1,远程端口为 55555,为 connectbutton(button1) 编写了一个事件处理程序:

procedure TForm2.Button1Click(Sender: TObject);
begin
try
TcpClient1.Active := true;
except
showmessage('error');

end;
end;

为 ttcpclient 编写了一个 onconnect 事件:

procedure TForm2.TcpClient1Connect(Sender: TObject);
begin
    Label1.Caption := 'connected!';
end;

最后为 ttcpclient 编写了一个 onrecieve 事件处理程序:

procedure TForm2.TcpClient1Receive(Sender: TObject; Buf: PAnsiChar;
  var DataLen: Integer);
begin
    Label1.caption := TcpClient1.Receiveln();
end;

我的客户端程序标题应该更改为“消息”(在我连接并单击服务器表单上的按钮后),但事实并非如此。我这样做的方式不对吗?如果是,那么该怎么做呢?我正在尝试从服务器向客户端发送短信(是的,反向连接!)

I have 2 forms, 1 for server another for client.
After dropping ttcpserver on server form and setting its localhost property to 127.0.0.1 and localport property to 55555 and Active property to true I wrote a button1(sendtextbutton) onclick event handler:

procedure TForm2.Button1Click(Sender: TObject);
begin
      TcpServer1.Sendln('message');
end;

Then on client form I dropped 1 ttcpclient 1 label 2 buttons, set clients remote host property to 127.0.0.1 and remote port to 55555, wrote an event handler for connectbutton(button1):

procedure TForm2.Button1Click(Sender: TObject);
begin
try
TcpClient1.Active := true;
except
showmessage('error');

end;
end;

Wrote an onconnect event for ttcpclient:

procedure TForm2.TcpClient1Connect(Sender: TObject);
begin
    Label1.Caption := 'connected!';
end;

and then finally an onrecieve event hadler for ttcpclient:

procedure TForm2.TcpClient1Receive(Sender: TObject; Buf: PAnsiChar;
  var DataLen: Integer);
begin
    Label1.caption := TcpClient1.Receiveln();
end;

My client programs caption was supposed to change to 'message'(after I connect and click button on my server form), but it doest. Am I doing it the wrong way? If yes, then how to do it? I am trying to send a text message from server to client(Yes a reverse connection!)

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

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

发布评论

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

评论(2

提笔书几行 2024-09-15 12:29:45

TTcpServer 不存储已连接的连接列表,这使得广播式消息变得困难。

我建议切换到 TidTcpServer 和 TidTcpClient。 TidTcpServer 组件有一个 Context 属性,您可以循环使用它来向客户端广播消息,类似于您想要做的事情。

这里有一些使用 TidTcpServer 和 TIdTcpClient 的示例链接:

TTcpServer does not store a list of connected connection which make broadcast style messages difficult.

I would recommend switching to TidTcpServer and TidTcpClient. The TidTcpServer component has a Context Property that you can loop through to broadcast messages to the clients similar to what you seem to want to do.

Here some links to examples of using TidTcpServer and TIdTcpClient:

丑疤怪 2024-09-15 12:29:45

您的服务器代码不起作用,因为 TTcpServer.SendLn() 不会将数据发送到已连接套接字的客户端端点。这就是为什么客户端永远看不到数据的原因 - 它被发送到地狱边缘。

如果 TTcpServer.BlockMode 属性设置为 bmThreadBlocking(默认情况下),或者如果 TTcpServer.BlockMode 设置为其他值并且您手动调用无参数重载 TTcpServer.Accept() 方法,那么您可以访问客户端端点的唯一位置是从 TTcpServer.OnAccept 事件内部。在这些情况下,当该事件处理程序退出时,服务器将断开与客户端的连接,因此服务器想要对客户端执行的任何工作都必须从该事件内部完成。

如果这不满足您的需求,则必须将 TTcpServer.BlockMode 属性设置为 bmBlockingbmNonBlocking,然后手动调用重载的 TTcpServer.Accept() 方法返回 TCustomIpClient 对象。触发并退出 TTcpServer.OnAccept 事件后,您将获得该对象的所有权并完全控制其生命周期,然后可以随时随地访问它。

Your server code does not work because TTcpServer.SendLn() does not send the data to the client's endpoint of the connected socket. That is why the client never sees the data - it is being sent into limbo.

If the TTcpServer.BlockMode property is set to bmThreadBlocking (which it is by default), or if the TTcpServer.BlockMode is set to anything else and you are manually calliing the parameterless overloaded TTcpServer.Accept() method, then the only place you have access to the client's endpoint is from inside the TTcpServer.OnAccept event. Under these conditions, when that event handler exits, the server will disconnect the client, so any work the server wants to do with the client must be done from inside that event.

If that does not suit your needs, then you will have to set the TTcpServer.BlockMode property to either bmBlocking or bmNonBlocking and then manually call the overloaded TTcpServer.Accept() method that returns a TCustomIpClient object. After the TTcpServer.OnAccept event has been triggered and exited, you will gain ownership of that object and have full control over its lifetime and can then access it whenever and however you want.

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