Socket implementation file Transfer function

Source: Internet
Author: User
Tags fread
The functionality to be implemented is that client downloads a file from the server and saves it locally.

There are two questions to be noted about writing this program:
1 File size is not sure, there may be a lot larger than the buffer, call once write ()/send () function can not complete the delivery of the contents of the file. You will also experience the same situation when you receive data.

To resolve this problem, you can use the while loop, for example:
Server code int ncount; while ((ncount = fread (buffer, 1, buf_size, FP)) > 0) {Send (sock, buffer, ncount, 0);}//client code int ncount; while (ncount = recv (clntsock, buffer, buf_size, 0)) > 0) {fwrite (buffer, ncount, 1, FP); for Server-side code, when read to the end of the file, F Read () returns 0, ending the loop.

For client-side code, there is a key problem, that is, after the file is transferred, let Recv () return 0 and end the while loop. Note: Reading the data in the buffer recv () does not return 0, but is blocked until the data is again in the buffer. 2 How the client side determines that the file is received, which is the problem mentioned above-when the while loop ends.

The simplest way to end a while loop is of course to have the recv () function return 0 after the file is received, so how do I get recv () to return 0? The only time the recv () returns 0 is when the fin package is received.

The fin packet indicates that the data transfer is complete and the computer receives the fin packet and knows that the other person will not transmit the data to itself, and when the Read ()/recv () function is invoked, if there is no data in the buffer, it returns 0, indicating that the "end of the socket file" was read

Here we call shutdown () to send the FIN packet: The server-side direct call Close ()/closesocket () invalidates the data in the output buffer, and the file content is likely to be disconnected when the connection is not completed and the call to shutdown () Waits for the data in the output buffer to complete.

This section takes windows as an example to demonstrate file transfer functionality, and Linux is similar to this and will not repeat it. Take a look at the complete code below.

Server-side server.cpp:
#include <stdio.h> #include <stdlib.h> #include <winsock2.h> #pragma comment (lib, "Ws2_32.lib")//Load W S2_32.dll #define BUF_SIZE 1024 int main () {//first check file for char *filename = "D:\\send.avi";//filename File *FP = fopen (filename, "RB"); Open file as binary (fp = = NULL) {printf ("Cannot open file, press any key to exit!\n"); System ("pause"); exit (0);} Wsadata Wsadata; WSAStartup (Makeword (2, 2), &wsadata); Socket Servsock = socket (af_inet, sock_stream, 0); Sockaddr_in sockaddr; memset (&sockaddr, 0, sizeof (SOCKADDR)); sockaddr.sin_family = pf_inet; SOCKADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1"); Sockaddr.sin_port = htons (1234); Bind (Servsock, (sockaddr*) &sockaddr, sizeof (SOCKADDR)); Listen (Servsock, 20); SOCKADDR clntaddr; int nsize = sizeof (SOCKADDR); SOCKET Clntsock = Accept (Servsock, (sockaddr*) &clntaddr, &nsize); loop send data until end of file char buffer[buf_size] = {0}; buffer int ncount; while ((ncount = fread (buffer, 1, buf_size, FP)) > 0) {send (Clntsock, buffEr, ncount, 0); } shutdown (Clntsock, sd_send); File read, disconnect output stream, send fin packet recv to client (clntsock, buffer, buf_size, 0); Blocking, waiting for client to receive complete fclose (FP); Closesocket (Clntsock); Closesocket (Servsock); WSACleanup (); System ("pause"); return 0; }
Client code:
#include <stdio.h> #include <stdlib.h> #include <WinSock2.h> #pragma comment (lib, "Ws2_32.lib") # Define buf_size 1024 int main () {//Enter the filename first to see if the file can create a successful char filename[100] = {0};//File name printf ("Input filename to Save:"); Gets

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.