delphi tserversocket 忽略第一条消息

发布于 2024-12-08 11:10:44 字数 501 浏览 0 评论 0原文

请告知为什么会发生这种情况。 在一个简单的示例服务器应用程序上,我有以下代码:

procedure TForm13.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var str : string;
begin
  str := socket.ReceiveText;
  showmessage(str);
end;

在客户端,我没有

clientsocket1.Open;
clientsocket1.Socket.SendText(txtMSG.Text);

什么花哨的。 奇怪的是,当我第一次向服务器发送消息时,它被忽略了。此后每次效果都很好。 ClientRead 事件根本不会在第一条消息上触发,

我可以在服务器上更改什么以使其接受第一条消息。 我无法控制客户端,因为第三方向我发送消息,但我总是错过第一条消息。

谢谢!

pls advise why this happens.
on a simple sample server app i have the following code:

procedure TForm13.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var str : string;
begin
  str := socket.ReceiveText;
  showmessage(str);
end;

and on the client side i have

clientsocket1.Open;
clientsocket1.Socket.SendText(txtMSG.Text);

nothing fancy.
the strange thing is that when i send a message to the server for 1st time it gets ignored. every time after that it works great. the ClientRead event doesn't fire off at all on the 1st message

what can i change on the server to make it accept the 1st message.
I have no control over the client side as a 3rd party sends me messages, ut i am always missing the 1st message.

thanks!

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

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

发布评论

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

评论(1

幸福丶如此 2024-12-15 11:10:44

如果您在非阻塞模式(默认)下使用 TClientSocket,则无法在 Open() 返回后立即发送数据,因为连接未建立。准备好了。您必须先等待 OnConnect 事件被触发,例如:

Procedure TForm1.StartConnectingToServer;
Begin
  ClientSocket1.Open;
End;

Procedure TForm1.ClientSocket1Connect(Socket: TCustomWinSocket);
Begin
  Socket.SendText(txtMSG.Text);
End;

If you are using the TClientSocket in non-blocking mode (which is the default), you can't send data immediately after Open() returns, as the connection is not ready yet. You have to wait for the OnConnect event to be triggered first, eg:

Procedure TForm1.StartConnectingToServer;
Begin
  ClientSocket1.Open;
End;

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