이어서 클라이언트에서는 아래와 같습니다. 

IP주소, 포트번호, 파일 경로 및 인자 값 입력 받는 등의 추가 옵션은

코드를 약간만 변경하시면 됩니다.


해더파일은

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

----

int main(int argc, char **argv)
{

    int client_len;
    int client_sockfd;
    char buf[256];
    struct sockaddr_in clientaddr;

    clientaddr.sin_family = AF_INET;
    clientaddr.sin_addr.s_addr = inet_addr("100.10.1.57");
    clientaddr.sin_port = htons(8002);
    client_len = sizeof(clientaddr);

    client_sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (connect (client_sockfd, (struct sockaddr *)&clientaddr,
                client_len) < 0)
    {
        perror("connect error :");
        exit(0);
    }

    int nbyte;
    size_t filesize = 0, bufsize = 0;
    FILE *file = NULL;

    file = fopen("test.jpg", "wb");

    ntohl(filesize);
    recv(client_sockfd, &filesize, sizeof(filesize), 0);
    printf("file size = [%d]\n", filesize);
    bufsize = 256;
    while(filesize != 0)
    {
        if(filesize < 256)
            bufsize = filesize;

        nbyte = recv(client_sockfd, buf, bufsize, 0);
        filesize = filesize -nbyte;

        fwrite(buf, sizeof(char), nbyte, file);

        nbyte = 0;

    }

    close(client_sockfd);
    fclose(file);
    return 0;
}


반응형

'Language > C' 카테고리의 다른 글

fwrite함수 예제  (6) 2012.11.06
switch case문과 if문의 성능차이  (8) 2012.10.31
바이너리 파일전송 C언어 예제 -서버-  (8) 2012.10.18
itoa 함수 예제  (7) 2012.10.17
strspn 함수 예제  (7) 2012.09.20

+ Recent posts