Linux Network programming Socket File Transfer sample _c language

Source: Internet
Author: User
Tags strlen htons

The example program described in this paper is a socket network programming based on Linux platform, which realizes the file transfer function. This example is a socket network File transfer program based on the TCP stream protocol implementation. Written in C language. Eventually, you can implement a file transfer program that transmits any format file.

The specific implementation code is as follows:

The server-side code is as follows:

/************************************************************************* > File name:server.c > Author:so Nglee ************************************************************************/#include <netinet/in.h>/  sockaddr_in #include <sys/types.h>//socket #include <sys/socket.h>//socket #include <stdio.h>// printf #include <stdlib.h>//exit #include <string.h>//bzero #define SERVER_PORT 8000 #define LENG Th_of_listen_queue #define BUFFER_SIZE 1024 #define FILE_NAME_MAX_SIZE, int main (void) {//Declare and initialize a server-side so 
  Cket address structure struct sockaddr_in server_addr; 
  Bzero (&server_addr, sizeof (SERVER_ADDR)); 
  server_addr.sin_family = af_inet; 
  SERVER_ADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any); 
 
  Server_addr.sin_port = htons (Server_port); 
  Create socket, if successful, return socket descriptor int SERVER_SOCKET_FD = socket (pf_inet, sock_stream, 0); 
if (SERVER_SOCKET_FD < 0) {perror ("Create socket Failed:");    Exit (1); 
  int opt = 1; 
 
  SetSockOpt (SERVER_SOCKET_FD, Sol_socket, so_reuseaddr, &opt, sizeof (opt));  
    Bind socket and socket address structure if ( -1 = (Bind (server_socket_fd, (struct sockaddr*) &server_addr, sizeof (SERVER_ADDR))) { 
    Perror ("Server Bind Failed:"); 
  Exit (1); }//Socket listener if ( -1 = (Listen (server_socket_fd, length_of_listen_queue)) {perror ("Server Listen Faile 
    D: "); 
  Exit (1); 
    while (1) {//defines the socket address structure of the client struct sockaddr_in client_addr; 
 
    socklen_t client_addr_length = sizeof (CLIENT_ADDR);  Accepts the connection request, returns a new socket (descriptor), which is used for the connected client communication//Accept function to write the connected client information to the CLIENT_ADDR int new_server_socket_fd = 
    Accept (SERVER_SOCKET_FD, (struct sockaddr*) &client_addr, &client_addr_length); 
      if (NEW_SERVER_SOCKET_FD < 0) {perror ("Server Accept Failed:"); 
    Break 
    The///recv function receives data to the buffer buffers Char buffer[buffer_size]; Bzero (buffer, buffer_size);
    if (recv (new_server_socket_fd, buffer, buffer_size, 0) < 0) {perror ("server recieve Data Failed:"); 
    Break 
    //Then copy from buffer (buffer) to file_name Char file_name[file_name_max_size+1]; 
    Bzero (file_name, file_name_max_size+1); strncpy (file_name, buffer, strlen (buffer) >file_name_max_size? 
    File_name_max_size:strlen (buffer)); 
 
    printf ("%s\n", file_name); 
    Open the file and read the file data *FP = fopen (file_name, "R"); 
    if (NULL = fp) {printf ("file:%s not found\n", file_name); 
      else {bzero (buffer, buffer_size); 
      int length = 0; 
        Every time a piece of data is read, it is sent to the client, looping until the file has been read, while (length = fread (buffer, sizeof (char), buffer_size, FP) > 0) { if (send (new_server_socket_fd, buffer, length, 0) < 0) {printf ("Send file:%s failed./n", File_ 
          name); 
        Break 
      } bzero (buffer, buffer_size); 
      //Close file fclose (FP); Printf ("file:%s Transfer successful!\n", file_name); 
  //Shut down the connection to the client close (NEW_SERVER_SOCKET_FD); 
  //Turn off the listening socket Close (SERVER_SOCKET_FD); 
return 0; 

 }

The

client-side code is as follows:

/************************************************************************* > File name:client.c > Author:so Nglee ************************************************************************/#include <netinet/in.h>/ sockaddr_in #include <sys/types.h>//socket #include <sys/socket.h>//socket #include <stdio.h>/ /printf #include <stdlib.h>//exit #include <string.h>//bzero #define SERVER_PORT 8000 #define B Uffer_size 1024 #define FILE_NAME_MAX_SIZE int main () {//Declare and initialize the socket address structure of a client struct SOCKADDR_IN client 
  _ADDR; 
  Bzero (&client_addr, sizeof (CLIENT_ADDR)); 
  client_addr.sin_family = af_inet; 
  CLIENT_ADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any); 
 
  Client_addr.sin_port = htons (0); 
  Create socket, if successful, return socket descriptor int CLIENT_SOCKET_FD = socket (af_inet, sock_stream, 0); 
    if (CLIENT_SOCKET_FD < 0) {perror ("Create socket Failed:"); 
  Exit (1); }//Binding client's socket and client's SOCKET address structure is not required if ( -1 = (bind (client_socket_fd, struct sockaddr*) &client_addr, sizeof (CLIENT_ADDR))) {Perro 
    R ("Client Bind Failed:"); 
  Exit (1); 
  //Declare a server-side socket address structure and initialize it with the IP address and port on the server side, for subsequent connection struct sockaddr_in server_addr; 
  Bzero (&server_addr, sizeof (SERVER_ADDR)); 
  server_addr.sin_family = af_inet; 
    if (Inet_pton (af_inet, "127.0.0.1", &server_addr.sin_addr) = = 0) {perror ("Server IP address Error:"); 
  Exit (1); 
  } Server_addr.sin_port = Htons (Server_port); 
 
  socklen_t server_addr_length = sizeof (SERVER_ADDR); Initiating a connection to the server, CLIENT_SOCKET_FD represents a socket connection to the client and server if (connect (client_socket_fd, struct sockaddr*) &server_ 
    addr, Server_addr_length) < 0) {perror ("Can not Connect to server IP:"); 
  Exit (0); 
  //Enter the filename and place it in buffer buffers for sending Char file_name[file_name_max_size+1]; 
  Bzero (file_name, file_name_max_size+1); 
  printf ("Please Input File Name on server:\t"); scanf ("%s", file_name); 
  Char Buffer[buffer_size]; 
  Bzero (buffer, buffer_size); strncpy (buffer, file_name, strlen (file_name) >buffer_size? 
   
  Buffer_size:strlen (file_name)); Send data in buffer to the server if (send (client_socket_fd, buffer, buffer_size, 0) < 0) {perror ("Send File Name Failed:" 
    ); 
  Exit (1); 
  //Open file, ready to write to filename *FP = fopen (file_name, "w"); 
    if (NULL = fp) {printf ("file:\t%s Can not be Open to write\n", file_name); 
  Exit (1); 
  //From the server to receive data to buffer//receive a piece of data, it will be written to the file, the cycle until the file received and finished writing bzero (buffer, buffer_size); 
  int length = 0; while (length = recv (client_socket_fd, buffer, buffer_size, 0)) > 0) {if (fwrite (buffer, sizeof (char), length, 
      FP) < length) {printf ("file:\t%s Write failed\n", file_name); 
    Break 
  } bzero (buffer, buffer_size); 
  After successful, close the file, close the socket printf ("Receive file:\t%s from Server IP successful!\n", file_name); 
  Close (FP); 
  Close (CLIENT_SOCKET_FD); 
return 0;} 

 

It is not difficult to understand that the program has more detailed annotations. Interested friends can try some extensions on this basis to make their functions more powerful.

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.