Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2025-01

Sockets programming in Linux

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Duke Normandin

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

Reply by email

n7800
to Duke Normandin

For example, you might find the Synapse package useful:  https://wiki.freepascal.org/Synapse  
 
 
 
 

Show quoted text 10 lines
Воскресенье, 12 января 2025, 14:04 +05:00 от Duke Normandin via fpc-pascal :   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 - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

 

Reply by email

Hairy Pixels
to Duke Normandin: I’ve Googled my butt off looking for fpc/pascal examples /tutorials on writing a simple…

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 <

Show quoted text 12 lines
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 - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Dennis
to Duke Normandin: I’ve Googled my butt off looking for fpc/pascal examples /tutorials on writing a simple…
Duke Normandin wrote 8 lines
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 https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

I used

https://github.com/geby/synapse

which is cross platform.

Dennis

Reply by email

Duke Normandin
to Dennis

Thx - I’ll have a look at it

Show quoted text 19 lines
On Jan 20, 2025, at 4:19 AM, Dennis via fpc-pascal wrote:  On 11/01/2025 8:46 pm, Duke Normandin via fpc-pascal 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 - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal I used https://github.com/geby/synapse which is cross platform. Dennis

Reply by email