File transfer between Linux C server and client

Source: Internet
Author: User
Tags fread sendfile sleep function sprintf

Recently did a Linux C network project, simply write the server between the client upload, download files, the use of concurrent server, you can implement multiple clients simultaneously upload, download.
Write not good, also please big God more advice! Many do not say, everything is in the code, some of the code is as follows:

/*SERVER.C */server-side void *recvmation (void *arg)//receive the message sent over by the client, thread {int my_fd = * ((int *) arg); int N;while (1) {n = recv (MY_FD, (s Truct Regis *) &recvreg, sizeof (Recvreg), 0), if (n = =-1) {perror ("Fail1 to receive!\n"),//exit (1);}   else if (n = = 0) {continue; If the client disconnects, the server continues to listen for the receiving}printf ("filename:%s\n", Recvreg.filename);p rintf ("flag =%s\n", Recvreg.flag);p rintf ("MY_FD = %d\n ", my_fd); if (strncmp (Recvreg.flag," Sendfile ", 8) = = 0)//client uploads files to server {char send_filename[file_name];strcpy (send_   Filename, Recvreg.filename); Recvfile (MY_FD, send_filename); Accept client-Sent files}if (strncmp (Recvreg.flag, "Lookfile", 8) = = 0)//client download file from server {char down_filename[file_name];D ir *DP = null;s Truct dirent *dirp;if (dp = Opendir ("/root/server/dai") = = NULL)//Find and output all files under the current folder//This path can be modified according to your needs printf ("Cannot open \ n "); while ((DIRP = Readdir (DP)) = NULL) {printf ("%s\n ", Dirp->d_name); strcpy (D_file.dir_file, dirp->d_name); if (Send (MY_FD, (struct dir_file *) &d_file, sizeof (D_file), 0) < 0) Break;usleep (1000);//Sleep LetterNumber of delays, this must have, because non-blocking bzero (struct dir_file *) &d_file, sizeof (D_file));} Closedir (DP); Send (MY_FD, "OK", 2, 0);} if (strncmp (Recvreg.flag, "Downfile", 8) = = 0)//client download file from server {char down_filename[file_name];strcpy (down_filename,   Recvreg.filename); Sendfile (MY_FD, down_filename); The client downloads the file}}close (MY_FD) from the server,//After all operations are completed, close the file descriptor}void sendfile (int sfd, char down_filename[])//Send the file {char file_ to the client      Name[file_name];char Buffer[buffer_size];   Char path[path_size];strcpy (file_name, down_filename);  printf ("%s\n", file_name);       sprintf (Path, "/root/server/dai/%s", file_name);   This path can modify//open the file according to your needs and read the file data *FP = fopen (path, "R");      if (NULL = = fp) {printf ("file:%s not found\n", file_name);        } else {bzero (buffer, buffer_size);        int length = 0; Each time a piece of data is read, it is sent to the client, looping until the file is read while (length = fread (buffer, sizeof (char), buffer_size, FP)) > 0) {PR       intf ("length =%d\n", length), if (the Send (SFD, buffer, length, 0) < 0)   {printf ("Send file:%s failed.\n", file_name);          Break        } bzero (buffer, buffer_size);   }//Close file fclose (FP);   Sleep (1);//delay//Sleep function delay, this must be because the non-blocking send (SFD, "OK", 2, 0);      printf ("file:%s Transfer successful!\n", file_name); }//close connection to client}void recvfile (int sfd, char send_filename[])//Accept client-sent file {char File_name[file_name];char Buffe  R[buffer_size];char path[path_size];strcpy (file_name, send_filename);   printf ("%s\n", file_name);      sprintf (Path, "/root/server/dai/%s", file_name);   This path can modify//open the file according to your needs and read the file data *FP = fopen (Path, "w");      if (NULL = = fp) {printf ("file:%s not found\n", file_name);    } bzero (buffer, buffer_size);  int length = 0; while (length = recv (sfd, buffer, buffer_size, 0)) > 0) {printf ("length =%d\n", length), if (strcmp (buffer, "OK ") = = 0) {break;} if (fwrite (buffer, sizeof (char), length, FP) < length) {printf ("file:\t%s Write FaiLed!\n ", file_name);      Break    } bzero (buffer, buffer_size);    }//After receiving success, close the file, close socket printf ("Receive file:\t%s from Server IP successful!\n", file_name);    Fclose (FP); }


 

/*client.c*/client void recvfile (int client_socket_fd, char down_filename[])//Client download file from server {char file_name[file_name];        Char Buffer[buffer_size];         strcpy (file_name, down_filename);    Send the data in buffer to the server FILE *FP = fopen (file_name, "w");      if (NULL = = fp) {printf ("file:\t%s Can not Open to write\n", file_name);    Exit (1);    }//Receives data from the server into buffer//receives a piece of data, writes it to the file, loops until the file is received and finished bzero (buffer, buffer_size);  int length = 0; while (length = recv (client_socket_fd, buffer, buffer_size, 0)) > 0) {printf ("length =%d\n", length); if (STRCM P (buffer, "OK") = = 0) {break;}        if (fwrite (buffer, sizeof (char), length, FP) < length) {printf ("file:\t%s Write failed\n", file_name);      Break    } bzero (buffer, buffer_size); Note here to use the empty function, otherwise an unexpected error will occur}//After receiving success, close the file, close the socket printf ("Receive file:\t%s from Server IP successful!\n", fi    Le_name);  Fclose (FP);  printf ("CLIENT_SOCKET_FD =%d\n", client_socket_fd); }voidSendfile (int client_socket_fd, char send_filename[])//upload file to server {char file_name[file_name]; char buffer[buffer_size]; strcpy (file_name, send_filename);   Open the file, ready to write to *FP = fopen (file_name, "R");      if (NULL = = fp) {printf ("file:%s not found\n", file_name);        } else {bzero (buffer, buffer_size);        Note here that the empty function is used, otherwise an unexpected error will occur int length = 0; Each time a piece of data is read, it is sent to the client, looping until the file is read while (length = fread (buffer, sizeof (char), buffer_size, FP)) > 0) {PR intf ("length =%d\n", length), if (Send (client_socket_fd, buffer, length, 0) < 0) {printf ("Send File            :%s failed.\n ", file_name);          Break     } bzero (buffer, buffer_size);        Notice here that the empty function is used, otherwise unexpected errors will occur} sleep (1); Close File//Sleep function delay, this must be because non-blocking fclose (FP);       Send (CLIENT_SOCKET_FD, "OK", 2, 0);    printf ("file:%s Transfer successful!\n", file_name); }//Close the connection to the client}


Of course this is only part of the code for reference, complete code http://download.csdn.net/detail/u013930856/8734007

Also add Linux c under MySQL database implementation Login Registration interface

File transfer between Linux C server and client

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.