Delphi Network Programming UDP control

Source: Internet
Author: User
Tags port number

There are two UDP controls in Delphi: Tidudpserver and Tidudpclient controls, which can be used to transmit UDP data, and are simple to use, mainly in the form of subtle features that make it clear that there is a great benefit to using both controls correctly, as described below:

First, Tidudpserver: Represents a UDP server, receive data sent by UDP clients; Place a Tidudpserver control on the form named Udpsvr, and write the following code in the Formcreate event:

UDPSVR.BINDINGS.ADD;

Udpsvr.bindings[0]. IP: = ' 192.168.2.117 ';

Udpsvr.bindings[0]. Port: = 1812;

Udpsvr.active: = True;

Write the following code in the Onudpread event of the Udpsvr control:

Var

BUFFER:ARRAY[0..1024] of Char;

Isize:integer;

sdata:string;

Begin

ZeroMemory (@Buffer, sizeof (Buffer));

Isize: = adata.size;

If isize > 1024x768 Then

Begin

Isize: = 1024;

End

Adata.seek (0,sofrombeginning);

Isize: = Adata.read (buffer,isize);

。。。。。 {Processing of received data}

End

This completes a UDP application that can receive data;

In fact, Tidudpserver has the method of sending data: Send and Sendbuffer, is inherited from Tidupdbase, so as long as the use of Tidudpserver control can be done to send and receive data, add a TButton control on the form, Add the following code to the click event;

Var

BUFFER:ARRAY[0..1024] of Char;

stext:string;

Ilen:integer;

Begin

Stext: = ' 12345678 '

ZeroMemory (@Buffer, sizeof (Buffer));

Strpcopy (Buffer,stext);

Ilen: = Length (stext);

Udpsvr.sendbuffer (' 192.168.2.117 ', 1814,buffer,ilen);

End

This allows data to be sent to another UDP application;

A Tidudpserver control can open more than one port, as the following code opens two ports:

UDPSVR.BINDINGS.ADD;

Udpsvr.bindings[0]. IP: = Getlocalip;

Udpsvr.bindings[0]. Port: = 1812;

UDPSVR.BINDINGS.ADD;

UDPSVR.BINDINGS[1]. IP: = Getlocalip;

UDPSVR.BINDINGS[1]. Port: = 1813;

Udpsvr.active: = True;

When multiple ports are open, the sending data is sent from which port. Based on the test result: the port where the data was recently received, or bindings[0] if no data has been received. Port;

In the event of receiving data, there is a parameter of type tidsockethandle: abinding; This parameter has two pairs of properties:

IP, port: Represents the local IP address and ports;

PeerIP, Peerport: Represents the remote IP address and port;

where PeerIP, peerport in the delivery of data recurrence of UDP application is very useful, because the UDP server can respond to PeerIP and peerport data, instead of setting the UDP client IP address and port number (this method is improper application, will cause problems, The following will be said);

Second, Tidudpclient: On behalf of a UDP client, dedicated to send UDP data, not receive data, because there is no corresponding events and methods; As mentioned earlier, the use of the Tidudpserver control can complete the transmission of UDP data, so has been suspected TIDUDP The necessity of the presence of the client control, which should be rare for clients that have a UDP client that sends only data and never receives data, and later I think it might be possible to share the load of the Tidudpserver control with the Tidudpclient control. In a server that needs to send and receive a large amount of UDP data, the Tidudpserver control only receives data, and it also sends data specifically with one Tidudpclient control, which may improve the performance of the application (not verified) There are two ways to send data using tidudpclient:

1, using the Tidudpclient control itself, the send and Sendbuffer methods, you need to set the host and Port properties, put a tidudpclient control on the form, named: UDPCLT , set the host and port property values to: 192.168.2.117 and 1814, and then write the following code:

Var

BUFFER:ARRAY[0..1024] of Char;

stext:string;

Ilen:integer;

Begin

Stext: = ' 12345678 ';

ZeroMemory (@Buffer, sizeof (Buffer));

Strpcopy (Buffer,stext);

Ilen: = Length (stext);

Udpclt.sendbuffer (Buffer,ilen);

End

2, do not need to set the host and Port properties, and directly take advantage of the tidupdbase inherited from the Send and Sendbuffer methods, you can also send data, the code is as follows:

Udpclt.sendbuffer (' 192.168.2.117 ', 1814,buffer,ilen);

What port does the Tidudpclient control emit when it sends data? According to the results of the test: is random, so to the above mentioned UDP server can be to PeerIP and Peerport to respond to the data caused trouble, that is, if the UDP server received data is sent through the Tidudpclient control, Can not be returned through PeerIP and Peerport, but should set the client's IP address and port number, in the specific application is the case, depending on the test results.

I myself toss a long time, always error, the original is not added UDP control, these two controls under the Indy Server tab, depressed AH. Tossing the day, the original reason.

My own

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.