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 43 44 45 46
|
//Socket Client
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_TAILLE 10
int main (int argc, char *argv[]) {
/*******************************************/
char buffer[BUFFER_TAILLE]="ip address";
int sock;
int on = 1;
struct sockaddr_in sin;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
exit(1);
}
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
&on, sizeof(on)) < 0) {
perror("setsockopt");
exit(1);
}
sin.sin_family = PF_INET;
sin.sin_addr.s_addr = INADDR_BROADCAST;
sin.sin_port = htons(1111);
if (bind(sock, (struct sockaddr *)&sin,
sizeof(sin)) < 0) {
perror("bind");
exit(1);
}
write(sock,buffer, 10);
close(sock);
/*************************************************************/
return 0;
} |
Partager