C 언어를 이용하여 인터넷에 있는 html 문서를 읽어올라 하는데

3dollar의 이미지

리눅스 상에서 C 언어를 이용하여 인터넷에 있는 html 문서를
읽어올라 하는데요....

C 로는 내부 프로그램만 짜봐서 그런지..
네트워크로 문서를 다운하고 이런 건 함수나 헤더화일등을 모르겠네요...

도움이 되는 사이트나 사용하는 헤더 화일등 그런것 있으면 좀 알려주시면
감사하겠습니다..^^

맹고이의 이미지

간단한 예제입니다.

#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#define MAX_BUFFER 10000 

void get_home_page(int socket_fd) {
   char buffer[MAX_BUFFER];
   ssize_t number_characters_read;

   /* Send the HTTP GET command for the home page. */
   sprintf(buffer, "GET /\n");
   write(socket_fd, buffer, strlen(buffer));

   while(1) {
      number_characters_read = read(socket_fd, buffer, MAX_BUFFER);
      if(number_characters_read == 0)
         return;
      fwrite(buffer, sizeof(char), number_characters_read, stdout);
   }
}

int main(int argc, char *argv[]) {
   int socket_fd;
   struct sockaddr_in name;
   struct hostent *hostinfo;

   /* Create socket. */
   socket_fd = socket(AF_INET, SOCK_STREAM, 0);

   name.sin_family = AF_INET;
   hostinfo = gethostbyname(argv[1]);

   if(hostinfo == NULL)
      return 1;
   else
      name.sin_addr = *((struct in_addr *)hostinfo->h_addr);
   name.sin_port = htons(80);

   /* Connect to web server. */
   if(connect(socket_fd, (struct sockaddr *)&name, sizeof(struct sockaddr)) == -1) {
      perror("connect");
      return 1;
   }

   /* Retrieve the server's home page. */
   get_home_page(socket_fd);

   return 0;
}
nthroot의 이미지

------식은이 처------
길이 끝나는 저기엔 아무 것도 없어요. 희망이고 나발이고 아무 것도 없어.