AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

System.Net.Sockets

Ein Thema von sdean · begonnen am 4. Jul 2023 · letzter Beitrag vom 4. Jul 2023
Antwort Antwort
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#1

System.Net.Sockets

  Alt 4. Jul 2023, 10:28
Hello every one , please i need an example on how to Send/Receive TCP packet with TSocket from System.Net.Socket (cross platform) in an AsyncCallback way or in a separate thread .

Thank you in advance .
  Mit Zitat antworten Zitat
Benutzerbild von Sherlock
Sherlock

Registriert seit: 10. Jan 2006
Ort: Offenbach
3.797 Beiträge
 
Delphi 12 Athens
 
#2

AW: System.Net.Sockets

  Alt 4. Jul 2023, 10:48
Hi, please consider asking your question over at our english speaking sister site: https://en.delphipraxis.net/

Sherlock
Oliver
Geändert von Sherlock (Morgen um 16:78 Uhr) Grund: Weil ich es kann
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#3

AW: System.Net.Sockets

  Alt 4. Jul 2023, 11:20
Hi, please consider asking your question over at our english speaking sister site: https://en.delphipraxis.net/

Sherlock
Thank you Sherlock , DONE .

But still waiting for any helpful response over here too .
  Mit Zitat antworten Zitat
Benutzerbild von Union
Union

Registriert seit: 18. Mär 2004
Ort: Luxembourg
3.492 Beiträge
 
Delphi 7 Enterprise
 
#4

AW: System.Net.Sockets

  Alt 4. Jul 2023, 12:01
Or consider asking AI , see below.

Delphi-Quellcode:
unit MainForm;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  System.Net.Socket;

type
  TForm1 = class(TForm)
    ButtonSend: TButton;
    MemoLog: TMemo;
    procedure ButtonSendClick(Sender: TObject);
  private
    FSocket: TSocket;
    procedure AsyncCallback(const ASyncResult: IAsyncResult);
    procedure DisplayReceivedData(const AData: TBytes);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.ButtonSendClick(Sender: TObject);
var
  DataToSend: TBytes;
begin
  DataToSend := TEncoding.UTF8.GetBytes('Hello, server!');

  if not Assigned(FSocket) then
  begin
    FSocket := TSocket.Create(TIPAddress.Any, 0);
    FSocket.Connect('127.0.0.1', 1234);
    FSocket.BeginSend(DataToSend, 0, Length(DataToSend), AsyncCallback, nil);
  end;
end;

procedure TForm1.AsyncCallback(const ASyncResult: IAsyncResult);
var
  BytesReceived: Integer;
  ReceivedData: TBytes;
begin
  try
    BytesReceived := FSocket.EndReceive(ASyncResult);
    if BytesReceived > 0 then
    begin
      SetLength(ReceivedData, BytesReceived);
      FSocket.ReceiveBuffer.Read(ReceivedData, 0, BytesReceived);
      TThread.Queue(nil,
        procedure
        begin
          DisplayReceivedData(ReceivedData);
        end);
    end;
  except
    on E: Exception do
    begin
      TThread.Queue(nil,
        procedure
        begin
          MemoLog.Lines.Add('Error: ' + E.Message);
        end);
    end;
  end;
end;

procedure TForm1.DisplayReceivedData(const AData: TBytes);
begin
  MemoLog.Lines.Add('Received: ' + TEncoding.UTF8.GetString(AData));
end;

end.
Ibi fas ubi proxima merces
sudo /Developer/Library/uninstall-devtools --mode=all
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#5

AW: System.Net.Sockets

  Alt 4. Jul 2023, 13:09
Or consider asking AI , see below.

Delphi-Quellcode:
unit MainForm;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  System.Net.Socket;

type
  TForm1 = class(TForm)
    ButtonSend: TButton;
    MemoLog: TMemo;
    procedure ButtonSendClick(Sender: TObject);
  private
    FSocket: TSocket;
    procedure AsyncCallback(const ASyncResult: IAsyncResult);
    procedure DisplayReceivedData(const AData: TBytes);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.ButtonSendClick(Sender: TObject);
var
  DataToSend: TBytes;
begin
  DataToSend := TEncoding.UTF8.GetBytes('Hello, server!');

  if not Assigned(FSocket) then
  begin
    FSocket := TSocket.Create(TIPAddress.Any, 0);
    FSocket.Connect('127.0.0.1', 1234);
    FSocket.BeginSend(DataToSend, 0, Length(DataToSend), AsyncCallback, nil);
  end;
end;

procedure TForm1.AsyncCallback(const ASyncResult: IAsyncResult);
var
  BytesReceived: Integer;
  ReceivedData: TBytes;
begin
  try
    BytesReceived := FSocket.EndReceive(ASyncResult);
    if BytesReceived > 0 then
    begin
      SetLength(ReceivedData, BytesReceived);
      FSocket.ReceiveBuffer.Read(ReceivedData, 0, BytesReceived);
      TThread.Queue(nil,
        procedure
        begin
          DisplayReceivedData(ReceivedData);
        end);
    end;
  except
    on E: Exception do
    begin
      TThread.Queue(nil,
        procedure
        begin
          MemoLog.Lines.Add('Error: ' + E.Message);
        end);
    end;
  end;
end;

procedure TForm1.DisplayReceivedData(const AData: TBytes);
begin
  MemoLog.Lines.Add('Received: ' + TEncoding.UTF8.GetString(AData));
end;

end.
Thank you Union have you tried to compile the code ?
  Mit Zitat antworten Zitat
Benutzerbild von Union
Union

Registriert seit: 18. Mär 2004
Ort: Luxembourg
3.492 Beiträge
 
Delphi 7 Enterprise
 
#6

AW: System.Net.Sockets

  Alt 4. Jul 2023, 13:13
No, didn't try. But the code generated could be a starting point to build on.
Ibi fas ubi proxima merces
sudo /Developer/Library/uninstall-devtools --mode=all
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:28 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz