- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要有关 TCPServer 和 TcpClient 问题的帮助。我正在使用 Delphi XE2 和 Indy 10.5。
我根据一个流行的屏幕捕获程序制作了服务器和客户端程序:
ScreenThief - stealing screen shots over the Network
我的客户端程序向服务器发送一个 .zip
文件和一些数据。这通常会单独工作几次,但如果我对其进行压力测试,其中通过计时器在 5 秒内执行传输 5 次,恰好在尝试 #63 时,客户端将无法再连接到服务器:
Socket Error # 10053
Software Caused Abort connection .
显然,服务器似乎耗尽了资源,无法接受更多客户端连接。
出现错误消息后,我无法以任何方式连接到服务器 - 不能在单独测试中,也不能在压力测试中。即使我退出并重新启动客户端,错误仍然存在。我必须退出并重新启动服务器,然后客户端才能再次连接。
有时客户端会发生套接字错误#10054,这会使服务器完全崩溃并必须重新启动。
我不知道发生了什么事。我只知道,如果服务器必须时不时地重新启动,那么它就不是一个健壮的服务器。
以下是客户端和服务器的源码,以便大家测试:
http://www.mediafire.com/download/m5hjw59kmscln7v/ComunicaTest.zip
运行服务器,运行客户端,然后选中“Just check to Run Infinite”。在测试中,服务器在 localhost
中运行。
有人可以帮助我吗?雷米·勒博?
最佳答案
我发现您的客户端代码存在问题。
您正在调用 TCPClient.Connect()之后分配
。您应该在调用 TCPClient.OnConnected
和 TCPClient.OnDisconnected
事件处理程序Connect()
之前分配它们。
您正在发送所有数据后分配TCPClient.IOHandler.DefStringEncoding
。您应该在发送任何数据之前对其进行设置。
您以字节形式发送 .zip
文件大小,然后使用 TStringStream
发送实际文件内容。您需要使用 TFileStream
或 TMemoryStream
来代替。另外,您可以从流中获取文件大小,无需在创建流之前查询文件大小。
您完全缺乏错误处理能力。如果在 btnRunClick()
运行时引发任何异常,则您将泄漏 TIdTCPClient
对象,并且不会将其与服务器断开连接。
我也发现您的服务器代码存在一些问题:
您的 OnCreate
事件在创建 Clients
列表之前激活服务器。
各种滥用 TThread.LockList()
和 TThreadList.Unlock()
。
不必要地使用InputBufferIsEmpty()
和TRTLCriticalSection
。
缺乏错误处理。
使用 TIdAntiFreeze
,这对服务器没有影响。
试试这个:
客户:
unit ComunicaClientForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
IdAntiFreezeBase, Vcl.IdAntiFreeze, Vcl.Samples.Spin, Vcl.ExtCtrls,
IdComponent, IdTCPConnection, IdTCPClient, idGlobal;
type
TfrmComunicaClient = class(TForm)
memoIncomingMessages: TMemo;
IdAntiFreeze: TIdAntiFreeze;
lblProtocolLabel: TLabel;
Timer: TTimer;
grp1: TGroupBox;
grp2: TGroupBox;
btnRun: TButton;
chkIntervalado: TCheckBox;
spIntervalo: TSpinEdit;
lblFrequencia: TLabel;
lbl1: TLabel;
lbl2: TLabel;
lblNumberExec: TLabel;
procedure FormCreate(Sender: TObject);
procedure TCPClientConnected(Sender: TObject);
procedure TCPClientDisconnected(Sender: TObject);
procedure TimerTimer(Sender: TObject);
procedure chkIntervaladoClick(Sender: TObject);
procedure btnRunClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmComunicaClient: TfrmComunicaClient;
implementation
{$R *.dfm}
const
DefaultServerIP = '127.0.0.1';
DefaultServerPort = 7676;
procedure TfrmComunicaClient.FormCreate(Sender: TObject);
begin
memoIncomingMessages.Clear;
end;
procedure TfrmComunicaClient.TCPClientConnected(Sender: TObject);
begin
memoIncomingMessages.Lines.Insert(0,'Connected to Server');
end;
procedure TfrmComunicaClient.TCPClientDisconnected(Sender: TObject);
begin
memoIncomingMessages.Lines.Insert(0,'Disconnected from Server');
end;
procedure TfrmComunicaClient.TimerTimer(Sender: TObject);
begin
Timer.Enabled := False;
btnRun.Click;
Timer.Enabled := True;
end;
procedure TfrmComunicaClient.chkIntervaladoClick(Sender: TObject);
begin
Timer.Interval := spIntervalo.Value * 1000;
Timer.Enabled := True;
end;
procedure TfrmComunicaClient.btnRunClick(Sender: TObject);
var
Size : Int64;
fStrm : TFileStream;
NomeArq : String;
Retorno : string;
TipoRetorno : Integer; // 1 - Anvisa, 2 - Exception
TCPClient : TIdTCPClient;
begin
memoIncomingMessages.Lines.Clear;
TCPClient := TIdTCPClient.Create(nil);
try
TCPClient.Host := DefaultServerIP;
TCPClient.Port := DefaultServerPort;
TCPClient.ConnectTimeout := 3000;
TCPClient.OnConnected := TCPClientConnected;
TCPClient.OnDisconnected := TCPClientDisconnected;
TCPClient.Connect;
try
TCPClient.IOHandler.DefStringEncoding := TIdTextEncoding.UTF8;
TCPClient.IOHandler.WriteLn('SendArq'); // Sinaliza Envio
TCPClient.IOHandler.WriteLn('1'); // Envia CNPJ
TCPClient.IOHandler.WriteLn('email@gmail.com'); // Envia Email
TCPClient.IOHandler.WriteLn('12345678'); // Envia Senha
TCPClient.IOHandler.WriteLn('12345678901234567890123456789012'); // Envia hash
memoIncomingMessages.Lines.Insert(0,'Write first data : ' + DateTimeToStr(Now));
NomeArq := ExtractFilePath(Application.ExeName) + 'arquivo.zip';
fStrm := TFileStream.Create(NomeArq, fmOpenRead or fmShareDenyWrite);
try
Size := fStrm.Size;
TCPClient.IOHandler.WriteLn(IntToStr(Size));
if Size > 0 then begin
TCPClient.IOHandler.Write(fStrm, Size, False);
end;
finally
fStrm.Free;
end;
memoIncomingMessages.Lines.Insert(0,'Write file: ' + DateTimeToStr(Now) + ' ' +IntToStr(Size)+ ' bytes');
memoIncomingMessages.Lines.Insert(0,'************* END *********** ' );
memoIncomingMessages.Lines.Insert(0,' ');
// Recebe Retorno da transmissão
TipoRetorno := StrToInt(TCPClient.IOHandler.ReadLn);
Retorno := TCPClient.IOHandler.ReadLn;
//making sure!
TCPClient.IOHandler.ReadLn;
finally
TCPClient.Disconnect;
end;
finally
TCPClient.Free;
end;
lblNumberExec.Caption := IntToStr(StrToInt(lblNumberExec.Caption) + 1);
end;
end.
服务器:
unit ComunicaServerForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
IdCustomTCPServer, IdTCPServer, IdScheduler, IdSchedulerOfThread,
IdSchedulerOfThreadPool, IdBaseComponent, IdSocketHandle, Vcl.ExtCtrls,
IdStack, IdGlobal, Inifiles, System.Types, IdContext, IdComponent;
type
TfrmComunicaServer = class(TForm)
txtInfoLabel: TStaticText;
mmoProtocol: TMemo;
grpClientsBox: TGroupBox;
lstClientsListBox: TListBox;
grpDetailsBox: TGroupBox;
mmoDetailsMemo: TMemo;
lblNome: TLabel;
TCPServer: TIdTCPServer;
ThreadManager: TIdSchedulerOfThreadPool;
procedure lstClientsListBoxClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure TCPServerConnect(AContext: TIdContext);
procedure TCPServerDisconnect(AContext: TIdContext);
procedure TCPServerExecute(AContext: TIdContext);
private
{ Private declarations }
procedure RefreshListDisplay;
procedure RefreshListBox;
public
{ Public declarations }
end;
var
frmComunicaServer: TfrmComunicaServer;
implementation
{$R *.dfm}
type
TClient = class(TIdServerContext)
public
PeerIP : string; { Client IP address }
HostName : String; { Hostname }
Connected, { Time of connect }
LastAction : TDateTime; { Time of last transaction }
end;
const
DefaultServerIP = '127.0.0.1';
DefaultServerPort = 7676;
procedure TfrmComunicaServer.FormCreate(Sender: TObject);
begin
TCPServer.ContextClass := TClient;
TCPServer.Bindings.Clear;
with TCPServer.Bindings.Add do
begin
IP := DefaultServerIP;
Port := DefaultServerPort;
end;
//setup TCPServer
try
TCPServer.Active := True;
except
on E: Exception do
ShowMessage(E.Message);
end;
txtInfoLabel.Caption := 'Aguardando conexões...';
RefreshListBox;
if TCPServer.Active then begin
mmoProtocol.Lines.Add('Comunica Server executando em ' + TCPServer.Bindings[0].IP + ':' + IntToStr(TCPServer.Bindings[0].Port));
end;
end;
procedure TfrmComunicaServer.FormClose(Sender: TObject; var Action: TCloseAction);
var
ClientsCount : Integer;
begin
with TCPServer.Contexts.LockList do
try
ClientsCount := Count;
finally
TCPServer.Contexts.UnlockList;
end;
if ClientsCount > 0 then
begin
Action := caNone;
ShowMessage('Há clientes conectados. Ainda não posso sair!');
Exit;
end;
try
TCPServer.Active := False;
except
end;
end;
procedure TfrmComunicaServer.TCPServerConnect(AContext: TIdContext);
var
DadosConexao : TClient;
begin
DadosConexao := TClient(AContext);
DadosConexao.PeerIP := AContext.Connection.Socket.Binding.PeerIP;
DadosConexao.HostName := GStack.HostByAddress(DadosConexao.PeerIP);
DadosConexao.Connected := Now;
DadosConexao.LastAction := DadosConexao.Connected;
(*
TThread.Queue(nil,
procedure
begin
MMOProtocol.Lines.Add(TimeToStr(Time) + ' Abriu conexão de "' + DadosConexao.HostName + '" em ' + DadosConexao.PeerIP);
end
);
*)
RefreshListBox;
AContext.Connection.IOHandler.DefStringEncoding := TIdTextEncoding.UTF8;
end;
procedure TfrmComunicaServer.TCPServerDisconnect(AContext: TIdContext);
var
DadosConexao : TClient;
begin
DadosConexao := TClient(AContext);
(*
TThread.Queue(nil,
procedure
begin
MMOProtocol.Lines.Add(TimeToStr(Time) + ' Desconnectado de "' + DadosConexao.HostName + '"');
end
);
*)
RefreshListBox;
end;
procedure TfrmComunicaServer.TCPServerExecute(AContext: TIdContext);
var
DadosConexao : TClient;
CNPJ : string;
Email : string;
Senha : String;
Hash : String;
Size : Int64;
FileName : string;
Arquivo : String;
ftmpStream : TFileStream;
Cmd : String;
Retorno : String;
TipoRetorno : Integer; // 1 - Anvisa, 2 - Exception
begin
DadosConexao := TClient(AContext);
Cmd := AContext.Connection.IOHandler.ReadLn;
if Cmd = 'SendArq' then
begin
CNPJ := AContext.Connection.IOHandler.ReadLn;
Email := AContext.Connection.IOHandler.ReadLn;
Senha := AContext.Connection.IOHandler.ReadLn;
Hash := AContext.Connection.IOHandler.ReadLn;
Size := StrToInt64(AContext.Connection.IOHandler.ReadLn);
// Recebe Arquivo do Client
FileName := ExtractFilePath(Application.ExeName) + 'Arquivos\' + CNPJ + '-Arquivo.ZIP';
fTmpStream := TFileStream.Create(FileName, fmCreate);
try
if Size > 0 then begin
AContext.Connection.IOHandler.ReadStream(fTmpStream, Size, False);
end;
finally
fTmpStream.Free;
end;
// Transmite arquivo para a ANVISA
Retorno := 'File Transmitted with sucessfull';
TipoRetorno := 1;
// Grava Log
fTmpStream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Arquivos\' + CNPJ + '.log', fmCreate);
try
WriteStringToStream(ftmpStream, Retorno, TIdTextEncoding.UTF8);
finally
fTmpStream.Free;
end;
// Envia Retorno da ANVISA para o Client
AContext.Connection.IOHandler.WriteLn(IntToStr(TipoRetorno)); // Tipo do retorno (Anvisa ou Exception)
AContext.Connection.IOHandler.WriteLn(Retorno); // Msg de retorno
// Sinaliza ao Client que terminou o processo
AContext.Connection.IOHandler.WriteLn('DONE');
end;
end;
procedure TfrmComunicaServer.lstClientsListBoxClick(Sender: TObject);
var
DadosConexao: TClient;
Index: Integer;
begin
mmoDetailsMemo.Clear;
Index := lstClientsListBox.ItemIndex;
if Index <> -1 then
begin
DadosConexao := TClient(lstClientsListBox.Items.Objects[Index]);
with TCPServer.Contexts.LockList do
try
if IndexOf(DadosConexao) <> -1 then
begin
mmoDetailsMemo.Lines.Add('IP : ' + DadosConexao.PeerIP);
mmoDetailsMemo.Lines.Add('Host name : ' + DadosConexao.HostName);
mmoDetailsMemo.Lines.Add('Conectado : ' + DateTimeToStr(DadosConexao.Connected));
mmoDetailsMemo.Lines.Add('Ult. ação : ' + DateTimeToStr(DadosConexao.LastAction));
end;
finally
TCPServer.Contexts.UnlockList;
end;
end;
end;
procedure TfrmComunicaServer.RefreshListDisplay;
var
Client : TClient;
i: Integer;
begin
lstClientsListBox.Clear;
mmoDetailsMemo.Clear;
with TCPServer.Contexts.LockList do
try
for i := 0 to Count-1 do
begin
Client := TClient(Items[i]);
lstClientsListBox.AddItem(Client.HostName, Client);
end;
finally
TCPServer.Contexts.UnlockList;
end;
end;
procedure TfrmComunicaServer.RefreshListBox;
begin
if GetCurrentThreadId = MainThreadID then
RefreshListDisplay
else
TThread.Queue(nil, RefreshListDisplay);
end;
end.
关于delphi - tcpserver x tcpclient 在运行压力测试时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27661700/
如何在 Ruby 中使用 TcpServer.new(0) 找到由 Windows 分配给 TcpServer 的端口 最佳答案 您可以使用 addr 获取它: tcp_server = TCPSer
我已经阅读了 Ruby 文档,但我并没有真正理解这两个对象之间的区别。 TCPServer.new(端口) 和 TCPServer.open("X.X.X.X", 端口) 我所知道的是 .new 仅响
我不得不为工作中的一个项目编写一个 TCP 服务器,我在谷歌上搜索了如何编写。我有一个 MSDN回答并设法让它工作,但我不知道如何让数据能够来回接收。 socket 的连接时间最多不会超过 30 秒(
import SocketServer import sys from Queue import * import threading class CustomTCPServer(SocketServ
python 2.7.2 window 7 32 位 让我以我对网络编程相当陌生作为这个问题的序言。我使用 python 的内置 SocketServer 创建了一个简单的服务器和客户端 Socket
我有一个正在编码的应用程序,用于通过 tcpsocket 将日志记录信息发送到服务器,并有一个监视器客户端连接到服务器以查看日志记录数据。到目前为止,我能够进入将信息发送到服务器的阶段,但是我需要一些
我正在尝试编写一个 c# 服务器/客户端,它将同时通过 TCP 相互发送字节数组。我正在努力思考如何实现这一目标。我见过的所有示例都在等待消息,然后发送响应。我需要同时进行交流。 我是否需要为服务器和
也许我把我的套接字编程方式搞混了,但像这样的东西不应该工作吗? srv = TCPServer.open(3333) client = srv.accept data = "" while (tmp
我目前仅使用标准库在 ruby 中实现一个简单的发布/订阅系统,客户端可以在其中订阅消息主题或将消息发布到主题,这些消息将发送给所有订阅者。我只使用标准的 TCPServer 并假设所有消息都在使
int main() { int sock, connected, bytes_recieved , true = 1; char send_data [1024] , recv_data[1
我正在尝试使用 jacoco 代理远程获取覆盖数据,并使用 Reset=true 重置服务器上的执行信息; 服务器上的jacoco java代理: JAVA_OPTIONS="${JAVA_OPTIO
我试图通过客户端的 GET 请求从 SocketServer 模块关闭 TCPServer,该请求在窗口关闭时发出,但是以下代码无法启动关闭: def show_webgl(data): im
我正在编写一个简单的 python3 网络服务器。我已经尝试了各种教程,并且都运行良好。尽管如此,还是有一点我不明白。 在一个教程中,他们使用 HTTPServer 如下: server = HTTP
目前我有一个 c# tcpclient 和一个 c++ tcpserver,但我只能从服务器端的流中读取字符,因此所有数据都将转换为字符。由于c++ char是一个有符号的8位整数,而c#客户端发送的
我一直在玩弄 Python 的 SocketServer: #!/usr/bin/python import SocketServer class EchoHandler(SocketServer.B
我正在创建一个仅在 Windows 操作系统上运行的客户端-服务器应用程序,每个 Windows 用户将一次连接到服务器。局域网使用DHCP,所以IP不固定。 我需要将字符串消息从 IdTCPServ
我尝试在客户端发送长度小于 255 的数据。 我尝试使用此协议(protocol)发送数据: Lenghth+String Byte+string sample : string = ABCD sen
我正在使用 Python 的 SocketServer.ThreadingTCPServer。现在我想知道在某个时刻连接了多少个客户端。 如何解决? 最佳答案 SocketServer.Threadi
我想创建一个 TCPserver 并根据需要向客户端发送/接收消息,而不是 TCPserver 的 OnExecute 事件。 发送/接收消息没有问题;我确实喜欢这样: procedure TForm
我需要有关 TCPServer 和 TcpClient 问题的帮助。我正在使用 Delphi XE2 和 Indy 10.5。 我根据一个流行的屏幕捕获程序制作了服务器和客户端程序: ScreenTh
我是一名优秀的程序员,十分优秀!