Milan Roubal :c-cko socket(), HTTP, POST poziadavok

www www na inmail.sk
Pátek Únor 8 20:08:22 CET 2002


Chcem si vyskusat ako pracuje HTTP prehliadac.
 Nainstalovanu mam distribuciu RH 6.2.
 Web-server Apache a PHP.
 Napisal som jednoduchy skript odpoved.php3

 =======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-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\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);
}
 ============================================================

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

 Mozete mi poradit preco to tak nie je, alebo ako to (HTTP/1.1) pracuje?
 Dik.






Další informace o konferenci Linux