Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 8155|回复: 7

一个官方的TCP/IP协议的例子

[复制链接]

该用户从未签到

发表于 2009-2-16 10:53:47 | 显示全部楼层 |阅读模式
TCP/IP Protocol
[edit] 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.



{
  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;

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.
回复

使用道具 举报

该用户从未签到

发表于 2009-6-20 22:44:26 | 显示全部楼层
收藏了
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-4-22 13:24:27 | 显示全部楼层
收藏
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-5-21 00:10:42 | 显示全部楼层
收藏了
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-8-24 23:46:06 | 显示全部楼层
收下了,谢谢你搂主.

回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-10-30 16:24:42 | 显示全部楼层
收下了,谢谢搂主.
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-6-15 22:51:27 | 显示全部楼层
收下了,谢谢你搂主.
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-6-17 09:30:22 | 显示全部楼层
学习了,谢谢楼主
回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-11-17 01:36 , Processed in 0.028319 second(s), 10 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表