// 1: Display file progress
// 2: Termination of the transfer process at any time
// Send data thread
Uint SendDatathread (LPVOID LPPARAM);
/ / Receive data threads
Uint ReceiveDatathread (LPVOID LPPARAM);
// Send a data button message response function
Void ctzg004dlg :: onbuttonsend ()
{
// Todo: Add Your Control Notification Handler Code Here
// Initialization Data Send End Sign
m_bsendendnd = false;
// Initialization Data Receive End Sign
m_brecend = false;
// Update dialog data
Updatedata (TRUE);
// Open the file dialog
Cfiledialog Dlg (True, Null, Null, OFN_HIDEREADOONLY | OFN_OVERWRITEPROMPT,
"All files (*. *) | *. * ||");
IF (DLG.Domodal () == iDOK)
{
m_strfilename = dlg.getpathname ();
// Start sending data threads
AfxBeginthread (SendDatathread, this, thread_priority_normal);
}
}
/ / Receive Data Button Message Response Function
Void ctzg004dlg :: OnbuttonReceive ()
{
// Todo: Add Your Control Notification Handler Code Here
// Initialization Data Send End Sign
m_bsendendnd = false;
// Initialization Data Receive End Sign
m_brecend = false;
Updatedata (TRUE);
// Start receiving data thread
AfxBeginthread (ReceiveDataThread, this, thread_priority_normal);
}
// Terminate the send button message response
Void ctzg004dlg :: onbuttonsendend ()
{
// Todo: Add Your Control Notification Handler Code Here
/ / Set the end flag
m_bsendend = true;
}
// Terminate the receiving button message response
Void ctzg004dlg :: OnbuttonRecend ()
{
// Todo: Add Your Control Notification Handler Code Here
/ / Set the end flag of receiving data
m_brecend = true;
}
Uint SendDatathread (LPVOID LPPARAM)
{
CTZG004DLG * PDLG = (CTZG004DLG *) LPPARAM;
CFILE FILE;
IF (! file.open (pdlg-> m_strfilename, cfile :: moderad))
{
AfxMessageBox ("Open File!");
Return 0;
}
Csocket socktemp;
CSTRING STR, STR1;
SockTemp.create (pdlg-> m_idataport1); // Get the port number
SockTemp.Listen (1); // Only one connection
Csocket socksend;
/ / Set the send button is forbidden
PDLG-> Getdlgitem (IDC_Button_send) -> EnableWindow (false);
SockTemp.accept (SOCKSEND); // Note that SockTem has handed over his pointer address to SOCKSEND, so it is not a close
// Open Send Termination Button
PDLG-> getdlgitem (idc_button_send_end) -> EnableWindow (TRUE); int ibufsize = 1024 * 5;
Int isize = ibufsize;
LPBYTE PBUF = New Byte [ibufsize];
DWORD DWTEMP = 0;
Bool btest = socksend.asyncselect (0); // Because the CSocket is actually asynchronous, it becomes a synchronous (blocking) method.
Socksend.ioctl (Fionbio, & dwtemp); // To use ioctl to use the first parameter of Asyncselect to 0, see MSDN
Uint uilength = file.getlength ();
SOCKSEND.SEND (& Uilength, 4); // Transport file size to the receiver (Client)
INT inumbyte;
Uint uitotal = 0;
While (UITOTAL { INT IEND = PDLG-> m_bsendend; / / Transfer the sender status (whether it ends) Inumbyte = Socksend.send (& Ind, Sizeof (int)); //Send Error IF (Inumbyte == Socket_ERROR) { AfxMessageBox ("Send Error!"); Goto exitlable1; } else if (IEND == 1) // Termination of the sender { AfxMessageBox ("Send End Termination"); Goto exitlable1; } // read the contents of the file IF ((int) (uilength - uitotal Isize = uilength - uitotal; / / // is less than the processing of the buffer ITEST Isize = file.read (pbuf, isize); // Get the number of bytes read Int iCount = 0; // Send a fixed length file data While (ICOUNT { Inumbyte = SOCKSEND.SEND (PBUF, ISIZE-ICOUNT); / / Note that inumbyte is the actual number of sending bytes, do not use isize as accurate IF (Inumbyte == Socket_ERROR) { AfxMessageBox ("Send Error!"); Goto exitlable1; } ICOUNT = Inumbyte; If (ICOUNT { File.seek (isize-iCount, cfile :: current); } } UITOTAL = ICOUNT; // Set the send data progress bar PDLG-> M_CTRLPROGRESSEND.SETPOS (INT ((Double) UITOTAL / UILENGTH * 100) Str.Format ("Send Progress:% D %%", int ((Double) UITAL / UILENGTH * 100)); // Indicates the percentage of sending data PDLG-> getdlgitem (idc_static_send) -> getWindowText (STR1); IF (str1! = STR) PDLG-> Getdlgitem (IDC_STATIC_SEND) -> SetWindowText (STR); } // Send a file success AfxMessageBox ("Send file success!"); Exitlable1: delete [] PBUF; File.Close (); SOCKSEND.CLOSE (); PDLG-> m_ctrlprogresssend.setpos (0); // Restore progress PDLG-> getdlgitem (IDC_BUTTON_SEND_END) -> EnableWindow (false); // Setting the send end button is forbidden PDLG-> getdlgitem (idc_button_send) -> EnableWindow (TRUE); // Setting the send button is normal PDLG-> getdlgitem (idc_static_send) -> setWindowText ("Send Progress:"); // Restore Progress Return 0; } Uint ReceiveDatathread (LPVOID LPPARAM) { CTZG004DLG * PDLG = (CTZG004DLG *) LPPARAM; / / Save File Dialog Cfiledialog DLG (false, null, null, OFN_HIDEREADOONLY | OFN_OVERWRITEPROMPT, "All files (*. *) | *. * ||"); While (DLG.Domodal ()! = iDOK) { AfxMessageBox ("Select file error, please re-select!"); } CSTRING STR, STR1, STR2; Csocket SockRecv; SockRecv.create (); PDLG-> m_ctrlipsend.getWindowText (STR); // Get the sender IP address PDLG-> getdlgitem (idc_button_receive) -> enableWindow (false); // Disable Receive button While (SockRecv.connect (STR, PDLG-> M_IDataPort2) == 0) // Connect the sender address, if the Internet can be changed to the actual IP address, the port is the same as the Server terminal. { Sleep (50); } PDLG-> getdlgitem (IDC_BUTTON_REC_END) -> EnableWindow (TRUE); // Open Termination Receive Button Str2 = dlg.getpathname (); // Get file name CFILE FILE; File.open (str2, cfile :: modecreate | cfile :: modewrite); Bool bfilefail = false; DWORD DWTEMP = 0; SockRecv.asyncselect (0); SockRecv.ioctl (Fionbio, & dwtemp); // becomes blocked Uint uilength; SockRecv.Receive (& Uilength, 4); // Receive the file size of the Server end INT IBUFSIZE = 1024 * 5; Int isize = ibufsize; LPBYTE PBUF = New Byte [ibufsize]; INT inumbyte; Uint uitotal = 0; While (UITOTAL { INT IEND = 0; // Receive end termination IF (PDLG-> M_BRECEND) { AfxMessageBox ("Receive Termination!"); Goto exitlable2; } / / Receive the sender status data Inumbyte = SockRecv.Receive (& Iend, Sizeof (int)); if (Inumbyte == Socket_ERROR) { AfxMessageBox ("Receive Signal Error!"); Goto exitlable2; } // Termination of the sender IF (IEND == 1) { AfxMessageBox ("Termination!"); Goto exitlable2; } IF ((int) (uilength - uitotal ISIZE = uilength - uitotal; Int iCount = 0; // Read the fixed length data While (ICOUNT { Inumbyte = SockRecv.Receive (PBUF, ISIZE-ICOUNT); IF (Inumbyte == Socket_ERROR) { AfxMessageBox ("Receive Error!"); Goto exitlable2; } ICOUNT = Inumbyte; File.write (PBUF, Inumbyte); } UITOTAL = ICOUNT; / / as the actual reception byte // Set the progress of the reception PDLG-> M_CTRLPROGRESSREC.SETPOS (INT ((Double) UITAL / UILENGTH * 100)); Str.Format ("Receive progress:% D %%", int ((Double) UITOTAL / UILENGTH * 100); // Display the percentage of reception progress PDLG-> getdlgitem (IDC_STATIC_REC) -> getWindowText (STR1); IF (str1! = STR) PDLG-> getdlgitem (IDC_STATIC_REC) -> setWindowText (STR); } // Receive file success AfxMessageBox ("Received Success!"); Bfilefail = true; EXITLABLE2: delete [] PBUF; File.Close (); / / The file receives failed, then deletes the received file IF (! bfilefail) { Cfile :: remove (str2); } SockRecv.Close (); PDLG-> M_CTRLPROGRESSREC.SETPOS (0); // Restore the reception progress // Do not terminate the receiving button PDLG-> Getdlgitem (IDC_Button_Rec_end) -> EnableWindow (FALSE); // Open the receiving button PDLG-> Getdlgitem (IDC_Button_Receive) -> EnableWindow (TRUE); // Restore prompt schedule PDLG-> getdlgitem (IDC_STATIC_REC) -> setWindowText ("Receive progress:"); Return 0; }