Implement file transfer between Windows and Linux (C++&c implementation)

Source: Internet
Author: User
Tags htons

To implement file transfer between Windows and Linux, it can be programmed via the socket network.

This time, the function is the same as that implemented in string and file transfer through sockets Windows, where the client first sends a string to the server and then sends a file; The server first receives the string sent by the client as the file name, It then receives the files sent by the client and saves them locally.

The Windows platform program as a client, Linux platform program as a server, and is in the area of LAN file transfer.

Implementation of the Windows client:

The client program compiles and runs under the VS2012 IDE, still using the Filetransfer class that is simply encapsulated in "string and file transfer through sockets under Windows". Send data and files by declaring a Filetransfer object and calling the appropriate function. Its main function is as follows:

/************************************************************************* > File Name: Client main function >author: Xiongmao >purpose: Declare a specific object of the file transfer class in the main function and send the file to the Linux server ********************************************************* ***************/#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include"File_transfer.h"using namespacestd;intPORT;stringSERVER_IP ="127.0.0.1" ;#defineBuffer_size 1024#defineFile_name_max_size 512#pragmaComment (lib, "Ws2_32")intMain () {cout<<"input IP and port"<<Endl; CIN>>SERVER_IP>>PORT;  while(1)    {        BOOLFlag; stringfilename; printf ("input file name:"); CIN>>filename;        Filetransfer ft;        Ft.setipandport (Server_ip,port);        Ft.setfilepath (filename); Flag=Ft.sendfile (filename,filename); if(flag) {printf ("send file%s success \ n", Filename.c_str ()); }         Else{printf ("send file%d fail. The error code is:%d \ n", GetLastError ()); }} system ("Pause"); return 0; } 

At the beginning of the main function, first enter the server's IP address and port, and then enter the name of the file you want to send (the file should exist in the source directory), the client can complete the file bag amount sent.

Code for the Linux server:

Linux only makes it possible to have a main.cpp file that implements a simple server function with the following code:

//main.cpp#include<netinet/inch.h>#include<sys/types.h>#include<sys/socket.h>#include<errno.h>#include<netdb.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<unistd.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#defineHello_world_server_port 8989#defineLength_of_listen_queue 20#defineBuffer_size 1024intMainintargcChar**argv) {      structsockaddr_in server_addr; intServer_socket; intopt =1; Bzero (&AMP;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 (Hello_world_server_port); /*Create a socket*/Server_socket= Socket (Pf_inet,sock_stream,0); if(Server_socket <0) {printf ("Create Socket failed!"); Exit (1); }     setsockopt (Server_socket, Sol_socket, SO_REUSEADDR,&opt,sizeof(opt)); if(Bind (Server_socket, (structsockaddr*) &server_addr,sizeof(SERVER_ADDR))) {printf ("Server Bind Port:%d failed!", Hello_world_server_port); Exit (1); }      if(Listen (Server_socket, length_of_listen_queue)) {printf ("Server Listen failed!"); Exit (1); }              while(true) {printf ("wait for file transfer...\n"); CharFile_name[buffer_size]; CharBuffer[buffer_size]; structsockaddr_in client_addr; socklen_t Client_addr_len=sizeof(CLIENT_ADDR); //first receive the string sent over        intNewsocket = Accept (M_socket, (SOCKADDR *) &client_addr, &Client_addr_len); if(Newsocket <0) {printf ("Server Accept Failed:%d", WSAGetLastError ()); Continue; } memset (Buffer,0,sizeof(buffer)); memset (file_name,0,sizeof(file_name)); if(Recv (Newsocket,buffer,sizeof(buffer),0) <0) {printf ("recv file name fail!\n");            Close (Newsocket); Continue;        } strncpy (File_name,buffer,strlen (buffer)); printf ("recv file name:%s \ n", file_name); FILE* fp = fopen (file_name,"WB"); if(fp==NULL) {printf ("Open File error\n"); Continue; }        //get a string and continue to get file datamemset (Buffer,0, buffer_size); intLength =0;  while(length = recv (newsocket, 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);        } fclose (FP);        Close (Newsocket); printf ("File Transfer success!\n");      } close (Server_socket); return 0; }  

Under Linux, go to the directory where the main.cpp is located and use the command

GCC Main.cpp-o Main.  out

To compile the server-side program, and then use the command

./main.  out

To execute the compiled program, you can start the server.

By comparing the implementation of the server program in string and file transfer via socket under Windows, it is observed that the code implementation is basically consistent, and the implementation under Linux has the following differences:

1, Linux is used to bind the port, the implementation of the listener socket server_socket is an int type, and under window is the socket type.

2, Linux, closed socket function is close, use this function needs to include Unistd.h header file, Windows is to use closesocket to close the socket.

3. Use the bzero function to set the 0 operation. The bzero function is not a standard function and is not recommended for use. The use of nonstandard functions can cause inconvenience to the porting of the program. The function of the bzero function can be implemented with the Memset function.

Implement file transfer between Windows and Linux (C++&c implementation)

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.