delphi tserversocket 忽略第一条消息
请告知为什么会发生这种情况。 在一个简单的示例服务器应用程序上,我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在非阻塞模式(默认)下使用
TClientSocket
,则无法在Open()
返回后立即发送数据,因为连接未建立。准备好了。您必须先等待OnConnect
事件被触发,例如:If you are using the
TClientSocket
in non-blocking mode (which is the default), you can't send data immediately afterOpen()
returns, as the connection is not ready yet. You have to wait for theOnConnect
event to be triggered first, eg: