First, I will display the sequence of UDP SOCKET call without connection protocol.
In my summary, the methods for sending and receiving data through UDP in Delphi are as follows:
Example Description: The following is an example of my actual device communication. 'f1, 00' (hexadecimal, 2 bytes) is sent on port 4660 using UDP ), receive 'f1, 00,00, 00,00, 00,00, 00' (hexadecimal, 2 bytes) on the same port ))
1. Use underlying functions to implement
Procedure tform1.formcreate (Sender: tobject );
VaR
Wsadata: twsadata;
Begin
Edthost. Text: = 192.168.1.222 ';
Edtport. Text: = '123 ';
// 1. initialize Winsock
If (wsastartup (makeword (2, 0), wsadata) <> 0) then
Begin
// Initialization failed
Meminfo. lines. Add ('winsock init failed ');
Exit;
End
Else
Meminfo. lines. Add ('socket start ');
End;
Procedure tform1.btnfingerclick (Sender: tobject );
VaR
Info: string;
Bufsend, bufrecv: array [0 .. 1024] of byte;
SKT: tsocket;
ADDR: tsockaddr;
Re: integer;
S: string;
I: integer;
Begin
// ================================== data transmission ================ =============================< br> // 2. establish a socket
SKT: = socket (af_inet, sock_dgram, ipproto_udp);
If (SKT = invalid_socket) Then
begin
meminfo. lines. add ('error: Create socket failed! ');
exit;
end;
// 3. connect to the host
zeromemory (@ ADDR, sizeof (ADDR);
ADDR. sin_family: = af_inet;
ADDR. sin_addr.s_addr: = inet_addr ('2017. 168.1.222 ');
ADDR. sin_port: = htons (4660);
re: = connect (SKT, ADDR, sizeof (ADDR);
If (Re <> 0) then
begin
meminfo. lines. add ('connect to server failed');
exit;
end;
// 4. send message
bufsend [0]: = $ F1;
bufsend [1]: = 0;
re: = Send (SKT, bufsend, 2, 0 );
If (RE = socket_error) Then
begin
meminfo. lines. add ('send data failed');
exit;
end;
// 6. disable socket
closesocket (SKT );
// ================== receiving data ============================ ======================
// 2. Create a socket
SKT: = socket (af_inet, sock_dgram, ipproto_udp );
If (SKT = invalid_socket) then
Begin
Meminfo. lines. Add ('error: Create socket failed! ');
Exit;
End;
// 3. Bind the host
Zeromemory (@ ADDR, sizeof (ADDR ));
ADDR. sin_family: = af_inet;
ADDR. sin_addr.s_addr: = inet_addr ('192. 168.1.106 ');
ADDR. sin_port: = htons (4660 );
Re: = BIND (SKT, ADDR, sizeof (ADDR ));
If (Re <> 0) then
Begin
Meminfo. lines. Add ('connect to server failed ');
Exit;
End;
// 4. Receive information
Re: = Recv (SKT, bufrecv, 6, 0 );
If (RE = socket_error) then
Begin
Meminfo. lines. Add ('send data failed ');
Exit;
End
Else
Begin
S: = '';
For I: = 0 to re-1 do
Begin
S: = S + inttohex (INTEGER (bufrecv [I]), 2 );
End;
Meminfo. lines. Add (s );
End;
// 6. Disable socket
Closesocket (SKT );
End;
Procedure tform1.formdestroy (Sender: tobject );
Begin
// 6. Release Winsock
Wsacleanup ();
End;
2. Use the tudpsocket component to implement
Tudpsocket componentIs inherited from tcustomipclient. It is designed to be used only as one client.
Therefore, it cannot be directly used to receive data. To receive data, you must define another tipsocket for receiving and rebind the interface.
Type
Tform2 = Class (tform)
Udpsocket1: tudpsocket;
Button1: tbutton;
Udpsocket2: tudpsocket;
Procedure formcreate (Sender: tobject );
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
Audpserver: tipsocket;
End;
VaR
Form2: tform2;
Implementation
{$ R *. DFM}
Procedure tform2.formcreate (Sender: tobject );
VaR
ADDR: sockaddr_in;
Begin
Udpsocket1.remotehost: = '192. 168.1.222 ';
Udpsocket1.remoteport: = '000000 ';
Udpsocket1.open;
Audpserver: = tipsocket. Create (NiL );
Audpserver. remotehost: = '192. 168.1.222 ';
Audpserver. remoteport: = '20140901 ';
Audpserver. localhost: = audpserver. localhostname;
Audpserver. localport: = '20140901 ';
Audpserver. Protocol: = ipproto_udp;
Audpserver. socktype: = stdgram;
Audpserver. Active: = true;
ADDR: = audpserver. getsocketaddr (audpserver. localhost, audpserver. localport );
BIND (audpserver. Handle, ADDR, sizeof (ADDR ));
End;
Procedure tform2.button1click (Sender: tobject );
VaR
Receivedstring: string;
Buff: array [0 .. 1] of byte;
Revbuf: array [0 .. 1024] of byte;
Revsize: integer;
I: integer;
S: string;
Toaddr: sockaddr_in;
Len: integer;
A: in_addr;
Begin
Buff [0]: = $ F1;
Buff [1]: = 0;
Udpsocket1.sendbuf (buff, 2 );
Toaddr: = udpsocket2.getsocketaddr (udpsocket2.localhost, udpsocket2.localport );
BIND (udpsocket2.handle, toaddr, sizeof (toaddr ));
Revsize: = udpsocket2.receivebuf (revbuf, 6 );
S: = '';
For I: = 0 to RevSize-1 do
Begin
S: = S + inttohex (INTEGER (revbuf [I]), 2 );
End;
Showmessage (s );
End;
3. Use the tidudpclient component to implement
The tidudpclient component re-binds a socket to receive data, so a component can receive data.
Procedure tform1.button1click (Sender: tobject );
VaR
Receivedstring: string;
Buff: array [0 .. 1] of byte;
Revbuf: array [0 .. 1024] of byte;
Revsize: integer;
I: integer;
S: string;
Begin
Buff [0]: = $ F1;
Buff [1]: = 0;
Idudpclient1.sendbuffer (buff, 2 );
Revsize: = idudpclient1.binding. Recv (revbuf, 6, 0 );
S: = '';
For I: = 0 to RevSize-1 do
Begin
S: = S + inttohex (INTEGER (revbuf [I]), 2 );
End;
Showmessage (s );
End;
Procedure tform1.formcreate (Sender: tobject );
Begin
Idudpclient1.host: = '192. 168.1.222 ';
Idudpclient1.binding. Port: = 4660;
Idudpclient1.binding. Bind;
Idudpclient1.active: = true;
End;
3. Using the tnmudp component
I found that during the first operation, you must press the button twice in a row to receive the data.
Procedure tform1.bitbtn2click (Sender: tobject );
VaR
Buf: array [0 .. 1] of char;
Revbuf: array [0 .. 1024] of char;
SS: string;
I: integer;
Len: integer;
Begin
Buf [0]: = char ($ F1 );
Buf [1]: = char (0 );
Nmudp1.sendbuffer (BUF [0], 2 );
Nmudp1.readbuffer (revbuf [0], Len );
If Len> 0 then
Begin
SS: = '';
For I: = 1 to Len do
Begin
SS: = SS + inttohex (INTEGER (revbuf [I]), 2 );
End;
Showmessage (SS );
End;
End;