Multicast and broadcast

Source: Internet
Author: User
Tags htons
There are two types of broadcast, One typeIt is directed broadcast. For example, if your network segment is 192.168.0.x, you can send it to 192.168.0.255;

AnotherIt is limited broadcast. For example, UDP is set to a broadcast IP address, which is usually 255.255.255.255.
VaR
Broadcast: bool;
Broadcast: = true;
Setsockopt (fsocket, sol_socket, so_broadcast, @ broadcast, sizeof (broadcast ));

Both are acceptable.
Directed broadcast does not require setsockopt (). Take the standard class C network as an example to directly send x. x. x.255,
This broadcast can only be received by machines in the same logical subnet. That is to say, the peer address should be x. y.
Not even in the same physical subnet. Of course, this is related to the subnet mask.

Limited broadcast must have setsockopt (fsocket, sol_socket, so_broadcast, @ broadcast, sizeof (broadcast ));
Its broadcast address is 255.255.255.255.255. Its advantage is that the host in the same subnet can receive such
Broadcast, rather than in a unified logical subnet. For example, if your address is x. x. X.1,
The host whose address is x. y. Z. A can also receive it.

The following is an example of using the socket API to implement limited broadcast:
Unit UDP;

Interface

Uses
Windows, messages, sysutils, classes, graphics, controls, forms, dialogs, WinSock,
Stdctrls;

Const
Wm_sock = wm_user + 1; // customize Windows messages
Udpport = 6543; // set the UDP port number
// Inaddr_allhosts_group = u_long ($ e0000001); // Class D address 224.0.0.1

(*
* Argument Structure for ip_add_membership and ip_drop_membership.
* Ip_mreq is not defined in the Winsock. PAS of delphi5.
*)

{Type
Ip_mreq = record
Imr_multiaddr: in_addr; (* IP multicast address of Group *)
Imr_interface: in_addr; (* local IP address of interface *)
End ;}

Type
Tfrmmain = Class (tform)
Button1: tbutton;
Edit1: tedit;
Memo1: tmemo;
Edit2: tedit;
Label1: tlabel;
Label2: tlabel;
Label3: tlabel;
Procedure formcreate (Sender: tobject );
Procedure formclose (Sender: tobject; var action: tcloseaction );
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
S: tsocket;
ADDR: tsockaddr;
Fsockaddrin: tsockaddrin;
// Mreq: ip_mreq;
// Obtain UDP messages in real time using messages
Procedure readdata (VAR message: tmessage); message wm_sock;
Public
{Public declarations}
Procedure senddata (content: string );
End;

VaR
Frmmain: tfrmmain;

Implementation

{$ R *. DFM}

Procedure tfrmmain. formcreate (Sender: tobject );
VaR
Tempwsadata: twsadata;
Optval: integer;
Begin
// Initialize the socket
If wsastartup ($101, tempwsadata) = 1 then
Showmessage ('startup error! ');

S: = socket (af_inet, sock_dgram, 0 );
If (S = invalid_socket) Then // socket creation failed
Begin
Showmessage (inttostr (wsagetlasterror () + 'socket creation failed ');
Closesocket (s );
// Exit;
End;
// Bind the sender sockaddr
ADDR. sin_family: = af_inet;
ADDR. sin_addr.s_addr: = inaddr_any;
ADDR. sin_port: = htons (udpport );
If BIND (S, ADDR, sizeof (ADDR) & lt; & gt; 0 then
Begin
Showmessage ('Bind fail ');
End;

Optval: = 1;
If setsockopt (S, sol_socket, so_broadcast, pchar (@ optval), sizeof (optval) = socket_error then
Begin
Showmessage ('udp cannot be broadcasted ');
End;

{Mreq. imr_multiaddr.s_addr: = inaddr_allhosts_group;
Mreq. imr_interface.s_addr: = inaddr_any;
If setsockopt (S, ipproto_ip, ip_add_membership, pchar (@ mreq), sizeof (mreq) = socket_error then
Begin
Showmessage ('udp multicast not allowed ');
End ;}

Wsaasyncselect (S, frmmain. Handle, wm_sock, fd_read );
// Set the receiving end's sockaddrin.
Fsockaddrin. sin_family: = af_inet;
Fsockaddrin. sin_port: = htons (udpport );

Label3.caption: = 'port: '+ inttostr (udpport );
End;

Procedure tfrmmain. formclose (Sender: tobject; var action: tcloseaction );
Begin
Closesocket (s );
End;

Procedure tfrmmain. readdata (VAR message: tmessage );
VaR
Buffer: array [1 .. 4096] of char;
Len: integer;
FLEN: integer;
Event: word;
Value: string;
Begin
FLEN: = sizeof (fsockaddrin );
Fsockaddrin. sin_port: = htons (udpport );
Event: = wsagetselectevent (message. lparam );
If event = fd_read then
Begin
Len: = recvfrom (S, buffer, sizeof (buffer), 0, fsockaddrin, FLEN );
Value: = copy (buffer, 1, Len );
Memo1.lines. Add (value)
End;
End;

Procedure tfrmmain. senddata (content: string );
VaR
Value {, hostname}: string;
Len: integer;
Begin
Fsockaddrin. sin_addr.s_addr: = inaddr_broadcast;
// Fsockaddrin. sin_addr.s_addr: = inet_addr (pchar (edit1.text); // inaddr_broadcast =-1?
Value: = content;
Len: = sendto (S, value [1], length (value), 0, fsockaddrin, sizeof (fsockaddrin ));
If (wsagetlasterror () & lt; & gt; wsaewouldblock) and (wsagetlasterror () & lt; & gt; 0) then
Showmessage (inttostr (wsagetlasterror ()));
If Len = socket_error then
Showmessage ('send fail ');
If Len & lt; & gt; length (value) then
Showmessage ('not send all ');
End;

Procedure tfrmmain. button1click (Sender: tobject );
Begin
Senddata (edit2.text );
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.