Re: Sockets programming in Linux

Hairy Pixels via fpc-pascal <[email protected]>
Newsgroups gmane.comp.compilers.free-pascal.general
Message-ID <CAGsUGtneNMWZik925+QnKiYofgOKzd9fxZoQUf1nv6SnAm5Edw@mail.gmail.com>
Here’s what ChatGPT says. It just used the UNIX sockets API so nothing
different from what you’d see in C. Looks pretty much correct to me but I
didn’t try to build it.

program TCPClient;

uses
  SysUtils, Sockets;

const
  HOST = '127.0.0.1';
  PORT = 12345;

var
  ClientSocket: LongInt;
  Address: TInetSockAddr;
  Buffer: array[0..1023] of Char;
  BytesReceived: LongInt;
begin
  // Create a socket
  ClientSocket := fpSocket(AF_INET, SOCK_STREAM, 0);
  if ClientSocket = -1 then
  begin
    WriteLn('Error creating client socket.');
    Halt(1);
  end;

  // Connect to the server
  FillChar(Address, SizeOf(Address), 0);
  Address.sin_family := AF_INET;
  Address.sin_port := htons(PORT);
  Address.sin_addr := StrToHostAddr(HOST);

  if fpConnect(ClientSocket, @Address, SizeOf(Address)) = -1 then
  begin
    WriteLn('Error connecting to server.');
    Halt(1);
  end;

  WriteLn('Connected to server.');

  // Send data to the server
  fpSend(ClientSocket, 'Hello from the client!', 23, 0);

  // Receive response from the server
  BytesReceived := fpRecv(ClientSocket, @Buffer, SizeOf(Buffer) - 1, 0);
  if BytesReceived > 0 then
  begin
    Buffer[BytesReceived] := #0; // Null-terminate the string
    WriteLn('Received: ', Buffer);
  end;

  // Close the socket
  fpClose(ClientSocket);
end.

program TCPServer;

uses
  SysUtils, Sockets;

const
  PORT = 12345;

var
  ServerSocket, ClientSocket: LongInt;
  Address: TInetSockAddr;
  Buffer: array[0..1023] of Char;
  BytesReceived: LongInt;
begin
  // Create a socket
  ServerSocket := fpSocket(AF_INET, SOCK_STREAM, 0);
  if ServerSocket = -1 then
  begin
    WriteLn('Error creating server socket.');
    Halt(1);
  end;

  // Bind the socket to a port
  FillChar(Address, SizeOf(Address), 0);
  Address.sin_family := AF_INET;
  Address.sin_port := htons(PORT);
  Address.sin_addr := htonl(INADDR_ANY);

  if fpBind(ServerSocket, @Address, SizeOf(Address)) = -1 then
  begin
    WriteLn('Error binding server socket.');
    Halt(1);
  end;

  // Start listening for incoming connections
  if fpListen(ServerSocket, 5) = -1 then
  begin
    WriteLn('Error listening on server socket.');
    Halt(1);
  end;

  WriteLn('Server is running on port ', PORT, '. Waiting for
connections...');

  // Accept a connection
  ClientSocket := fpAccept(ServerSocket, nil, nil);
  if ClientSocket = -1 then
  begin
    WriteLn('Error accepting connection.');
    Halt(1);
  end;

  WriteLn('Client connected.');

  // Receive data from the client
  BytesReceived := fpRecv(ClientSocket, @Buffer, SizeOf(Buffer) - 1, 0);
  if BytesReceived > 0 then
  begin
    Buffer[BytesReceived] := #0; // Null-terminate the string
    WriteLn('Received: ', Buffer);
  end;

  // Send a response to the client
  fpSend(ClientSocket, 'Hello from the server!', 23, 0);

  // Close sockets
  fpClose(ClientSocket);
  fpClose(ServerSocket);
end.

Regards,
    Ryan Joseph


On Jan 11, 2025 at 7:46:50 PM, Duke Normandin via fpc-pascal <
[email protected]> wrote:

> I’ve Googled my butt off looking for fpc/pascal examples
> /tutorials on writing a simple tcp client and server. NO JOY! Anybody got
> any ideas? TIA
> —
> Duke
> Sent from my iPhone
> _______________________________________________
> fpc-pascal maillist  -  [email protected]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

_______________________________________________
fpc-pascal maillist  -  [email protected]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.