c - Sending broadcast in Linux via Sockets -
solved please close question (but not know how :/ bad day)
im trying send broadcast in linux via sockets, going out through both interfaces(ive got active eth0 , eth1, both in different segments), suddelny, going out through first one, eth0
here code:
void sendbroad(char *dstip, char *localip) { int sock; /* socket */ struct sockaddr_in broadcastaddr; /* broadcast address */ int broadcastpermission; /* socket opt set permission broadcast */ unsigned int localiplen; /* length of string broadcast */ /* create socket sending/receiving datagrams */ if ((sock = socket(pf_inet, sock_dgram, ipproto_udp)) < 0) perror("socket() failed"); /* set socket allow broadcast */ broadcastpermission = 1; if (setsockopt(sock, sol_socket, so_broadcast, (void *) &broadcastpermission, sizeof(broadcastpermission)) < 0) perror("setsockopt() failed"); /* construct local address structure */ memset(&broadcastaddr, 0, sizeof(broadcastaddr)); /* 0 out structure */ broadcastaddr.sin_family = af_inet; /* internet address family */ broadcastaddr.sin_addr.s_addr = inet_addr(dstip); /* broadcast ip address */ broadcastaddr.sin_port = htons(broadcastport); /* broadcast port */ localiplen = strlen(localip); /* find length of localip */ int j; (j=0; j<1; j++) //doesnt mean far, not important { /* broadcast localip in datagram clients */ if (sendto(sock, localip, localiplen, 0, (struct sockaddr *) &broadcastaddr, sizeof(broadcastaddr)) != localiplen) perror("sendto() sent different number of bytes expected"); } }
any issue?
thanks in advance!
just had make fix broadcast ports, mixed. code ok
Comments
Post a Comment