c-cko socket(), HTTP, POST poziadavok

www www na inmail.sk
Neděle Únor 3 09:01:43 CET 2002


Chcem si vyskusat ako pracuje HTTP prehliadac.
Nainstalovanu mam distribuciu RH 6.2.
Web-server Apache a PHP.
Napisal som jednoduche skripty index.php3 a odpoved.php3 :

=======index.php3===============
<html>
<FORM ACTION="odpoved.php3" METHOD=POST>
Meno:<INPUT TYPE=TEXT name=meno>
</FORM>
</html>
=============================

=======odpoved.php3============
<html>
Ty si <? echo $meno ?>
</html>
=============================

Vytvoril som program v c-cku...
=======skuska.c================
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdarg.h>

#define PORT  80

main()
{
  int sock;
  char odoslat[100];
  char server[100];
  char prijem[600];
  char buf[1024];

  struct sockaddr_in sin;
  struct hostent *hp;

  strcpy(server, "localhost");
  hp = gethostbyname(server);
  if (hp==NULL) {
    printf("Unknown host: %s\n",server);
    exit(0);
  }

  bzero((char*) &sin, sizeof(sin));
  bcopy(hp->h_addr, (char *) &sin.sin_addr, hp->h_length);
  sin.sin_family = hp->h_addrtype;
  sin.sin_port = htons(PORT);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  connect(sock,(struct sockaddr *) &sin, sizeof(sin));

  strcpy(odoslat, "meno=Martin");

  sprintf(buf,"POST http://localhost/index.php3 HTTP/1.1\r\n");
  send(sock, buf, strlen(buf),0);
  sprintf(buf,"User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows
NT)\r\n");
  send(sock, buf, strlen(buf),0);
  sprintf(buf,"Host: %s\r\n", server);
  send(sock, buf, strlen(buf),0);
  sprintf(buf,"Accept-Charset: iso-8859-2, windows-1250; q=0.8, us-ascii;
q=0.2\r\n");
  send(sock, buf, strlen(buf),0);
  sprintf(buf,"Content-type: text/html\r\n");
  send(sock, buf, strlen(buf),0);
  sprintf(buf,"Content-length: %d\r\n", strlen(odoslat));
  send(sock, buf, strlen(buf),0);
  sprintf(buf,"\r\n");
  send(sock, buf, strlen(buf),0);
  sprintf(buf,"%s\r\n", odoslat);
  send(sock, buf, strlen(buf),0);
  read(sock,prijem, 550, 0);
  printf("%s\n", prijem);
}
============================================================

Pri spusteni programu mi dostanem od web serveru takuto odpoved:
============================================================
HTTP/1.1 200 OK
Date: Thu, 31 Jan 2002 13:05:00 GMT
Server: Apache/1.3.12 (Unix)  (Red Hat/Linux) mod_czech/3.1.0 PHP/3.0.16
X-Powered-By: PHP/3.0.16
Vary: User-Agent, Accept-Charset
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-2

60
<html>
<FORM ACTION="odpoved.php3" METHOD=POST>
Meno:<INPUT TYPE=TEXT name=meno>
</FORM>
</html>
0

9@ř9@
========================================================


Ocakaval som, ze ako odpoved mi nacita skript odpoved.php3 aj so zadanym
menom Martin.


Mozete mi poradit preco to tak nie je, alebo ako to pracuje?
Dik.











Další informace o konferenci Linux