Socket Transfer File Example (on)

zhaozj2021-02-16  75

// 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;

}

转载请注明原文地址:https://www.9cbs.com/read-22832.html

New Post(0)