Sending and receiving data streams in Delphi

2019-06-04 08:02发布

问题:

I want to create a software to connect to another and send some data (text based) to another program through the internet.

The software will send data every 300 milliseconds (using timer) and the receiver must receive the data on time.

The connection can be like the following

  1. any data can be lost;
  2. but the rest must arrive on time with minimum delay as possible (maximum 2 seconds);
  3. the delayed data can be considers as lost, can be ignored.

I think it may be similar to a video conference software, but only using simple text as data.

Can anyone tell me how to make such a program, specifically

  • what kind of component can I use (any INDY examples);
  • what technologies do you recommend.

I have planned to do it with Delphi but other recommendation also welcome .

==========================update1 =================

Is it possible to send images through the stream

回答1:

I suggest using UDP protocol and adding timestamp information to your data and track incoming data on the receiving end. You can use UDP server (TIdUDPServer) and client (TIdUDPClient) components from Indy or other packages. Client component is for sending data and server for receiving.

Personally I usually prefer Synapse -classes. They are lower level than Indy, so it's easier to know what's happening but on the otherhand you may need to implement something yourself what Indy may provide by default.

Update

Implementation is pretty straight forward:

Sending data:

Drop TIdUDPClient on the form. Set "Host" to name or IP address of receiving end (or "localhost" if you run your programs in same computer) and port to high number where server is listening, eg 54656.

Add following code to button or timer event:

IdUDPClient1.Send('Hello, world!');

Receiving data:

Drop TIdUDPServer component on the form. Set default port to same port as in sending application. Add OnUDPRead event handler, with code:

MessageDlg('Received: ' + StringOf(AData), mtInformation, [mbOk], 0);

And new message dialog pops up everytime new message is received.

Update 2

UDP is not good for images, if you want to be sure they will stay uncorrupted, unless the image is very small and fits inside one packet.



回答2:

I'd recommend using anything but Indy. It is both buggy (especially versions bundled with Delphi) and slower than other component sets. It is easy to understand and start using it, but once you delve deeper under the hood, you start noticing small problems. Indy is constantly under development and you can find latest build here. Problem is that you can't easily replace the bundled version with newer in Delphi versions from 2009 onward because of some hard-coded dependencies.

Delphi has few other network communication methods integrated, but I recommend exploring 3rd party components. First, if you want open source, you should take a look at Overbyte ICS. It is a bit difficult to master, but it has good performance and feature set.

As a very good commercial solution, take a look at IP^Works. I have only scratched it, but from what I saw, I can wholeheartedly recommend it.



标签: delphi indy