Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Completion of socket file transfer function

2025-02-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

In this section, we will complete the socket file transfer program, which is a very applicable example. The function to be done is: client downloads a file from server and keeps it locally.

To write this program, you need to pay attention to two achievements:

1) the size of the file is uncertain, it can be much larger than the buffer, and the misappropriation of the write () / send () function cannot complete the sending of the file content. You will also encounter strange situations when accepting data.

To deal with this score, you can use while reincarnation, 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);}

With regard to the code on the Server side, when reading to the end of the file, fread () will go to 0 and finish the cycle.

With regard to the Client side code, there is a sticking point, that is, after the end of the file transfer, let recv () go to 0, finish the while reincarnation.

Note: after reading the data in the buffer, recv () does not go to 0, but is choked until there is no data in the buffer again.

2) how does Client judge the end of document acceptance, that is, the achievement mentioned below-when to complete the while reincarnation.

The most complicated way to finish the while cycle is, of course, to let the recv () function go to 0 after the end of the file acceptance, so how do you let recv () go to 0? The only chance for recv () to go to 0 is when the FIN packet is received.

FIN package performance data transmission at the end, the computer received the FIN package to know that the other side will no longer transfer data to me, when misappropriating the read () / recv () function, if there is no data in the buffer, it will go to 0, the performance of the read "the end of the socket file".

Here we misappropriate shutdown () to send FIN packets: the server side directly misappropriates close () / closesocket () will make the data in the input buffer effective, the file content can be disconnected without the connection at the end of transmission, and the misappropriation of shutdown () will wait for the end of data transfer in the input buffer.

This section uses Windows as an example to demonstrate the file transfer function. Linux is similar to this and will not repeat it. Please look at the intact code below.

Server server.cpp:

# include # pragma comment (lib, "ws2_32.lib") / / load ws2_32.dll # define BUF_SIZE 1024 int main () {/ / reflect on the existence of the file char * filename = "D:\\ send.avi"; / / File name FILE * fp = fopen (filename, "rb"); / / Open the file if (fp = = NULL) {printf ("Cannot open file, press any key to exit!\ n") in binary method 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); / / send data recursively until the beginning of the 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) / / at the end of file reading, disconnect the input stream and send FIN packet recv (clntSock, buffer, BUF_SIZE, 0) to the client; / / congested, waiting for the end of fclose (fp); closesocket (clntSock); closesocket (servSock); WSACleanup (); system ("pause"); return 0;}

Client code:

# include # pragma comment (lib, "ws2_32.lib") # define BUF_SIZE 1024 int main () {/ / output the file name first to see if the file can create a victory char filename [100] = {0}; / / the file name printf ("Input filename to save:"); gets (filename); FILE * fp = fopen (filename, "wb") / / Open (create) the file if (fp = = NULL) {printf ("Cannot open file, press any key to exit!\ n"); system ("pause"); exit (0);} WSADATA wsaData; WSAStartup (MAKEWORD (2,2), & wsaData); SOCKET sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 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); connect (sock, (SOCKADDR*) & sockAddr, sizeof (SOCKADDR)); / / recurrent acceptance of data until the end of file transfer char buffer [BUF _ SIZE] = {0}; / / file buffer int nCount; while ((nCount = recv (sock, buffer, BUF_SIZE, 0) > 0) {fwrite (buffer, nCount, 1, fp) } puts ("File transfer success!"); / / close the socket directly after the end of file acceptance without misappropriating shutdown () fclose (fp); closesocket (sock); WSACleanup (); system ("pause"); return 0;}

Prepare the send.avi file on disk D, run server first, and then run client:

Input filename to save: d:\\ recv.avi ↙

/ / wait a little while.

File transfer success!

Open the D disk and you can see recv.avi, which is opposite in size to send.avi and can be played normally.

Note that line 42 of the server.cpp code shows that recv () does not accept the data from the client end. When the client terminal misappropriates closesocket (), the server terminal will receive the FIN packet, and recv () will go there, and the previous code will continue to execute.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report