Data propagation in the network is divided into: Unicast (unicast), multicast (multicast or multicast), and broadcast (broadcast). Broadcast and multicast are only applied to UDP, and they are important for applications that need to transmit messages to multiple receivers at the same time. TCP is a connection-oriented protocol, which means that there is a connection between two processes (determined by the port number) running on two hosts (determined by the IP address), respectively. Broadcast addresses are not allowed to forward routers to other interfaces by default, and broadcasts cannot traverse routers. The broadcast has several forms:
The limited broadcast address is 255.255.255.255, which is used for the address of the IP datagram during host configuration, at which point the host may not know the network mask of its network or even its IP address. In any case, the router does not forward datagrams with the destination address as a restricted broadcast address, and such datagrams appear only on the local network.
The broadcast address to the network is the address of the host number 1, the Class A network broadcast address is netid.255.255.255, where NetID is the network number of the Class A network.
The broadcast address that points to the subnet is the address where the host number is 1, and the IP address that is broadcast directly as a subnet needs to know the netmask of the subnet. If the subnet mask for Class B network 128.1 is 255.255.255.0, the address 128.1.2.255 is the broadcast address of the corresponding subnet.
- Broadcasts that point to all subnets:
Broadcasts that point to all subnets also need to know the subnet mask of the destination network. The subnet number and host number of these broadcast addresses are all 1. If the destination subnet mask is 255.255.255.0, then the IP address 128.1.255.255 is a broadcast address that points to all subnets.
A PHP socket is also available for broadcast. In socket communication, the server that implements the connection and the client need to bind the same port number, and the port number represents the process of sending and receiving. Here is a simple example of a broadcast communication implemented in PHP, which is tested with PHP and C as a client:
<?php# Script--broadcast.php/* Author @ huoty * Date @ 2015-11-17 09:58:25 * Brief @ * */* Create a broadcast event */functionBroadcast(){$sock=Socket_create(Af_inet,Sock_dgram,Sol_udp);Transfer data in UDP mode using IPV4 format address, datagram formSocket_set_option($sock,Sol_socket,So_broadcast,1);Set to broadcast modeWhile(True){$msg=' hi! ‘.Date("Y-m-d h:i:s",Time());The string to sendSocket_sendto($sock,$msg,Strlen($msg),0,"255.255.255.255",12345);Send, 255.255.255.255 is the broadcast address, 12345 is the portecho "broadcast...\n";Sleep(2);}Socket_close($sock);Shut down}/* Create daemon */$pid=Pcntl_fork();If($pid<0){Die("Fork failed!\ n");}ElseIf($pid>0){Exit;}Else{/* Output process ID for easy kill */Echo"Daemons ID:".Posix_getpid () "\n" /* Keep the program running */set_time_limit (0 ); /* Create a new Session */ $sid = posix_setsid (); if ( $sid < 0 ) {exit} /* change working directory as root directory */chdir ( "/" Span class= "P" >); broadcast () } ?>
<?php# Script--client.php/* Author @ huoty * Date @ 2015-11-17 09:58:25 * Brief @ * *Error_reporting (E_all);Set_time_limit(0);Ob_implicit_flush();$socket=Socket_create(Af_inet,Sock_dgram,Sol_udp);If($socket===False){Echo"Socket_create () Failed:reason:".Socket_strerror(Socket_last_error())."\ n";}$ok=Socket_bind($socket,' 255.255.255.255 ',12345);If($ok===False){Echo"Socket_bind () Failed:reason:".Socket_strerror(Socket_last_error($socket));}While(True) { $from = $port = 0socket_recvfrom ( $socket $ Buf10240 $from $port echo $buf "\n" usleep (1000 ); } ?>
/* CLIENT.C */#include <stdio.h> #include <string.h> #include <unistd.h> #include <netinet/in.h>#define MAXLINE 80#define Serv_port 12345IntMain(Intargc,Char*Argv[]){structSockaddr_inServaddr;IntSockfd,N;CharBuf[MAXLINE];CharStr[Inet_addrstrlen];Socklen_tServaddr_len;Sockfd=Socket(Af_inet,Sock_dgram,0);Bzero(&Servaddr,sizeof(Servaddr));Servaddr.Sin_family=Af_inet;Servaddr.Sin_addr.S_addr=Htonl(Inaddr_any);Servaddr.Sin_port=Htons(Serv_port);Bind(Sockfd,(structSockaddr*)&Servaddr,sizeof(Servaddr));fprintf(StdOut,"Accepting connections ...\ n");Memset(Buf,0,sizeof(Buf));While(1){N=Recvfrom(Sockfd,Buf,MAXLINE,0,Null,0);If(N==-1) fprintf (stderr "Recvfrom error" fprintf (stdout "%s \n "buf); memset (buf0 sizeof (buf} close (sockfdreturn 0;}
In general, a broadcast is a task that takes a long time, so you can create a daemon to complete the broadcast to avoid the long running of the program to the control terminal. If you do not use a daemon, you can also use the Linux nohup
command to implement. However, the process control of PHP cannot be applied in a WEB server environment. So, to make PHP process control in the WEB environment to be applied, you can use a roundabout way, that is, to cli
execute the PHP file containing the process control, the so-called CLI mode refers to the way the shell executes. Another problem to note is that in a Web environment, because the PHP program is a dead loop, the program runs all the time, so the client will never get the results of the server's return. To solve this problem, you can use the &
let program to run in the background while redirecting the output to /dev/null
. A new file can then be created to ensure that the broadcast can be triggered in a WEB server environment:
<?php # Script -- startup.php/* Author @ Huoty * Date @ 2015-12-02 16:53:43 * Brief @ */exec("php ./broadcast_daemons.php >/dev/null &");echo "Finished!";?>
PHP broadcasts information to clients