File Transfer Over Network Programming in Linux

This program is developed under Linux using C language, combined with Socket programming, consisting of both client and server programs, thus adopting a C/S architecture. The corresponding source code is as follows:

Server Side:

#include <stdio.h> //#include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>
#define LISTENQ 5
#define DUMMY 0
#define BUFSIZE 1024
#define PORT 6666
int main(){    int listenfd, connfd;    int source_fd, num;    char iobuffer[BUFSIZE];    socklen_t   len;    struct sockaddr_in   servaddr, cliaddr;    char *filename="200911122224040961.jpg";  // The file sent by the server is located in the current directory
    listenfd=socket(AF_INET, SOCK_STREAM,0);    if(listenfd<0)    {       printf("Socket created failed.\n");       return -1;    }
    servaddr.sin_family=AF_INET;    servaddr.sin_port=htons(PORT);    servaddr.sin_addr.s_addr=htonl(INADDR_ANY);    if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))<0)    {       printf("bind failed.\n");       return -1;    }
    printf("listening....\n");    listen(listenfd, LISTENQ);    if((source_fd=open(filename,O_RDONLY,DUMMY))==-1)    {       printf("Source file open error!\n");       return -1;    }
    while(1)    {       len=sizeof(cliaddr);       connfd=accept(listenfd,(struct sockaddr *)&cliaddr, &len);       printf("connect from %s, port %d \n",inet_ntoa(cliaddr.sin_addr.s_addr),ntohs(cliaddr.sin_port));       lseek(source_fd,0L,0);
       while((num=read(source_fd, iobuffer, sizeof(iobuffer)))>0)           write(connfd,iobuffer,num);       close(connfd);       printf("send file success!\n");     }     close(source_fd);     close(listenfd);}

Client Side:

#include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>
#define PERMS 0666
#define BUFSIZE 1024
#define PORT 6666
#define HOST_ADDR "127.0.0.1"
int main(int argc, char *argv[]){    int sockfd,n;    int target_fd;    char iobuffer[BUFSIZE];    struct sockaddr_in   servaddr;    char * filename="test.jpg";
    sockfd=socket(AF_INET,SOCK_STREAM,0);    if(sockfd<0)    {       printf("Socket created failed.\n");       return -1;    }   
    servaddr.sin_family=AF_INET;    servaddr.sin_port=htons(PORT);    //servaddr.sin_addr.s_addr=inet_addr(HOST_ADDR);   // Method one to set server IP, the inet_addr() function is deprecated, commonly used method three    //servaddr.sin_addr.s_addr=htonl(INADDR_ANY);          // Method two    if(inet_aton(HOST_ADDR,&servaddr.sin_addr)<0)         // Method three    {         printf("inet_aton error.\n");         return -1;    }    printf("connecting...\n");    if(connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)    {       printf("Connect server failed.\n");       return -1;    }       if((target_fd=open(filename, O_WRONLY|O_CREAT, PERMS))==-1)    {       printf("Target file open error!\n");       return -1;    }
    while((n=read(sockfd,iobuffer,sizeof(iobuffer)))>0)    {           write(target_fd, iobuffer, n);    }    printf("save file success!\n");    close(target_fd);    return 0;}

In the client program, there are three ways to set the server IP. Method two is automatically searched by the system, while methods one and three can be manually set.

Server Compilation Method:

gcc -o ex2serv ex2serv.c

Client Compilation Method:

gcc -o ex2cli ex2cli.c

Leave a Comment