[socket programming] tcp 이용한 파일 전송 프로그램

2020. 12. 11. 16:41CS/네트워크

server 코드 (TCPserver.c)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>

#define BUFSIZE 1024
//#define MAX_PATH 10

void error_handling(char *message);

int main(int argc, char **argv) {
    FILE* file; //파일
    int sd; //서버소켓
    int cd; //클라이언트소켓
    char file_name[BUFSIZE];

    char buf[BUFSIZE];
    int buf_len;
    int filename_len;

    struct sockaddr_in serv_addr;
    struct sockaddr_in clnt_addr;
    int clnt_addr_size;

    if(argc!=2){
        printf("%s <port_number>\n", argv[0]);
        exit(1);
    }

    sd = socket(PF_INET, SOCK_STREAM, 0);
    if(sd == -1)
        error_handling("socket() error"); //소켓생성

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    serv_addr.sin_port=htons(atoi(argv[1]));

    if(bind(sd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1)
        error_handling("bind() error"); //bind

    if(listen(sd, 5)==-1)
        error_handling("listen() error!");

    clnt_addr_size=sizeof(clnt_addr);
    cd = accept(sd, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
    if(cd == -1)
        error_handling("accept() error!");

    filename_len = read(cd,file_name,BUFSIZE);
    file = fopen(file_name, "wb");

// sleep x
    while((buf_len = read(cd,buf,BUFSIZE)) > 0){
        fwrite(buf,sizeof(char),buf_len,file);
    } //iterative server 형태


    fclose(file);
    close(sd);
    close(cd);
    return 0;
 }

void error_handling(char *message)
 {
      fputs(message, stderr);
      fputc('\n', stderr);
      exit(1);
 }

client 코드 (TCPclient.c)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>

#define BUFSIZE 1024

void error_handling(char *message);

int main(int argc, char **argv) {
    int sd; //소켓

    struct sockaddr_in serv_addr;

    if(argc!=4){
        printf(" Usage %s <IP> <port> <file name>\n", argv[0]);
        exit(1);
    }

    sd = socket(PF_INET, SOCK_STREAM, 0);
    if(sd == -1)
        error_handling("socket() error"); //소켓생성

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
    serv_addr.sin_port=htons(atoi(argv[2]));

    if( connect(sd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1 )
        error_handling("connect() error!"); //connect

   //파일 읽기 시작

   int total_bufnum;
   long filesize;
   char buf[BUFSIZE];
   int sendlength;
   int sendbuf = 0;
   int namesize;


   FILE* file; //파일
   if((file = fopen(argv[3], "rb")) == NULL) { //argv[3]의 파일을 open
      printf("Error");
       exit(1);
   }
   if(file == NULL){
      printf("File is not exit");
      exit(1);
   }

   write(sd, argv[3], strlen(argv[3]));

   while((sendlength = fread(buf, sizeof(char), 5, file) > 0)) {
      write(sd, buf,sendlength);
   }

   printf("send file done\n");

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

void error_handling(char *message)
{
      fputs(message, stderr);
      fputc('\n', stderr);
      exit(1);
}

 

server쪽은 ./TCPserver <portnum>

client쪽은 ./TCPserver <ip address> <portnum> <filename>

으로 실행하면 파일 전송 완료