最简单范例。
分你自己留着吧。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TForm1 = class(TForm)
ServerSocket: TServerSocket;
btnStart: TButton;
btnStop: TButton;
procedure btnStartClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.btnStartClick(Sender: TObject);
begin
ServerSocket.Open;
end;
procedure TForm1.btnStopClick(Sender: TObject);
begin
ServerSocket.Close;
end;
procedure TForm1.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
Var
s,rep:String;
i,j:Byte;
begin
rep:='<html><body><font face="宋体" size="小五" color="red">Well Done!正常</font><br>No page found<br></body></html>';
s:=Socket.ReceiveText;
For i:=1 to Length(s) do s:=UpCase(s);
If Copy(s,1,3)='GET' Then
Begin
Socket.SendText('HTTP/1.1 200 OK'#$0D#$0A);
Socket.SendText('MIME-version: 1.0'#$0D#$0A);
Socket.SendText('Content-Length: '+InttoStr(Length(rep))+#$0D#$0A);
Socket.SendText('Content-Type: text/html'#$0D#$0A);
Socket.SendText(#$0D#$0A);
for j:=1 to length(rep) do
begin
Socket.SendText(rep[j]);
//sleep(500);
end;
Socket.Close;
End;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
If ServerSocket.Active Then ServerSocket.Close;
end;
end.