A simple Ethernet broadcast transceiver example

xiaoxiao2021-03-06  39

Http://www.opc-china.com Collect #include #include #include #include #include #include < Windows.h> #pragma Comment (lib, "ws2_32.lib") void main (void) {socket s; sockaddr_in from, a; wsadata wsdata; bool optval; // Start Socket library, version 2.0 wsastartup (0x0202, & wsdata ); Optval = true; // then assigns two addresses, one for binding sockets, one to receive messages from broadcast addresses from the network; A.SIN_FAMILY = AF_INET; A.SIN_ADDR.S_ADDR = 0; A.SIN_PORT = HTONS (5050);

from.sin_family = AF_INET; from.sin_addr.s_addr = INADDR_BROADCAST; from.sin_port = htons (5050); int fromlength = sizeof (SOCKADDR); // initialize UDP socket with s = socket (AF_INET, SOCK_DGRAM, 0); / / Set the socket for broadcast type, setsockopt (s, s, s, sufar *) & optval, sizeof (optVal)); bind (s, (sockaddr *) & a, sizeof (sockaddr_in)); char BUF [256]; WHILE (1) {// receive messages from the broadcast address, pay attention to the address used to bind the address and the address of the received message is a different RECVFROM (S, BUF, 256, 0, (Struct SockAddr Far *) & from (int far *); Sleep (2000); Printf ("% s / n", buf); zeromemory (buf, 256);}}}}} #include #include #include #include #include #include

转载请注明原文地址:https://www.9cbs.com/read-62266.html

New Post(0)