Use transmitfile (Send File)

Source: Internet
Author: User
Bool transmitfile (socket hsocket, handle hfile, DWORD nnumberofbytestowrite, DWORD merge, lpoverlapped, lptransmit_file_buffers lptransmitbuffers, DWORD dwflags); parameter: hsocket connection-oriented socket handle. The number of bytes to be transferred by the hfile opened file handle nnumberofbytestowrite. The value 0 indicates that the entire file is transmitted. The size of the data block that nnumberofbytespersend sends each time. The value 0 indicates the default value of socket layer. The lpoverlapped pointer to the overlapped structure. The null value indicates that overlapped I/O mode is not enabled. Lptransmitbuffers points to the transmit_file_buffers structure pointer. Null indicates that only files are transmitted. Dwflags has three optional values: tf_disconnect tf_reuse_socket token to provide the receiver code: Socket socketlisten = socket (af_inet, sock_stream, ipproto_tcp); BIND (socketlisten, null, 0); Listen (socketlisten, somaxconn); socket socketread = accept (socketlisten, null, 0); char SZ [1000000]; while (true) {If (Recv (socketread, SZ, strlen (sz )) = socket_error) break; // Write File} sending code connect... transmitfile (hsocket, hfile, 0, 0, null, tf_disco Nnect); // example // sender // handle hfile = createfile (lpszfilepath, generic_read, file_share_read, 0, open_existing, file_attribute_normal, 0); transmitfile (m_socket, hfile, 0, trans_file_length, null, null, tf_disconnect); closehandle (hfile); // receiver ulong lreadsize = 0; char szinfo [trans_file_length] = {0 }; handle hfile = createfile (lpszfilepha, generic_write, file_pai_write, 0, create_always, file_attribute_normal, 0); While (hfile! = Invalid_handle_value) {int isize = Recv (m_socket, szinfo, trans_file_length, 0); If (isize = socket_error | isize = 0) {closehandle (hfile); break ;} else if (isize <trans_file_length) {writefile (hfile, szinfo, isize, & lreadsize, null); closehandle (hfile); break ;}// write the writefile (hfile, szinfo, isize, & lreadsize, null );}

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.