// UDP брудкаст не работает... :( #include <stdio.h> #include <sys/types.h> #include <unistd.h>
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>
#include <errno.h>
#define MAX_BUF 1024 #define PORT 1234
int main() { int sockd; struct sockaddr_in addr_host; struct sockaddr_in addr_dest;
char buf[MAX_BUF]; int status; int addrlen;
/* create a socket */ sockd = socket(AF_INET, SOCK_DGRAM, 0); printf("socket: %d\n", sockd); if (sockd == -1) { perror("Socket creation error"); exit(1); }
/* host address */ addr_host.sin_family = AF_INET; inet_aton("192.168.73.48", &addr_host.sin_addr); addr_host.sin_port = 0;
status = bind(sockd, (struct sockaddr*)&addr_host, sizeof(addr_host)); printf("bind: %d\n", status); if(status<0) perror("bind");
strcpy(buf, "Hello world!");
/* dest address */ addr_dest.sin_family = AF_INET; // inet_aton("192.168.73.255", &addr_dest.sin_addr); addr_dest.sin_addr.s_addr = INADDR_BROADCAST; addr_dest.sin_port = htons(PORT);
status = sendto(sockd, buf, strlen(buf)+1,0, (struct sockaddr*)&addr_dest, sizeof(addr_dest)); printf("sendto: %d\n", status); if(status<0) { perror("sendto"); // printf("%s\n",strerror(errno)); } sleep(1);
return 0; }