Unix Socket

Alle Fragen zur Netzwerkkommunikation
Antworten
Acia6850
Beiträge: 50
Registriert: Mo 9. Okt 2023, 18:45
OS, Lazarus, FPC: Windows + WSL / Linux Debian Rasbian OS (L 3.0.0 FPC 3.3.2)
CPU-Target: 64Bit
Wohnort: LK Ludwigsburg

Unix Socket

Beitrag von Acia6850 »

Hallo hat schon jemand was in Lazarus mit Unix Sockets gemacht AF-Unix mit einem Unix Pfad ?

In Python sieht es so aus :
SOCKET_PATH = "/var/run/arduino-router.sock"

with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
client.connect(SOCKET_PATH)

Mathias
Beiträge: 7178
Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz

Re: Unix Socket

Beitrag von Mathias »

Mit g_socket habe ich mal etwas probiert:

Code: Alles auswählen

program project1;

uses
  fp_glib2;

  procedure main;
  const
    host = 'example.com';
    req: pchar = 'GET / HTTP/1.0'#13#10'Host: example.com'#13#10#13#10;
    port = 443;
    BUF_SIZE = 64;
  var
    client: PGSocketClient;
    connection: PGSocketConnection;
    err: PGError = nil;
    ostream: PGOutputStream;
    istream: PGInputStream;
    n: Tgssize;
    buf: array[0..BUF_SIZE - 1] of char;
  begin
    client := g_socket_client_new;
    g_socket_client_set_tls(client, gTrue);

    connection := g_socket_client_connect_to_host(client, host, port, nil, @err);
    if connection = nil then begin
      g_printerr('Verbindung fehlgeschlagen: %s'#10, err^.message);
      g_error_free(err);
      g_object_unref(client);
      Exit;
    end;

    ostream := g_io_stream_get_output_stream(G_IO_STREAM(connection));
    g_output_stream_write(ostream, req, strlen(req), nil, @err);

    istream := g_io_stream_get_input_stream(G_IO_STREAM(connection));
    repeat
      n := g_input_stream_read(istream, @buf, BUF_SIZE - 1, nil, @err);
      if n > 0 then begin
        buf[n] := #0;
        g_print('%s', @buf[0]);
      end else if err <> nil then begin
        g_printerr('Fehler beim Lesen: %s', err^.message);
        g_error_free(err);
        err := nil;
      end;
    until n <= 0;
    g_object_unref(connection);
    g_object_unref(client);
  end;


begin
  main;
end.
Ausgabe:

Code: Alles auswählen

TTP/1.1 200 OK
Date: Tue, 03 Feb 2026 18:29:37 GMT
Content-Type: text/html
Connection: close
CF-RAY: 9c83feae0fc8931a-ZRH
last-modified: Fri, 30 Jan 2026 05:47:18 GMT
allow: GET, HEAD
Accept-Ranges: bytes
Age: 11533
cf-cache-status: HIT
Server: cloudflare

<!doctype html><html lang="en"><head><title>Example Domain</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>body{background:#eee;width:60vw;margin:15vh auto;font-family:system-ui,sans-serif}h1{font-size:1.5em}div{opacity:0.8}a:link,a:visited{color:#348}</style><body><div><h1>Example Domain</h1><p>This domain is for use in documentation examples without needing permission. Avoid use in operations.<p><a href="https://iana.org/domains/example">Learn more</a></div></body></html>
Vielleicht hilf dir dies weiter.
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot

Warf
Beiträge: 2251
Registriert: Di 23. Sep 2014, 17:46
OS, Lazarus, FPC: Win10 | Linux
CPU-Target: x86_64

Re: Unix Socket

Beitrag von Warf »

Du kannst entweder die Berkley Sockets API direkt verwenden (Unit Sockets), bzw. auf Main/Trunk die entsprechenden high-level wrapper funktionen (Unit fpSockets), oder die Socket Stream Klasse TUnixSocket (unit ssockets) verwenden. Je nachdem was dir mehr gelegen ist

Antworten