소켓으로 tar.gz 받을때 어케 하는거유..??ㅠ.ㅠ

mudori의 이미지

바이너리 받기 넘 힘드네요..
http 서버에서 c 소켓으로 tar.gz 파일 받는데

여기에서 걸리네요.

헤더 보내고 바로 받는건데.

fp=fopen("aaa.tar.gz", "wb");
while(numbytes=read(sock, buf, sizeof(buf))!=0) {
fwrite(buf, sizeof(buf), numbytes, fp);
printf("%s", buf);
}
fclose(fp);
close(sock);

이진 파일은 맞는데 gzip 파일이 아니고요..
그냥 받아오는 header도 저장해버리고...ㅠ.ㅠ

용량도 틀리고

아.. 미치겠습니다.
wget 소스 분석하기 넘 어렵고......

흑흑..

rfc 는 개념적이라 어렵고...

개으름의 극치인가봅니다. .. 고수님들......... 도와주셈..

익명 사용자의 이미지

mudori wrote:

fp=fopen("aaa.tar.gz", "wb");
while(numbytes=read(sock, buf, sizeof(buf))!=0) {
fwrite(buf, sizeof(buf), numbytes, fp);
printf("%s", buf);
}
fclose(fp);
close(sock);


음,.....물어본 만큼만 답변하겠소.

Quote:
#include <stdio.h>

size_t fwrite(const void *ptr, size_t size, size_t nitems,
FILE *stream);


fwrite(buf, sizeof(buf), numbytes,  fp);
==> 
fwrite(buf, numbytes, 1,  fp);

fp=fopen("aaa.tar.gz", "wb");
리눅스/유닉스는 모든 파일이 바이너리요. 윈XX랑은 틀리오.
유닉스/리눅스에서는 b라는 모드 자체가 없소이다.

익명 사용자의 이미지

그런데 왜 cURL을 안쓰는 것이요? 그거 내가 안만들었지만, 좋소이다.

mudori의 이미지

아는게 별루 없어................................ 무얼 물어야 하는지도 남감합니다.

rasungboy의 이미지

블럭킹 소켓인가요 논블럭인가요?

fp=fopen("aaa.tar.gz", "wb"); 
while(numbytes=read(sock, buf, sizeof(buf))!=0) { 
fwrite(buf, sizeof(buf), numbytes, fp); 
printf("%s", buf); 
} 
fclose(fp); 
close(sock); 

만약 넌블럭킹 소켓이라면 -1 이 리턴되도 루프를 탈출하지 않고

계속 recv 를 호출해야 합니다..(errno 가 EAGAIN 일경우)

근데 이문제보다는 지금 http 프로토콜중 파일전송 프로토콜에

관해 좀 찾아보셔야 할듯 하네요.

mudori의 이미지

문제가 있네요....

익명 사용자의 이미지

fwrite의 사용이 틀린것은 알고 있나요? 앞에서 지적했는데...

전체 소스를 올려보세요. 올려진 소스의 내용으로 봐서는 100줄 이내일것으로 보이는데. 물론, 좀 길어도 상관없지요.
질문에 끈기가 있어보이므로 저도 끊기 있게 답변해드리겠습니다.

mudori의 이미지

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdio.h>

int main()
{
int sock;
int numbytes;
struct sockaddr_in addr;
struct hostent *host;
char *hostname="aaa.com";
char *port="80";
char header1[1024];
char *getenv(const char *NAME);
char buf[50];
FILE *fp;
int MAX;

snprintf(header1, 1024, "GET /aaa.tar.gz HTTP/1.1\r\n"
"Host: domain.com\r\n"
"User-Agent: update\r\n"
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
"Accept-Language: ko-kr,ko;q=0.8,en-us;q=0.5,en;q=0.3\r\n"
"Accept-Encoding: gzip,deflate\r\n"
"Keep-Alive: 300\r\n"
"Connection: close\r\n"
"\r\n");

if(!(host=gethostbyname(hostname))) {
fprintf(stderr, "2000\n");
exit(1);
}

sock=socket(PF_INET, SOCK_STREAM, 0);
if(sock==-1) {

perror("2003");
exit(1);
}

memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr=*(struct in_addr *)host->h_addr;
addr.sin_port=htons(atoi(port));

if(connect(sock, (struct sockaddr *)&addr, sizeof(addr))==-1) {
perror("2001");
exit(1);
}

if(write(sock, header1, sizeof(header1))==-1) {
perror("2002");
exit(1);
}

numbytes=0;

fp=fopen("aaa.tar.gz", "wb");

while(numbytes=read(sock, buf, sizeof(buf))!=0) {
fwrite(buf, sizeof(buf), numbytes, fp) ;
}
fclose(fp);
close(sock);

return 0;
}

위에것이 소스입니다. 처음에 http 서버에 write 하고 그 다음 header 를 받고 무언가 처리해줘야 할것 같은데.. 코딩을 어케해야하는지를 모르겠습니다.

cdpark의 이미지

RFC 읽기 귀찮다면 그걸 구핸해놓은 라이브러리를 찾아서 사용하세요.

그나마 귀찮다면.. -_-;;

k2hyun의 이미지

read 로 들어오는 값들 중 일부는 버리셔야 할겁니다.

말씀하신 대로 헤더 정보가 포함되어 있으니까요

Contents-Length 라는 정보가 오면 실제 aaa.tgz 파일의 크기가 있을테고요.

나머지는 \r\n\r\n 이던가... 헤더와 바디를 구분하는 문자열이 나올때까지 버리세요.

더 이상 없다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.