1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include <me.h> #define BUF_SIZE 30
int main(int argc,char *argv[]) { int send_sock; struct sockaddr_in broad_adr; FILE *fp; char buf[BUF_SIZE+10]; int so_brd = 1; if (argc != 3) { printf("Usage : %s <Broadcast IP> <PORT>\n",argv[0]); exit(1); }
send_sock = socket(AF_INET,SOCK_DGRAM,0); memset(&broad_adr,0,sizeof(broad_adr)); broad_adr.sin_family = AF_INET; broad_adr.sin_addr.s_addr = inet_addr(argv[1]); broad_adr.sin_port = htons(atoi(argv[2]));
setsockopt(send_sock,SOL_SOCKET,SO_BROADCAST,(void*)&so_brd,sizeof(so_brd)); if ((fp = fopen("news.txt","r")) == NULL) error_handle("fopen() error")
while(!feof(fp)) { fgets(buf,BUF_SIZE,fp); sendto(send_sock,buf,strlen(buf),0,(struct sockaddr*)&broad_adr,sizeof(broad_adr)); char ch = fgetc(fp); if (ch == EOF) break; else sendto(send_sock,&ch,1,0,(struct sockaddr*)&broad_adr,sizeof(broad_adr)); sleep(1); } close(send_sock); return 0; }
|