Difference between revisions of "Networking/ja"
Line 22: | Line 22: | ||
== TCP/IP Protocol == | == TCP/IP Protocol == | ||
− | == | + | === Webserver example === |
− | + | Bellow there is an example http server written with Synapse and tested in 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. | |
− | |||
− | + | <delphi> | |
− | + | { | |
+ | 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 504 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 504' + CRLF); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end; | end; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
var | var | ||
− | + | ListenerSocket, ConnectionSocket: TTCPBlockSocket; | |
− | |||
begin | 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); | ||
+ | end; | ||
+ | until false; | ||
− | + | ListenerSocket.Free; | |
− | + | ConnectionSocket.Free; | |
− | + | end. | |
− | + | </delphi> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | end | ||
− | </ | ||
== Webサービス == | == Webサービス == |
Revision as of 03:15, 15 February 2009
│
Deutsch (de) │
English (en) │
español (es) │
français (fr) │
日本語 (ja) │
한국어 (ko) │
polski (pl) │
português (pt) │
русский (ru) │
slovenčina (sk) │
中文(中国大陆) (zh_CN) │
このページは、Lazarusでのネットワークプログラムについてのチュートリアルを開始するページになるでしょう。 この記事に、リンクやセクション、ページ、あなたのWikiへのリンクを追加していってください。
(訳注:最初にこの元記事を書いた人はネットワークプログラミングの専門でなく、独学で勉強したことを書いているそうです。日本では、一般的にネットワークプログラミングというと、ソケット、サービス、クライアントサーバー関連の事を指す事が多いのですが、ここでは、XML、およびその派生の仕様によるWEBサービスでのネットワーキングの記事になっていますので、クライアントサーバー、ソケット関連の記事を期待している方は別途TCP/IPソケットコンポーネントや、軽量ネットワークコンポーネントを参照してください。)
このページには、一般的な情報があります。
その他のネットワークのチュートリアル
- セキュア・プログラミング
- Sockets - TCP/IP ソケットコンポーネント
- lNet - 軽量ネットワークコンポーネント
- XML Tutorial/ja - XML is often utilized on network communications
- FPC and Apache Modules
TCP/IP Protocol
Webserver example
Bellow there is an example http server written with Synapse and tested in 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.
<delphi> {
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 504 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>
Teste
</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 504' + 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); end; until false;
ListenerSocket.Free; ConnectionSocket.Free;
end. </delphi>
Webサービス
W3CによるWEBサービスはネットワークの上のマシンから、別のマシンへ、共通的に利用できるように設計されたソフトウェア・システムの名称です。 WEBサービスには、WSDLなど、「機械が処理可能」な形式で記述されているインタフェースがあります。 また、他のシステムは、多くの場合、インタフェースによってSOAPに内包されるメッセージを使用し定められた方法でウェブサービスと対話します。もしくは、RESTによる方法を取るかもしれません。 これらのメッセージは、通常HTTPを使用して伝達され、ウェブ関連の他の規格に関連するXMLから成りたっています。
様々なプログラミング言語で書かれ、様々なプラットホームで動くソフトウェアアプリケーションは、データをおたがいに一つのコンピュータでコミュニケーションを処理します。それと同様の方法を使って、インターネットのようなコンピュータネットワークの上でもコミュニケーションを行うために、WEBサービスを利用することができます。
この相互運用性(例えば、WindowsとLinuxアプリケーションの間など)はオープンスタンダードの運用によるものです。 OASISとW3Cはウェブサービスのアーキテクチャと標準化の策定に責任や権限がある主な委員会です。
ウェブサービス実装間の相互運用性を改良するため、WS-I委員会は、さらに定義する一連のプロフィールを規格の関連の開発をしています。
FPC & Lazarus のWeb Service Toolkit
Web Service Toolkit はFPCとLazarusのWEBサービスのパッケージです。
External Links
XML
- W3Schools Xml Tutorial