Chcem sa spytat autora clanku, ci skusal pracovat so soketmi cisto objektovym pristupm. Mam na mysli ci skusal napisat si C++ program, ktory napr. posiela UDP segmenty...
Asi nie, pretoze by to nenazval C/C++ a sokety... skuste si to zabalit do objektu, prekompilot s C++ a uvide tu srandu. Nakoniec zistite, ze to neprekompiluje. NAto, aby som mohol vytvorit soket na UDP a posielat broadcasty, som musel pouzit toto:
if( (sd = socket(PF_INET, SOCK_DGRAM, 0)) == -1 )
{
fprintf(stderr, "Error, cannot create socket\n");
return;
}
if( setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1 )
{
fprintf(stderr, "Error: setsockopt: \"%s\"\n", strerror(errno));
close( sd );
return;
}
struct sockaddr_in *sock = new struct sockaddr_in();
sock->sin_family = PF_INET;
sock->sin_addr.s_addr = htonl(INADDR_ANY);
sock->sin_port = htons( port );
addr = reinterpret_cast<struct sockaddr*>(sock);
if( bind(sd, addr, sizeof(struct sockaddr_in)) == -1 )
{
fprintf(stderr, "Error: bind: \"%s\"\n", strerror(errno));
close( sd );
return;
}
if( setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1 )
{
fprintf(stderr, "Error: setsockopt: \"%s\"\n", strerror(errno));
close( sd );
return;
}
---------------
Chcem upozornit hlavne na tuto haluz:
addr = reinterpret_cast<struct sockaddr*>(sock);
bez tohto vam to urcite nepoujde, tak prosim nenazyvat tento serial tak odvazne, ze C/C++ ale radsej iba C...