Write a point to the Points file program with Delphi

zhaozj2021-02-08  266

Thesis:

Delphi is powerful, with Delphi write software, can greatly reduce the development cycle of software. This article describes how to write some pairs of Points file programs with Delphi.

text:

Use the Delphi to write a point to the point pass file program Delphi function, with Delphi write software, can greatly reduce the development cycle of software. With regard to the basic idea of ​​point-to-note file, a server software, a client software, uses the same port, after the connection, the client sends a request to the server, including the file name, size, etc. of the file to be passed, if the server Accept, start the file. Of course, there are two modes, ASCII codes, and bin when file transmission, but generally BIN can be. Based on the above discussion, I originally used Delphi4 NMSTRM and NMSTRMSERV controls to be completed, but I have tested, the NMSTRM control can be used for smaller files, and it is very convenient, but if the file is large (1M). So we use the Delphi TSERVERSOCKET and TCLIENTSOCKET to write this program due to the limitations of the Ethernet package and the Delphisocket process mechanism (Delphi, when you send a large Stream with a socket, the acceptor will inspire multiple ONREAD events, Delphi She only guarantees the completeness of each data in multiple ONREAD events without collecting data and returns to the user. So don't think you have sent the paid file in a socket, another in the other, the RECV is OK. You You must collect data or define your own protocol.), So we use custom protocols. The specification method for defining the protocol is to use the Record End. Such as: TMYFILEPROTOCOL = RecordssendType = (st_Query, s_refuse, st_data, st_abort, ...); ilength: integer; bufsend: buffer; end; I have tried this approach, but I have failed, and I have always thought that my method is correct. However, the program has been compiled, it is estimated that Delphi has problems :) So I use another way in the following sample programs. There are two attributes of RECEIVETEXT and RECEIVEBUF in the Socket class. In an OnRead event, you can only use the two attributes, so we can use a full variable to save whether it is called Text or buf, that is, read a text, and then BUF, this simulates TMYFILEPROTOCOL. Start Program: Write a simplest, mainly used to explain the method.

Definition protocol: constMP_Query = '1'; MP_REFUSE = '2'; MP_ACCEPT = '3'; MP_NEXTWILLBEDATA = '4'; MP_DATA = '5'; MP_ABORT = '6'; MP_OVER = '7'; MP_CHAT = '8' ; protocol Overview: first sent by the Client MP_QUERY, send MP_ACCEPT or MP_FEFUESE after receiving the Server; Client receives MP_ACCEPT send MP_FILEPROPERTY, send MP_NEXTWILLBEDATA after receiving the Server; Client receives send MP_NEXTWILLBEDATA, send MP_DATA after receiving the Server; Client receives MP_DATA , Send data, Server accept data, and send MP_NEXTWILLBEDATA; loop until the client sends MP_OVER; the middle can send MP_CHAT STRING; Server program: Savedialog1, BtnStart Server, SS, (TSERVERSOCKET) btnStart Server.onclick (Sender: TObject); beginss.Port: = 2000; ss.Open; end; ss.OnClientRead (Sender: TObject; Socket: TCustomWinSocket); varsTemp: string; bufRecv: Pointer; iRecvLength: integer; beginif bReadText thenbeginsTemp: = Socket.ReceiveText; case sTemp [1] ofMP_QUERY: begin // here reject SaveDialog1.FileName: = Copy (sTemp, 2, Length (STemp)); if SaveDialog1.Execute thenbegin Socket.SendText (MP_ACCEPT); fsRecv: = TFileStream.Create (SaveDialog1 .Filename, fmcreate; endelse socket.sendtext (MP_REFUSE 'Dyer'); end; mp_fileproperty: begin // To send StrtOINT (STEMP, 2, LE NGTH (STEMP))))) sub-/ / time progress display. . .

Socket.SendText (MP_NEXTWILLBEDATA); end; MP_NEXTWILLBEDATA: beginSocket.SendText (MP_DATA); bReadText: = false; end; MP_END: ​​beginfsRecv.FreebReadText: = true; end; MP_ABORT: beginfsRecv.Free; bReadText: = true; end; MP_CHAT : begin // Chat Msgend; end; {of case} endelse begintryGetMem (bufRecv, 2000); // 2000 must> iBYTESENDSocket.ReceiveBuf (bufRecv ^, iRecvLength); fsRecv.WriteBuffer (bufRecv ^, iRecvLength); finallyFreeMem (bufRecv, 2000); end; {of try} bReadText: = true; Socket.SendText (MP_NEXTWILLBEDATA); end; end; Client program: put the following controls: edtIPAddress, OpenDialog1, btnConnect, btnSendFile, cs (TClientSocket) btnConnect.OnClick (. Sender: TObject); begincs.Address: = edtIPAddress.Text; cs.Port: = 2000; cs.Connect; end; btnSendFile.OnClick (Sender: TObject); beginif OpenDialog1.Execute thenBegincs.Socket.SendText (MP_QUERY OpenDialog1. FileName); // FileSize ??? end; end; cs.OnRead (Sender: TObject; Socket: TCustomWinSocket); varsTemp: string; bufSend: pointer; beginsRecv: = Socket.ReceiveText; Case sRecv [1] ofMP_REFUSE: ShowMessage ( 'Faint, Be Refused!'); MP_ACCEPT: Beginf Ssend: = tfilestream.create (OpenDialog1.FileName, Fmopen); // ibytepersend is a constant, each sending the size of the package.

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

New Post(0)