Winsock of Windows network programming implement file Transfer sample _c language

Source: Internet
Author: User
Tags socket error htons

The example shows the method of Winsock file transfer in Windows network programming, and it has some reference value for Winsock network programming under Windows.

The program code is mainly based on the TCP stream protocol Winsock network File Transfer example, using Windows C language written. You can implement file transfer functions that pass any format file.

The specific implementation code is as follows:

The server-side code is as follows:

/************************************************************************* > File name:win_server.c > Author : Songlee ************************************************************************/#include <stdio.h> #inclu De <stdlib.h> #include <string.h> #include <WinSock2.h> #define PORT 8087 #define SERVER_IP "127.0 .0.1 "#define BUFFER_SIZE 1024 #define File_name_max_size #pragma comment (lib," ws2_32 ") int main () {//Declaration and 
  Initializes a service-side (local) address structure sockaddr_in server_addr; 
  server_addr.sin_family = af_inet; Server_addr.sin_addr. S_un. 
  S_ADDR = Inaddr_any; 
 
  Server_addr.sin_port = htons (port); 
  Initialize the socket DLL Wsadata wsadata; 
  WORD socketversion = Makeword (2, 0); 
    if (WSAStartup (socketversion, &wsadata)!= 0) {printf ("Init socket DLL error!"); 
  Exit (1); 
    //Create socket Socket M_socket = socket (af_inet, sock_stream, 0); if (Socket_error = = M_socket) {printf ("Create SOCKET ErRor! "); 
    Exit (1);  
    }//Binding SOCKET and service-side (local) address if (Socket_error = = Bind (M_socket, (lpsockaddr) &server_addr, sizeof (SERVER_ADDR))) { 
    printf ("Server Bind Failed:%d", WSAGetLastError ()); 
  Exit (1); 
    }//Monitor if (Socket_error = Listen (M_socket)) {printf ("Server Listen Failed:%d", WSAGetLastError ()); 
  Exit (1); 
 
    while (1) {printf (' Listening to client...\n '); 
    Sockaddr_in client_addr; 
 
    int client_addr_len = sizeof (CLIENT_ADDR); 
    SOCKET M_new_socket = Accept (M_socket, (SOCKADDR *) &client_addr, &client_addr_len); 
      if (Socket_error = = M_new_socket) {printf ("Server Accept Failed:%d", WSAGetLastError ()); 
    Break 
 } Char Buffer[buffer_size]; 
    memset (buffer, 0, buffer_size); 
      if (recv (m_new_socket, buffer, buffer_size, 0) < 0) {printf ("Server Receive Data failed!"); 
    Break 
    } Char file_name[file_name_max_size+1]; memset (file_name, 0, 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); FILE * fp = fopen (file_name, "RB"); 
    Under Windows is "RB", which means opening a read-only binary file if (NULL = fp) {printf ("File:%s not found\n", file_name); 
      else {memset (buffer, 0, buffer_size); 
 
      int length = 0; while (length = fread (buffer, sizeof (char), buffer_size, FP)) > 0 {if (send (m_new_socket, buffer, le 
          Ngth, 0) < 0) {printf ("Send File:%s failed\n", file_name); 
        Break 
      } memset (buffer, 0, buffer_size); 
      Fclose (FP); 
    printf ("File:%s Transfer successful!\n", file_name); 
  } closesocket (M_new_socket); 
  } closesocket (M_socket); 
  Release the Winsock library WSACleanup (); 
return 0; 

 }

The

client-side code is as follows:

/************************************************************************* > File name:win_client.c > Author : Songlee ************************************************************************/#include <stdio.h> #inclu De <stdlib.h> #include <string.h> #include <WinSock2.h> #define PORT 8087 #define SERVER_IP "127.0 .0.1 "#define BUFFER_SIZE 1024 #define File_name_max_size #pragma comment (lib," ws2_32 ") int main () {//initialization 
  Socket DLL Wsadata wsadata; 
  WORD socketversion = Makeword (2, 0); 
    if (WSAStartup (socketversion, &wsadata)!= 0) {printf ("Init socket DLL error!"); 
  Exit (1); 
  //Create socket Socket C_socket = socket (af_inet, sock_stream, 0); 
    if (Socket_error = = C_socket) {printf ("Create SOCKET error!"); 
    System ("pause"); 
  Exit (1); 
  ///Specify service-side address sockaddr_in server_addr; 
  server_addr.sin_family = af_inet; Server_addr.sin_addr. S_un. S_ADDR = inet_addr (SERVER_IP);
  Server_addr.sin_port = htons (port); if (Socket_error = = Connect (c_socket, (lpsockaddr) &server_addr, sizeof (SERVER_ADDR))) {printf ("Can not Connec 
    T to Server ip!\n "); 
    System ("pause"); 
  Exit (1); 
  }//input filename char file_name[file_name_max_size+1]; 
  memset (file_name, 0, file_name_max_size+1); 
  printf ("Please Input File Name on Server:"); 
 
  scanf ("%s", &file_name); 
  Char Buffer[buffer_size]; 
  memset (buffer, 0, buffer_size); strncpy (buffer, file_name, strlen (file_name) >buffer_size? 
 
  Buffer_size:strlen (file_name)); 
    Send file name to server if (send (c_socket, buffer, buffer_size, 0) < 0) {printf ("Send file Name failed\n"); 
    System ("pause"); 
  Exit (1); }//Open file, ready to write to filename * fp = fopen (file_name, "WB"); 
    Under Windows is "WB", which means opening a write-only binary file if (NULL = fp) {printf ("File:%s Can not Open to write\n", file_name); 
    System ("pause"); 
  Exit (1); 
    else {memset (buffer, 0, buffer_size); int Length = 0; while (length = recv (c_socket, buffer, buffer_size, 0)) > 0) {if (fwrite (buffer, sizeof (char), length, FP 
        < length) {printf ("File:%s Write failed\n", file_name); 
      Break 
    } memset (buffer, 0, buffer_size); 
  printf ("Receive File:%s from Server successful!\n", file_name); 
  Fclose (FP); 
   
  Closesocket (C_socket); 
 
  Release the Winsock library WSACleanup (); 
  System ("pause"); 
return 0; 

 }

A more detailed comment is available in the

program, which is not difficult to understand. Interested friends can flexibly improve the program according to their own needs, making it more powerful.

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.