You are not logged in.
Made a client-server application. The client sends files with an http Post request. The server accepts and saves files with a unique name in a special folder. But if you send a file from several machines, the message Socket error # 10061 Connection refused appears.
Offline
Here is the client code:
procedure TSendFile.SendFileToServer(const FileName: string; const ServerUrl: string);
var
HTTP: TIdHTTP;
Stream: TFileStream;
Response: string;
begin
HTTP := TIdHttp.Create(nil);
try
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
HTTP.Request.ContentType := 'txt';
HTTP.Request.ContentRangeStart := 0;
HTTP.Request.ContentLength := Stream.Size;
HTTP.Request.CharSet := 'utf-8';
Response := HTTP.Post(ServerUrl, Stream);
ShowMessage('Ответ сервера: ' + Response);
finally
Stream.Free;
end;
finally
HTTP.Free;
end;
end;
procedure TSendFile.ButtonSendClick(Sender: TObject);
var
IdHTTP: TIdHTTP;
FS: TFileStream;
Bytes: TBytes;
Base64String: string;
JSON: TJSONObject;
Response: string;
begin
SendFileToServer('C:\ubuntu-17.04-server-amd64.iso', '[url]http://192.168.0.63:8888/upload[/url]');
end;
end.
Last edited by Terinel (2024-08-29 06:26:33)
Offline
Here is the server code:
else if Ctxt.Url = '/upload' then
begin
try
ContentType := SetContentTypeFromFileName(Ctxt.InContentType);
Content := Ctxt.InContent;
FileName := 'unknown' + Random(100).ToString;
FileSize := Length(Content);
UploadPath := CreateUploadFolder(Ctxt);
try
if not Length(Ctxt.InContent) = 0 then
begin
TSynLog.Add.Log(sllWarning, 'Client has not finished sending data, but the folder is being created', Self);
ForceDirectories(UploadPath);
end;
SetLength(FileContentBytes, Length(Content));
for u := 0 to Length(Content) - 1 do
FileContentBytes u := Ord(Content u);
try
TSynLog.Add.Log(sllInfo, 'File processing started ' + Now.ToString + ' ' + FileName, Self);
FProjectData.SaveToFile(FileName, content);
except
on E: Exception do
begin
Ctxt.OutContent := FormatUtf8('Error creating file: %s', [E.Message]);
Ctxt.OutContentType := TEXT_CONTENT_TYPE;
Result := HTTP_SERVERERROR;
end;
end;
TSynLog.Add.Log(sllInfo, 'File processing completed ' + Now.ToString + ' ' + FileName, Self);
Ctxt.OutContent := FormatUtf8('File uploaded successfully: %s (%d байт)', [FileName, FileSize]);
Ctxt.OutContentType := TEXT_CONTENT_TYPE;
Result := HTTP_SUCCESS;
except
on E: Exception do
begin
Ctxt.OutContent := FormatUtf8('Error creating file: %s', [E.Message]);
Ctxt.OutContentType := TEXT_CONTENT_TYPE;
Result := HTTP_SERVERERROR;
end;
end;
except
on E: Exception do
end;
end
else
begin
Ctxt.RespStatus := HTTP_BADREQUEST;
Result := HTTP_BADREQUEST;
end;
end;
procedure TProjectData.SaveToFile(const pmcFileName: TFileName; Content: RawByteString);
begin
FileFromString(Content, pmcFileName);
end;
...
constructor TSimpleHttpAsyncServer.Create;
begin
inherited Create;
fHttpServer := THttpAsyncServer.Create(
'8888', nil, nil, '', SystemInfo.dwNumberOfProcessors + 1, 10000,
[hsoNoXPoweredHeader,
hsoNoStats,
hsoHeadersInterning,
hsoThreadSmooting
//, hsoLogVerb4ose
]);
//writeln('DropPriviledges=', DropPriviledges('abouchez'));
fHttpServer.HttpQueueLength := 100000; // needed e.g. from wrk/ab benchmarks
fHttpServer.OnRequest := DoOnRequest;
fHttpServer.WaitStarted; // raise exception e.g. on binding issue
end;
...
var
simpleServer: TSimpleHttpAsyncServer;
begin
with TSynLog.Family do
begin
Level :=
//LOG_VERBOSE +
LOG_FILTER[lfErrors];
//EchoToConsole := Level;
PerThreadLog := ptIdentifiedInOneFile;
HighResolutionTimestamp := true;
end;
simpleServer := TSimpleHttpAsyncServer.Create();
try
{$I-}
writeln;
TextColor(ccLightGreen);
writeln(simpleServer.fHttpServer.ClassName, ' running on localhost:8888'#10);
TextColor(ccWhite);
writeln('try curl http://localhost:8888/echo'#10);
TextColor(ccLightGray);
writeln('Press [Enter] to quit'#10);
TextColor(ccCyan);
ConsoleWaitForEnterKey;
writeln(ObjectToJson(simpleServer.fHttpServer, [woHumanReadable]));
TextColor(ccLightGray);
{$ifdef FPC_X64MM}
WriteHeapStatus(' ', 16, 8, {compileflags=}true);
{$endif FPC_X64MM}
finally
simpleServer.Free;
end;
end.
Last edited by Terinel (2024-08-29 06:27:45)
Offline
Please follow the forum rules
and DO NOT put such code in the forum threads.
Very strange. But other forum members also post code. They are fine, they don't get any comments. Then make comments to others. And yet, regarding my question, will they answer me on the topic? Or is making a comment a way to avoid answering?
Offline
ab wrote:Please follow the forum rules
and DO NOT put such code in the forum threads.Very strange. But other forum members also post code. They are fine, they don't get any comments. Then make comments to others. And yet, regarding my question, will they answer me on the topic? Or is making a comment a way to avoid answering?
@Terinel
You don't get it. If you post code at all, keep it very brief. I try to aim for approximately 10 lines or fewer; and if it is longer or for logs, I now use an online paste service like Paste.ee https://paste.ee/. There are many others like that available.
Apparently, it is necessary to do this in order not to overload this site's server.
Just my 2 cents.
Cheers,
JD
Last edited by JD (2024-08-28 09:15:08)
Offline
Got it. Thanks for your reply.
Offline