unit Unit1;
//version 29 Oct 03

//This should connect via TCP/IP to a small webserver on my LAN. That
//   device currently returns a page of HTML to the Opera webbrowser
//   just fine, but refuses to accept requests from this code.
//At the moment, when Opera uses the server, the server console says
//   nothing, but when my programm tries to connect (even before it
//   tries to send), the console reports:'ServerSocket accept failure'.
//Any ideas as to why would be very welcome!!

//A "bare bones" version for taking to gurus for help.
//I would not normally use global variables quite so much.

//At the moment, the program fails to connect. If you know that a simple
//   Windsock Send / Recv isn't going to work, even if I achieve connection,
//   warning would be welcome! Maybe I need an Async type interaction?

//Environment: Delphi 2.0 Developer, Win 98

//(I don't think it is the source of my current barrier to success, but
// I am new to using buffers to pass strings. In Send, szMsg, sizeof, etc,
// may need work, and similar comments apply to bits of RecV)
interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,Winsock,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    WSData:TWSAData;
    liOkay:longint;
    Sock:TSocket;
    Sin:TSockAddrIn;
    //Some code fragments on the web use TSockAddrIn fields,
    //   others use TSockAddr (no "In") fields. Are both
    //   still important in different circumstances, or
    //   has TSockAddrIn taken over?
    procedure CreateSocket;
    procedure ConnectToServer;
    procedure SendReq;
    procedure RecVAndUseAns;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
{$R+}

procedure TForm1.CreateSocket;
begin
Sock:=Socket(AF_INET,Sock_Stream,0);
//Can anyone tell me if there is any difference between AP_INET and PF_INET?
//Both compile, both are seen in examples on the web.
if Sock=Invalid_Socket then begin
  showmessage('Failed in Create Socket');
  exit;
  end;
end;

procedure TForm1.ConnectToServer;
const BufLen=sizeof(TSockAddrIn);
begin
Sin.sin_family:=AF_INET;
Sin.sin_port:=htons(80);//I'm pretty sure this is right port
Sin.sin_addr.s_addr:=inet_addr('192.168.2.148');
liOkay:=Connect(Sock,Sin,BufLen);
if liOkay=Socket_Error then begin
  showmessage('Failed in Connect');
  exit;
  end;
end;

procedure TForm1.SendReq;
var szMsg:pchar;
begin
//Don't worry too much about this yet... I'm still
// getting errors at the "Connect" phase... but if you spot
// errors, the news would be welcome!
szMsg:='GET index.html HTTP/1.0'+chr(13)+chr(10)+
    'Host: TINI'+chr(13)+chr(10)+
    'Connection: close'+chr(13)+chr(10)+
    'Accept: */*'+chr(13)+chr(10)+
    chr(13)+chr(10);
Send(Sock,szMsg,sizeof(szMsg),0);
if liOkay=Socket_Error then begin
  showmessage('Failed in SendMsg');
  exit;
  end;
end;

procedure TForm1.RecvAndUseAns;
//Eventually, all I hope for is that within this
//procedure I will get the HTML into a memo
//Obviously that isn't my final objective, but if
//you can help me get that far, I can do the rest!
//(And re-write this in a better style, too!)
//--- I will also be making the code more flexible,
//of course, instead of limiting the read to 1000 chars
var baBuff:array[0..1000] of byte;
begin
liOkay:=Recv(Sock,baBuff,1000,0);
showmessage('Bytes received:'+inttostr(liOkay));
  //... and then use the data collected... (To be written)
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
liOkay:=WSAStartup($202,WSData);
if liOkay<>0 then begin
  showmessage('WSStartup failed');
  application.terminate;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
WSACleanup();
end;

procedure TForm1.Button1Click(Sender: TObject);
//Each routine has error reporting and an exit
begin
Button1.enabled:=false;
  //In this crude version, the program should be shut down
  //after one use of the button. (Socket still connected)
CreateSocket;
ConnectToServer;
SendReq;
RecvAndUseAns;
end;

end.