|
楼主 |
发表于 2004-7-18 15:55:34
|
显示全部楼层
这里附加上主进程发送广播数据包的代码段,为简洁起见,部分变量声明在此简略
[code:1]
unsigned char bcaddr[4] = {0xC0, 0xA8, 0x01, 0xFF}; /*192.168.1.255 */
/*
** establish a UDP broadcast socket
*/
b_fd = socket (AF_INET, SOCK_DGRAM, 0);
// memset ((char *) &broadcast, 0, len);
bzero(&broadcast, sizeof(broadcast));
broadcast.sin_family = AF_INET;
broadcast.sin_port = htons (BROADCAST);
// memcpy(&broadcast.sin_addr, bcaddr, 4);
broadcast.sin_addr.s_addr = inet_addr("224.0.0.1");
setsockopt (b_fd, SOL_SOCKET, SO_BROADCAST, (char *) &opt, sizeof (opt));
/ *
* start up all remote slave services
*/
gethostname (hostname, sizeof (hostname));
if ((p = strchr (hostname, '.')) != NULL)
{
*p = '\0'; /* trunc .domainname */
}
sendto (b_fd, hostname, strlen (hostname) + 1, 0,
(struct sockaddr *) &broadcast, len);
close (b_fd);
[/code:1] |
|