Difference between revisions of "Networking"

From Free Pascal wiki
Jump to navigationJump to search
(→‎WebServices: Added RSS example)
(Fix typo)
(11 intermediate revisions by 2 users not shown)
Line 4: Line 4:
  
 
__TOC__
 
__TOC__
{{Web and Networking Programming}}
 
  
 
== TCP/IP Protocol ==
 
== TCP/IP Protocol ==
  
{{Note|Because there are multiple libraries (Synapse, lnet, fphttpclient, Indy,...) that provide networking functionality for FPC/Lazarus, many examples can be written for multiple libraries. Therefore you may see the same examples multiple times - for different libraries. Have a look at the [[Brook_for_Free_Pascal|Brook for Free Pascal]] or [https://web.archive.org/web/20170613212428/https://bitbucket.org/reiniero/fpctwit fpctwit] (Internet Archive; original link is dead) frameworks for examples on how to build frameworks that can use multiple network libraries}}
+
{{Note|Because there are multiple libraries (Synapse, lnet, fphttpclient, Indy,...) that provide networking functionality for FPC/Lazarus, many examples can be written for multiple libraries. Therefore you may see the same examples multiple times - for different libraries. Have a look at the [[Brook_for_Free_Pascal|Brook for Free Pascal]] or [https://bitbucket.org/mararosas/fpctwit/src/default/ fpctwit] frameworks for examples on how to build frameworks that can use multiple network libraries}}
  
 
=== CGI/FastCGI - REST, CRUD, chat, blog, web page etc. ===
 
=== CGI/FastCGI - REST, CRUD, chat, blog, web page etc. ===
Line 20: Line 19:
 
=== Webserver example ===
 
=== Webserver example ===
  
Given below is an example http server written with [[Synapse]] and tested in Win XP and Mac OS X, after changing the Synapse source to use a fixed constant $20000 as MSG_NOSIGNAL, because this constant isn't present in the sockets unit in Mac OS X. ''Note: please send your bug reports to the Synapse project - they are quite responsive - so everybody profits from improvements. To do: check if this modification is still needed for current (Oct 2012) Synapse''
+
Given below is an example http server written with [[Synapse]] and tested in Win XP and macOS, after changing the Synapse source to use a fixed constant $20000 as MSG_NOSIGNAL, because this constant isn't present in the sockets unit in macOS. ''Note: please send your bug reports to the Synapse project - they are quite responsive - so everybody profits from improvements. To do: check if this modification is still needed for current (Oct 2012) Synapse''
  
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
Line 143: Line 142:
 
These examples (using the FPC/Lazarus built in fphttpclient) can be found at [[fphttpclient#Examples]]
 
These examples (using the FPC/Lazarus built in fphttpclient) can be found at [[fphttpclient#Examples]]
  
== WebServices ==
+
=== LazWebsockets ===
 +
 
 +
A WebSocket is a persistent connection between a client and server. WebSockets provide a bidirectional, full-duplex communications channel that operates over HTTP through a single TCP/IP socket connection. At its core, the WebSocket protocol facilitates message passing between a client and server. [https://github.com/Warfley/LazWebsockets LazWebsockets] provides a small Websocket server and client implementation written for the FPC and Lazarus. It is based on the FCL ssockets unit and therefore independent of any additional dependencies. It can be easily built using only the FPC without Lazarus. There is a chat server and chat client example demo.
 +
 
 +
== Web Services ==
  
 
According to the [http://www.w3.org/ W3C] a Web service is a software system designed to support interoperable machine-to-machine interaction over a network. It has an interface that is described in a machine-processable format such as WSDL. Other systems interact with the Web service in a manner prescribed by its interface using messages, which may be enclosed in a SOAP envelope, or follow a REST approach. These messages are typically conveyed using HTTP, and are normally comprised of XML in conjunction with other Web-related standards. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer. This interoperability (e.g., between Windows and Linux applications) is due to the use of open standards. OASIS and the W3C are the primary committees responsible for the architecture and standardization of web services. To improve interoperability between web service implementations, the WS-I organisation has been developing a series of profiles to further define the standards involved.
 
According to the [http://www.w3.org/ W3C] a Web service is a software system designed to support interoperable machine-to-machine interaction over a network. It has an interface that is described in a machine-processable format such as WSDL. Other systems interact with the Web service in a manner prescribed by its interface using messages, which may be enclosed in a SOAP envelope, or follow a REST approach. These messages are typically conveyed using HTTP, and are normally comprised of XML in conjunction with other Web-related standards. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer. This interoperability (e.g., between Windows and Linux applications) is due to the use of open standards. OASIS and the W3C are the primary committees responsible for the architecture and standardization of web services. To improve interoperability between web service implementations, the WS-I organisation has been developing a series of profiles to further define the standards involved.
Line 153: Line 156:
 
=== Web Service Toolkit for FPC & Lazarus ===
 
=== Web Service Toolkit for FPC & Lazarus ===
 
[[Web Service Toolkit]] is a web services package for FPC and Lazarus.
 
[[Web Service Toolkit]] is a web services package for FPC and Lazarus.
 +
 +
=== Using Google Translate ===
 +
 +
For an example of using the Google translation service, see the article [[Using Google Translate]] which employs the [[fphttpclient]] component from the [[fcl-web]] package.
  
 
== See also ==
 
== See also ==
 +
 
* [[Portal:Web Development|Web Development Portal]]
 
* [[Portal:Web Development|Web Development Portal]]
* [[Networking libraries]] - comparison of various networking libraries
+
* [[Brook for Free Pascal]] - The perfect Free Pascal framework for your web applications. It's pure Pascal. You don't need to leave your preferred programming language.
* [[Brook Framework]] - The perfect Free Pascal framework for your web applications. It's pure Pascal. You don't need to leave your preferred programming language.
 
* [http://www.fastplaz.com FastPlaz] - Fast Web Framework for pascal. More features like theme/templating, simple Model, session, mailer, etc.
 
* [[Sockets]] - TCP/IP Sockets components
 
 
* [[fcl-net]] - Networking library supplied with FPC
 
* [[fcl-net]] - Networking library supplied with FPC
 +
* [[fcl-web]] - Also known as fpWeb, this is a library to develop web applications which can be deployed as cgi, fastcgi or apache modules.
 +
* [[fpbrowser]] - web browser written in FPC + Lazarus.
 
* [[Indy_with_Lazarus]] - Networking Components; How to install
 
* [[Indy_with_Lazarus]] - Networking Components; How to install
 +
* [[Internet Tools]] - A wrapper around Synapse/WinInet/Android's HTTP components simplifying HTTPS and redirections, and a XPath/XQuery/CSS Selector/JSONiq engine to process the downloaded pages
 +
* [[FPC and Apache Modules]]
 
* [[lNet]] - Lightweight Networking Components
 
* [[lNet]] - Lightweight Networking Components
 +
* [[Networking libraries]] - comparison of various networking libraries
 +
* [[OpenURL]] - opens a URL with the default/registered/preferred web browser as specified by the operating system.
 +
* [[Secure programming | Secure Programming]]
 +
* [[Sockets]] - TCP/IP Sockets components
 
* [[Synapse]] - Serial port and synchronous TCP/IP Library
 
* [[Synapse]] - Serial port and synchronous TCP/IP Library
 
* [[XML Tutorial]] - XML is often utilized on network communications
 
* [[XML Tutorial]] - XML is often utilized on network communications
* [[FPC and Apache Modules]]
+
 
* [[fcl-web]] - Also known as fpWeb, this is a library to develop web applications which can be deployed as cgi, fastcgi or apache modules.
+
=== macOS ===
* [[Secure programming | Secure Programming]]
+
 
* [[Internet Tools]] - A wrapper around Synapse/wininet/Android's httpcomponents simplifying https and redirections, and a XPath/XQuery/CSS Selector/JSONiq engine to process the downloaded pages
+
* [[macOS NSURLConnection]] This API lets you load the contents of a URL via HTTP and HTTPS by providing a URL request object.
 +
* [[macOS NSURLSession]] This class and related classes provide an API for downloading content via HTTP and HTTPS.  
 +
* [[macOS Open Sesame|macOS open command]] Opens a URL in the default web browser.
 +
 
 +
=== Windows ===
 +
 
 +
* [[Windows Programming Tips#Using Windows native wininet_for_web_retrieval|WinInet]] (Windows Internet) API enables your application to interact with FTP and HTTP protocols to access Internet resources.
 +
 
 +
== External links ==
 +
 
 
* [https://fanoframework.github.io Fano Framework] - Web application framework written in Free Pascal.
 
* [https://fanoframework.github.io Fano Framework] - Web application framework written in Free Pascal.
 
+
* [http://www.fastplaz.com FastPlaz] - Fast Web Framework for pascal. More features like theme/templating, simple Model, session, mailer, etc.
[[Category:Tutorials]]
 
[[Category:Networking]]
 
[[Category:FPC]]
 
[[Category:Lazarus]]
 

Revision as of 04:31, 1 April 2021

Deutsch (de) English (en) español (es) français (fr) 日本語 (ja) 한국어 (ko) polski (pl) português (pt) русский (ru) slovenčina (sk) 中文(中国大陆)‎ (zh_CN)

This page contains both tutorials/code and information with regard to network programming with Lazarus as well as plain FPC.

TCP/IP Protocol

Light bulb  Note: Because there are multiple libraries (Synapse, lnet, fphttpclient, Indy,...) that provide networking functionality for FPC/Lazarus, many examples can be written for multiple libraries. Therefore you may see the same examples multiple times - for different libraries. Have a look at the Brook for Free Pascal or fpctwit frameworks for examples on how to build frameworks that can use multiple network libraries

CGI/FastCGI - REST, CRUD, chat, blog, web page etc.

These functionalities can be used with fcl-web. They are also built into the Brook framework. Please have a look at this page. https://github.com/silvioprog/brookframework

SSH/Telnet client, sending emails, downloading files, OAuthv1 examples

See the Synapse page.

Webserver example

Given below is an example http server written with Synapse and tested in Win XP and macOS, after changing the Synapse source to use a fixed constant $20000 as MSG_NOSIGNAL, because this constant isn't present in the sockets unit in macOS. Note: please send your bug reports to the Synapse project - they are quite responsive - so everybody profits from improvements. To do: check if this modification is still needed for current (Oct 2012) Synapse

{
  The Micro Pascal WebServer

  This is a very simple example webserver implemented with the Synapse library.

  It works with blocking sockets and a single thread, so it
  can only handle one request at a given time.

  It will write the headers that it receives from the browser
  to the standard output.

  It serves a fixed webpage for the / URI
  For any other URI it will return 404 not found
}

program upserver;

{$ifdef fpc}
  {$mode delphi}
{$endif}

{$apptype console}

uses
  Classes, blcksock, sockets, Synautil, SysUtils;

{@@
  Attends a connection. Reads the headers and gives an
  appropriate response
}

procedure AttendConnection(ASocket: TTCPBlockSocket);
var
  timeout: integer;
  s: string;
  method, uri, protocol: string;
  OutputDataString: string;
  ResultCode: integer;
begin
  timeout := 120000;

  WriteLn('Received headers+document from browser:');

  //read request line
  s := ASocket.RecvString(timeout);
  WriteLn(s);
  method := fetch(s, ' ');
  uri := fetch(s, ' ');
  protocol := fetch(s, ' ');

  //read request headers
  repeat
    s := ASocket.RecvString(Timeout);
    WriteLn(s);
  until s = '';

  // Now write the document to the output stream

  if uri = '/' then
  begin

    // Write the output document to the stream
    OutputDataString :=
      '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
      + ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + CRLF
      + '<html><h1>Teste</h1></html>' + CRLF;

    // Write the headers back to the client
    ASocket.SendString('HTTP/1.0 200' + CRLF);
    ASocket.SendString('Content-type: Text/Html' + CRLF);
    ASocket.SendString('Content-length: ' + IntTostr(Length(OutputDataString)) + CRLF);
    ASocket.SendString('Connection: close' + CRLF);
    ASocket.SendString('Date: ' + Rfc822DateTime(now) + CRLF);
    ASocket.SendString('Server: Servidor do Felipe usando Synapse' + CRLF);
    ASocket.SendString('' + CRLF);

  //  if ASocket.lasterror <> 0 then HandleError;

    // Write the document back to the browser
    ASocket.SendString(OutputDataString);
  end
  else
    ASocket.SendString('HTTP/1.0 404' + CRLF);
end;

var
  ListenerSocket, ConnectionSocket: TTCPBlockSocket;

begin
  ListenerSocket := TTCPBlockSocket.Create;
  ConnectionSocket := TTCPBlockSocket.Create;

  ListenerSocket.CreateSocket;
  ListenerSocket.setLinger(true,10);
  ListenerSocket.bind('0.0.0.0','1500');
  ListenerSocket.listen;

  repeat
    if ListenerSocket.canread(1000) then
    begin
      ConnectionSocket.Socket := ListenerSocket.accept;
      WriteLn('Attending Connection. Error code (0=Success): ', ConnectionSocket.lasterror);
      AttendConnection(ConnectionSocket);
      ConnectionSocket.CloseSocket;
    end;
  until false;

  ListenerSocket.Free;
  ConnectionSocket.Free;
end.

TCP/IP Client - Server Example using built-in FCL-Net components

Please have a look at this blog post.

Download HTTP page body, web server upload using POST, get external IP address

These examples (using the FPC/Lazarus built in fphttpclient) can be found at fphttpclient#Examples

LazWebsockets

A WebSocket is a persistent connection between a client and server. WebSockets provide a bidirectional, full-duplex communications channel that operates over HTTP through a single TCP/IP socket connection. At its core, the WebSocket protocol facilitates message passing between a client and server. LazWebsockets provides a small Websocket server and client implementation written for the FPC and Lazarus. It is based on the FCL ssockets unit and therefore independent of any additional dependencies. It can be easily built using only the FPC without Lazarus. There is a chat server and chat client example demo.

Web Services

According to the W3C a Web service is a software system designed to support interoperable machine-to-machine interaction over a network. It has an interface that is described in a machine-processable format such as WSDL. Other systems interact with the Web service in a manner prescribed by its interface using messages, which may be enclosed in a SOAP envelope, or follow a REST approach. These messages are typically conveyed using HTTP, and are normally comprised of XML in conjunction with other Web-related standards. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer. This interoperability (e.g., between Windows and Linux applications) is due to the use of open standards. OASIS and the W3C are the primary committees responsible for the architecture and standardization of web services. To improve interoperability between web service implementations, the WS-I organisation has been developing a series of profiles to further define the standards involved.

RSS example

For an RSS example, see this article RSS Unit.

Web Service Toolkit for FPC & Lazarus

Web Service Toolkit is a web services package for FPC and Lazarus.

Using Google Translate

For an example of using the Google translation service, see the article Using Google Translate which employs the fphttpclient component from the fcl-web package.

See also

  • Web Development Portal
  • Brook for Free Pascal - The perfect Free Pascal framework for your web applications. It's pure Pascal. You don't need to leave your preferred programming language.
  • fcl-net - Networking library supplied with FPC
  • fcl-web - Also known as fpWeb, this is a library to develop web applications which can be deployed as cgi, fastcgi or apache modules.
  • fpbrowser - web browser written in FPC + Lazarus.
  • Indy_with_Lazarus - Networking Components; How to install
  • Internet Tools - A wrapper around Synapse/WinInet/Android's HTTP components simplifying HTTPS and redirections, and a XPath/XQuery/CSS Selector/JSONiq engine to process the downloaded pages
  • FPC and Apache Modules
  • lNet - Lightweight Networking Components
  • Networking libraries - comparison of various networking libraries
  • OpenURL - opens a URL with the default/registered/preferred web browser as specified by the operating system.
  • Secure Programming
  • Sockets - TCP/IP Sockets components
  • Synapse - Serial port and synchronous TCP/IP Library
  • XML Tutorial - XML is often utilized on network communications

macOS

  • macOS NSURLConnection This API lets you load the contents of a URL via HTTP and HTTPS by providing a URL request object.
  • macOS NSURLSession This class and related classes provide an API for downloading content via HTTP and HTTPS.
  • macOS open command Opens a URL in the default web browser.

Windows

  • WinInet (Windows Internet) API enables your application to interact with FTP and HTTP protocols to access Internet resources.

External links

  • Fano Framework - Web application framework written in Free Pascal.
  • FastPlaz - Fast Web Framework for pascal. More features like theme/templating, simple Model, session, mailer, etc.