Home > Back-end > C network programming, client and server
C network programming, client and server
Time:10-04
For a c + + network programming, client and server source code, requirements TXT, JPG format file, such as
CodePudding user response:
In Borland c + + Builder, use NMStrmServ and NMStrm components can easily transfer files between two computers, of course, you can also use NMFTP FTPJ protocol based file transfer components, but it is more convenient to use NMStrmServ and NMStrm components, quick,
First, run the Borland c + + Builder, add a NMStrmServ in form Form1 component and a save dialog component SaveDialog,
And then into NMStrmServ1 OnMSG events: //save file dialog If (SaveDialog1 - & gt; The Execute ()) { //if the target file exists, delete If (FileExists (SaveDialog1 - & gt; FileName)) The DeleteFile (SaveDialog1 - & gt; FileName); TFileStream * MyFStream; //set up files MyFStream=new TFileStream (SaveDialog1 - & gt; The FileName, fmCreate); Try { MyFStream - & gt; CopyFrom (STRM, STRM - & gt; The Size); ShowMessage (" receiving success! File save as "+ SaveDialog1 - & gt; FileName); } The catch (... ) { ShowMessage (" receive failure!" ); } //release MyFStream - & gt; Free (). }
In this way, a program that receives the file please ready, then we do the file sent by program: Run again Borland c + + Builder, in the form to join a new project NMStrm component, a OpenDialog component, a Button component, a Label component and an Edit component, changed for the Caption attribute to "send a file," changing the Label1 Caption attribute to "receiving host:", change the Text property of Edit1 to 127.0.0.1,
Then double-click for, add code to the OnClick event: //open file dialog box If (OpenDialog1 - & gt; The Execute ()) { TFileStream * MyFStream; MyFStream=new TFileStream (OpenDialog1 - & gt; The FileName, fmOpenRead); Try { //host name NMStrm1 - & gt; The Host=Edit1 - & gt; The Text; NMStrm1 - & gt; FromName="NetMasters"; //send NMStrm1 - & gt; PostIt (MyFStream); ShowMessage (" file "+ OpenDialog1 - & gt; FileName + send success! "" ); } The catch (... ) { ShowMessage (" file "+ OpenDialog1 - & gt; FileName + send failed! "" ); } MyFStream - & gt; Free (). } Code input and compile run the two programs, respectively, after the completion of the compilation, you can click the "send file" button to send application to open a file to transfer to a remote host, in this case the default of the receiving host IP address 127.0.0.1, namely the local computer, you can also run on LAN in the other computer receives the application, then fill in the host name of the router with the computer name or IP address, and then open the file to send, after the success of the send, receive program received the file and ask save the file, choose to save the path and filename to save the received files,