Socket Transfer File Example (below)

zhaozj2021-02-16  64

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 (& Ind, 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;

}

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

New Post(0)