这篇教程C++ sendto函数代码示例写得很实用,希望能帮到您。
本文整理汇总了C++中sendto函数的典型用法代码示例。如果您正苦于以下问题:C++ sendto函数的具体用法?C++ sendto怎么用?C++ sendto使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。 在下文中一共展示了sendto函数的29个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。 示例1: main//.........这里部分代码省略......... perror("socket(DIVERT)"); exit(2); } sd = r; sin.sin_port = htons(portnum); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; if (bind(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) { perror("bind(divert)"); exit(3); } p = pcap_open_dead(DLT_RAW, BUFMAX); dp = pcap_dump_open(p, dumpf); if (dp == NULL) { pcap_perror(p, dumpf); exit(4); } okay(portnum); nfd = sd + 1; for (;;) { FD_ZERO(&rds); FD_SET(sd, &rds); r = select(nfd, &rds, NULL, NULL, NULL); if (r == -1) { if (errno == EINTR) continue; perror("select"); quit(11); } if (!FD_ISSET(sd, &rds)) /* hmm. no work. */ continue; /* * use recvfrom(3 and sendto(3) as in natd(8). * see /usr/src/sbin/natd/natd.c * see ipfw(8) about using 'divert' and 'tee'. */ /* * read packet. */ l = sizeof(sin); nr = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, &l);if (debug) fprintf(stderr, "recvfrom(%d) = %zd (%d)/n", sd, nr, l); if (nr < 0 && errno != EINTR) { perror("recvfrom(sd)"); quit(12); } if (nr <= 0) continue; if (reflect) { /* * write packet back so it can continue * being processed by any further IPFW rules. */ l = sizeof(sin); r = sendto(sd, buf, nr, 0, (struct sockaddr *)&sin, l);if (debug) fprintf(stderr, " sendto(%d) = %d/n", sd, r); if (r < 0) { perror("sendto(sd)"); quit(13); } } /* * check maximums, if any. * but don't quit if must continue reflecting packets. */ if (maxpkts) { totpkts++; if (totpkts > maxpkts) { if (reflect == 1) continue; quit(0); } } if (maxbytes) { totbytes += nr; if (totbytes > maxbytes) { if (reflect == 1) continue; quit(0); } } /* * save packet in tcpdump(1) format. see pcap(3). * divert packets are fully assembled. see ipfw(8). */ (void) gettimeofday(&(phd.ts), NULL); phd.caplen = phd.len = nr; pcap_dump((u_char *)dp, &phd, buf); if (ferror((FILE *)dp)) { perror(dumpf); quit(14); } (void) fflush((FILE *)dp); } quit(0);}
开发者ID:FreeBSDFoundation,项目名称:freebsd,代码行数:101,
示例2: mainint main(){ int s, len, nbytes, one = 1; static struct sockaddr_in sin; static struct ip_mreq imr; char buf[100]; if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); return 1; } if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof (one)) == -1) { perror("setsockopt: SO_REUSEADDR"); exit(1); }#ifdef SO_REUSEPORT /* * This must be added for OSF1 v2.x (and BSD 4.3) */ if (setsockopt (s, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof (one)) == -1) { perror("setsockopt: SO_REUSEADDR"); exit(1); }#endif sin.sin_family = AF_INET; sin.sin_port = htons (PORT); sin.sin_addr.s_addr = htonl (INADDR_ANY); if (bind (s, (struct sockaddr *) & sin, sizeof (sin)) == -1) { perror ("bind"); return 1; } /* * the original posting was = htonl(inet_addr (GROUP)) * which is wrong. * * Send greeting message to multicast group: */ sin.sin_addr.s_addr = inet_addr (GROUP); if (sendto (s, "Hi!", 4, 0, (struct sockaddr *) & sin, sizeof (sin)) == -1) { perror("socket"); return 1; } /* * Join the group: * IP multicast address of group: * local IP address of interface: */ imr.imr_multiaddr = sin.sin_addr; imr.imr_interface.s_addr = htonl (INADDR_ANY); if (setsockopt (s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &imr, sizeof (imr)) == -1) { /* * The original posting did not include this: * [email C++ sendto_channel_local函数代码示例 C++ sendmessage函数代码示例
|