Precautions for sending stream files in Indy communication of Delphi

Source: Internet
Author: User

The client can send messages to the server in several ways. Two methods are discussed here:

1. After the client connects to the server, it sends a stream, and the server receives a stream.

 

{*------------------------------------------------------------------------------
The client sends a stream, assuming a connection has been established
------------------------------------------------------------------------------*}
Procedure tclientform. button3click (Sender: tobject );
VaR S: string; stream: tstream;
Begin

Try
S: = 'Hello world! ';
Stream: = tstringstream. Create (s );
Idtcpclient1.openwritebuffer;
Idtcpclient1.writeinteger (stream. size); // Note: Write the length of the stream first. If athread. Connection. readstream (Stream) is used during reading );
Idtcpclient1.writestream (stream, true );
Finally
Idtcpclient1.closewritebuffer;
Stream. Free;
End;
End;

{*------------------------------------------------------------------------------
Server receiving stream
------------------------------------------------------------------------------*}
Procedure tserverform. idtcpserver1execute (athread: tidpeerthread );
VaR stream: tstream;
Begin

If not athread. terminated and athread. Connection. connected then
Begin
Stream: = tstringstream. Create ('');

Athread. Connection. readstream (Stream); // This sentence is equivalent to readstream (stream,-1, false). It reads the length of the stream based on the first four bytes of the stream and then reads the stream.
Stream. Position: = 0;
Memo1.lines. loadfromstream (Stream );

End;

End;

2. After the client connects to the server, it sends one or more streams. When disconnected, the server merges all the streams for receiving.

{*------------------------------------------------------------------------------
The client sends a stream. Assume that no connection has been established.
------------------------------------------------------------------------------*}

Procedure tclientform. button3click (Sender: tobject );
VaR S: string; stream: tstream;
Begin
Idtcpclient1.connect;
Try
S: = 'Hello world! ';
Stream: = tstringstream. Create (s );
Idtcpclient1.openwritebuffer;
Idtcpclient1.writestream (stream, true );
Finally
Idtcpclient1.closewritebuffer;
Stream. Free;
Idtcpclient1.disconnect;
End;
End;

{*------------------------------------------------------------------------------
Server receiving stream
------------------------------------------------------------------------------*}
Procedure tserverform. idtcpserver1execute (athread: tidpeerthread );
VaR stream: tstream;
Begin

If not athread. terminated and athread. Connection. connected then
Begin
Stream: = tstringstream. Create ('');

Athread. Connection. readstream (stream,-1, true); // receives the stream when the connection is disconnected.
Stream. Position: = 0;
Memo1.lines. loadfromstream (Stream );

End;

End;

 

 

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.