- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试使用 Delphi firemonkey 应用程序创建一个 TCP 服务器。
我的资源文件Unit1.fmx
如下。
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 480
ClientWidth = 640
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
OnActivate = FormShow
OnCreate = FormCreate
DesignerMasterStyle = 0
object Label1: TLabel
Position.X = 504.000000000000000000
Position.Y = 248.000000000000000000
Text = 'Label1'
end
object TCPServer: TIdTCPServer
Bindings = <>
DefaultPort = 0
OnAfterBind = TCPServerAfterBind
OnConnect = TCPServerOnConnect
OnExecute = TCPserverExecute
Left = 560
Top = 184
end
end
我的代码文件在这里。
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Platform,
IdCustomTCPServer, IdTCPServer, IdBaseComponent, IdComponent, IdUDPBase, IdContext,
IdSocketHandle, IdUDPServer, FMX.Controls.Presentation, FMX.StdCtrls, Windows;
type
TForm1 = class(TForm)
TCPServer: TIdTCPServer;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure TCPserverExecute(AContext: TIdContext);
procedure TCPServerOnConnect(AContext: TIdContext);
procedure TCPServerAfterBind(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMyLoggingService = class(TInterfacedObject, IFMXLoggingService)
public
procedure Log(const Format: string; const Params: array of const);
end;
var
Form1: TForm1;
MyLoggingService: IFMXLoggingService;
implementation
{$R *.fmx}
procedure TMyLoggingService.Log(const Format: string; const Params: array of const);
begin
// do whatever you want...
OutputDebugString(PChar(Format));
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Binding : TIdSocketHandle;
begin
MyLoggingService := TMyLoggingService.Create;
if TPlatformServices.Current.SupportsPlatformService( IFMXLoggingService ) then
TPlatformServices.Current.RemovePlatformService( IFMXLoggingService );
// register my service
TPlatformServices.Current.AddPlatformService( IFMXLoggingService, MyLoggingService );
TCPServer.DefaultPort := 16000;
TCPServer.Bindings.Clear;
Binding := TCPServer.Bindings.Add;
Binding.IP := '127.0.0.1';
Binding.Port := 16000;
//if Assigned(MyLoggingService) then
MyLoggingService.Log('FormCreate!',[]);
end;
procedure TForm1.FormShow(Sender: TObject);
begin
TCPServer.Active := True;
end;
procedure TForm1.TCPServerAfterBind(Sender: TObject);
begin
MyLoggingService.Log('After Bind',[]);
end;
procedure TForm1.TCPServerOnConnect(AContext: TIdContext);
begin
MyLoggingService.Log('Recieved Connection From Client ',[]);
end;
procedure TForm1.TCPServerExecute(AContext: TIdContext);
Var
C : String;
begin
C:= AContext.Connection.Socket.ReadLn();
//if Assigned(MyLoggingService) then
MyLoggingService.Log('TserverExecute !',[]);
MyLoggingService.Log('Recived Data !',[C]);
if C = 'TESTSTRING' then
begin
AContext.Connection.Socket.Writeln('SENT');
end;
end;
end.
我的问题是在我记录消息的系统日志中,这是我得到的。
Debug Output: FormCreate! Process Project1.exe (3340)
Debug Output: After Bind Process Project1.exe (3340)
Debug Output: Recieved Connection From Client Process Project1.exe (3340)
First chance exception at $759BC42D. Exception class EIdSocketError with message
'Socket Error # 10054
Connection reset by peer.'.
Process Project1.exe (3340)
connection reset by peer .我的问题是为什么我的代码会发生这种情况?我做错了什么,我该如何解决?
在远程端,我有一个非常简单的 python 脚本,如下所示。
import socket
# Set up a TCP/IP socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Connect as client to a selected server
# on a specified port
s.connect(('127.0.0.1',16000))
# Protocol exchange - sends and receives
data = 'TESTSTRING';
s.send(data.encode())
while True:
resp = s.recv(1024)
if resp == "":
print('NOTHING')
break
else:
print(resp)
break
# Close the connection when completed
s.close()
print("\ndone")
最佳答案
服务器代码没有问题。客户端在其端断开连接,而服务器端的调试器 只是检测 Indy 向自身引发的异常以处理断开连接。 这是正常行为。
但是,您的客户端代码存在问题。在服务器端,您正在使用 TIdIOHandler.ReadLn()
,它期望客户端发送一行由 LF
字符(或 CRLF
字符串)。但是您的客户端脚本根本没有发送该分隔符,因此 ReadLn()
不会退出,因此服务器不会调用 TIdIOHandler.WriteLn()
来发送响应.在客户端,s.recv()
可能失败(因为没有收到响应)并且脚本结束,断开套接字,导致 ReadLn()
服务器端将异常返回给 TIdTCPServer
,以便它可以停止线程并关闭套接字。
关于delphi - Indy TCPServer 在客户端连接时出现 `Connection reset by peer` 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36073889/
每次暂存文件时,如果您需要取消暂存文件,Git 都会提供有用的说明: (use "git reset HEAD ..." to unstage) 不过体面Git Tutorials by Atlass
我需要添加几个文件以将它们组合到一个提交中,但我必须排除其中一个。在 this answer ,执行此操作的代码是: git add -u git reset -- file_to_ignore.da
这个问题在这里已经有了答案: What are typical use cases of git-reset's --merge and --keep flags? (4 个答案) 关闭 6 年前。
有时候,进行了错误的提交,但是还没有push到远程分支,想要撤销本次提交,可以使用git reset –-soft/hard命令。 1、二者区别: git reset –-soft:回退到某个版
我认为:软重置:从重置向量启动。硬复位:拉CPU的电平。 最佳答案 硬复位当然意味着整个CPU芯片及其所有外设都被复位。造成这种情况的原因可能有很多:复位引脚被外部拉高、时钟故障、片内低电压检测、看门
$python manage.py reset Unknown command: 'reset' Type 'manage.py help' for usage. 在django 1.6中是否取消了这
我是 git 的新手,所以问题可能很简单,git reset --hard HEAD 和 git reset --hard 有什么区别? 最佳答案 HEAD 在您未指定该参数时是隐含的。 但是,您可以
我使用 apollo-link-state 来本地存储错误,但清除缓存后出现以下错误。 我已在 apollo 客户端配置选项中将 errors 的默认值设置为空数组 []。 但是,在 apolloCl
我正在使用 bool 数组来存储标志(类似于“已更改”)。数组的大小是静态的,在编译时已知。 我需要定期重置数组,即将所有元素设置为 false。我应该使用常规数组和类似 memset 或 memcp
在 git 文档(和许多 SO 线程)中,推荐使用这种重置方法: $ git reset --soft HEAD^ ;# go back to WIP state $ git reset
在我的实验中,我没能发现两者之间的任何功能差异 git reset --hard 和 git reset --merge 使用说明也没有给出任何提示 --hard res
如何重置所有列过滤器?调用 reset() 似乎重置了表,但过滤器的所有字段保持不变。 最佳答案 您可以将输入值绑定(bind)到表的过滤器,如下所示: 注意 [value] 绑定(bind)。 关
使用 std::unique_ptr::reset,您可以轻松地将您的实例恢复到新状态。 C++11 之前,为了实现类似的行为,我看到很多类都定义了一个 Reset() 方法来重置其所有内部成员。但现
为了恢复工作树和索引中的更改,此答案 ( https://stackoverflow.com/a/5812972/8278160) 建议运行以下命令: git reset --hard 运行它是否与运
我目前正在测试竞技场。我本来以为这段代码会编译,但在运行时失败了,令人惊喜的是,编译器发现了这个问题。但我不知道它的推理是否正确。有人能给我解释一下吗?。错误:。来自umpalo的相关代码:
我目前正在测试竞技场。我本来以为这段代码会编译,但在运行时失败了,令人惊喜的是,编译器发现了这个问题。但我不知道它的推理是否正确。有人能给我解释一下吗?。错误:。来自umpalo的相关代码:
我正在尝试在不触发“重置”事件的情况下重置我的收藏。我已经设置了我的收藏来收听“重置”和“添加”事件 @.listenTo(@options.muses, 'add', @addOne) @.list
根据http://en.cppreference.com/w/cpp/memory/unique_ptr/reset , void reset( pointer ptr = pointer() );
我有一个别名,unstage,用于从暂存区域中删除更改。 unstage = reset -- 我注意到 git 的帮助建议改为 git reset HEAD。我还注意到 git rm --cache
这个问题在这里已经有了答案: What's the difference between HEAD^ and HEAD~ in Git? (17 个答案) 关闭 6 年前。 git reset --
我是一名优秀的程序员,十分优秀!