C + + Builder The use of components to transfer files

Source: Internet
Author: User
Tags save file

In Borland C + + builder, using the Nmstrmserv and NMSTRM components makes it easy to pass files between two computers. Of course, you can also use the FTPJ protocol based file transfer component Nmftp, but using Nmstrmserv and NMSTRM components is more convenient and quicker.

First, run Borland C + + Builder, add a Nmstrmserv component to the form Form1 and a Save dialog box component Savedialog.

Then add in the NMStrmServ1 onmsg event:

Save File dialog box
if (Savedialog1->execute ())
{
Delete if the destination file exists
if (FileExists (savedialog1->filename))
DeleteFile (Savedialog1->filename);
TFileStream *myfstream;
Create a file
Myfstream = new TFileStream (Savedialog1->filename, fmcreate);
Try
{
Myfstream->copyfrom (STRM, strm->size);
ShowMessage ("Receive success!") Save file as "+savedialog1->filename";
}
catch (...)
{
ShowMessage ("Receive failed!") ");
}
Release
Myfstream->free ();
}

In this way, a program to receive files, please do so, and then we do the file to send the program:

Run Borland C + + Builder again, adding a NMSTRM component, a OpenDialog component, a button buttons component, a label component, and an edit component on the new project's form. Change the Caption property of Button1 to "send File", change the Caption property of Label1 to "receive host:", change the Text property of Edit1 to "127.0.0.1".

Then double-click Button1 to add the code to its onclick event:

Open File dialog box
if (Opendialog1->execute ())
{
TFileStream *myfstream;
Myfstream = new TFileStream (Opendialog1->filename, Fmopenread);
Try
{
Host Name
Nmstrm1->host = edit1->text;
Nmstrm1->fromname = "Netmasters";
Send
Nmstrm1->postit (Myfstream);
ShowMessage ("file" +opendialog1->filename+) sent successfully! ");
}
catch (...)
{
ShowMessage ("File" +opendialog1->filename+) failed to send! ");
}
Myfstream->free ();
}

Code input complete, compile and run the two programs separately. Once the compilation is complete, you can open a file to the remote host by clicking the Send File button on the sending program. In this example, the default host IP address is 127.0.0.1, which is the local computer, you can also run the receiving program on other computers in the local area network, and then fill in the name or IP address of the computer in the host name of the sending program, and then open the file to send. After the send is successful, the receiving program receives the file and asks for the save file, selects the path and file name to save to save the received file.

The above program runs through the Borland C + + Builder 5.0,win98/win2000.

Related Article

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.