프로젝트 자료실

파일 소켓

작성자 임베디드코리아 작성일17-10-18 16:32 조회3,738회 댓글0건
< Makefile >
CC=gcc

MSG_BEGIN = ------------- BEGIN -------------
MSG_END= -------------- END --------------

CLIENT=client_File-TCP-socket
SERVER=server_File-TCP-socket

all: begin $(CLIENT) $(SERVER) end

$(CLIENT):$(CLIENT).c
$(CC)  -o  $@  $<
begin:
@echo $(MSG_BEGIN)
end:
@echo $(MSG_END)

$(SERVER) : $(SERVER).c
$(CC)    -o    $@  $<
clean:
rm  $(CLIENT) $(SERVER)


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

#define  PORT 4040
#define  BUFSIZE  1024
#define  FILE_NAME      "kilsung.txt"

int main(){
    int server_sockfd, client_sockfd , client_len, des_fd, read_len;
    int FileNameSize;
    char buf[BUFSIZE], charFileNameSize[4];

    struct sockaddr_in serveraddr, clientaddr;

    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(server_sockfd == -1) {
perror("socket error : ");
exit(0);
      }

    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serveraddr.sin_port = htons(PORT);

    if(bind(server_sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) {
perror("bind error : ");
exit(0);
}

  while(1) {
      if(listen(server_sockfd, 5) != 0) {
perror("listen error : ");
}

  //while(1) {
      client_len = sizeof(clientaddr);
      client_sockfd = accept(server_sockfd, (struct sockaddr *)&clientaddr, &client_len);
      printf("New Client Connect : %s\n", inet_ntoa(clientaddr.sin_addr));

      des_fd = open(FILE_NAME, O_RDONLY,0);
      if(!des_fd) {
        perror("file open error : ");
break;
}

      FileNameSize = strlen(FILE_NAME);
      printf("FileNameSize : %d \n", FileNameSize);
      write(client_sockfd, FILE_NAME, FileNameSize);
      read(client_sockfd,buf,1);   
      sprintf(charFileNameSize,"%d",FileNameSize);
     
      write(client_sockfd, charFileNameSize, sizeof(charFileNameSize));

      while(1){
        memset(buf, 0x00, BUFSIZE);
read_len = read(des_fd, buf, BUFSIZE);
write(client_sockfd, buf, read_len);
if(read_len == EOF || read_len == 0) {
  printf("finish file\n");
break;
}
      }
close(des_fd);
close(client_sockfd);
    }
    close(server_sockfd);

    return 0;
}


<Client>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

#define  MAXBUF  1024
#define  File_DIR  "/opt/"

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

    int  client_sockfd, iRet , read_len, file_FD;
    int File_Name_Size=30;

    char  filename[File_Name_Size], readBUFF[MAXBUF+5];
    char Full_fileName[MAXBUF];
    char charNameSize[4];
    struct sockaddr_in Server_addr;

    client_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(client_sockfd == -1) {
      perror("socket error : ");
exit(0);
      }
     
    bzero(&Server_addr, sizeof(Server_addr));
    Server_addr.sin_family = AF_INET;
    Server_addr.sin_port = htons(atoi(argv[1]));
    Server_addr.sin_addr.s_addr = inet_addr(argv[2]);

    if( 0> connect(client_sockfd,(struct sockaddr*)&Server_addr,sizeof(Server_addr)) ){
    perror("connect() ");
        close(client_sockfd);
return 0;
}

    read(client_sockfd, readBUFF, MAXBUF);
    printf("READ : File Name : %s \n", readBUFF);
    write(client_sockfd, charNameSize,1);
    read(client_sockfd, charNameSize, sizeof(charNameSize));
    File_Name_Size=atoi(charNameSize);
    printf("File_Name_Size : %d \n", File_Name_Size);
    memcpy( filename,readBUFF, File_Name_Size);
    printf(" cate Fliename : %s \n" , filename);
    sprintf( Full_fileName, "%s%s", File_DIR,filename);
    printf("Full_fileName : %s \n", Full_fileName);

    file_FD =open(Full_fileName, O_CREAT|O_WRONLY|O_TRUNC); 
    while(1){
        memset(readBUFF, 0x00, MAXBUF);
        read_len=read(client_sockfd , readBUFF, MAXBUF);
write(file_FD, readBUFF , read_len);
if(read_len == EOF | read_len == 0){

        printf("Download Complete ! \n");
        break;
      }
    }
    close(file_FD);
    close(client_sockfd);
    return 0;
}