qshi
8 years ago
4 changed files with 88 additions and 84 deletions
@ -1,82 +1,85 @@ |
|||||
#include<stdio.h> |
#include<stdio.h> |
||||
#include<stdlib.h> |
#include<stdlib.h> |
||||
#include<string.h> |
#include<string.h> |
||||
#include <signal.h> |
#include <signal.h> |
||||
#include<unistd.h> |
#include<unistd.h> |
||||
#include<sys/types.h> |
#include<sys/types.h> |
||||
#include<sys/socket.h> |
#include<sys/socket.h> |
||||
#include<netinet/in.h> |
#include<netinet/in.h> |
||||
#include <arpa/inet.h> |
#include <arpa/inet.h> |
||||
#include <errno.h> |
#include <errno.h> |
||||
|
|
||||
#define HOST_IF_IP "192.168.0.191" |
#define HOST_IF_IP "192.168.31.89" |
||||
#define GROUP_ADDR "225.0.0.5" |
#define GROUP_ADDR "225.0.0.5" |
||||
#define PORTRECV 8310 |
#define PORTRECV 8310 |
||||
|
|
||||
static int got_sig = 0; |
static int got_sig = 0; |
||||
|
|
||||
static void sigterm(int nr) |
static void sigterm(int nr) |
||||
{ |
{ |
||||
got_sig = 1; |
got_sig = 1; |
||||
} |
} |
||||
|
|
||||
int main(int argc, char *argv[]) |
int main(int argc, char *argv[]) |
||||
{ |
{ |
||||
FILE *myfile; |
FILE *myfile; |
||||
int sock, count; |
int sock, count; |
||||
socklen_t slen; |
socklen_t slen; |
||||
struct sockaddr_in ser_addr; |
struct sockaddr_in ser_addr; |
||||
struct ip_mreq mreq; |
struct ip_mreq mreq; |
||||
int yes=1; |
int yes=1; |
||||
unsigned char buf[64] = {0}; |
unsigned char buf[64] = {0}; |
||||
|
|
||||
signal(SIGTERM, sigterm); |
signal(SIGINT, sigterm); |
||||
|
sock = socket(AF_INET,SOCK_DGRAM,0); |
||||
sock = socket(AF_INET,SOCK_DGRAM,0); |
printf("sockfd: %d\n",sock); |
||||
printf("sockfd: %d\n",sock); |
if (sock == -1) { |
||||
if (sock == -1) { |
printf("socket error"); |
||||
printf("socket error"); |
exit(1); |
||||
exit(1); |
} |
||||
} |
|
||||
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0){ |
||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0){ |
perror("Reusing ADDR failed"); |
||||
perror("Reusing ADDR failed"); |
exit(1); |
||||
exit(1); |
} |
||||
} |
|
||||
|
memset(&ser_addr,0,sizeof(ser_addr)); |
||||
memset(&ser_addr,0,sizeof(ser_addr)); |
ser_addr.sin_family = AF_INET; |
||||
ser_addr.sin_family = AF_INET; |
ser_addr.sin_addr.s_addr = INADDR_ANY;//inet_addr(GROUP_ADDR);
|
||||
ser_addr.sin_addr.s_addr = inet_addr(HOST_IF_IP); |
ser_addr.sin_port = htons(PORTRECV); |
||||
ser_addr.sin_port = htons(PORTRECV); |
if (bind(sock,(struct sockaddr *) &ser_addr,sizeof(ser_addr)) < 0){ |
||||
if (bind(sock,(struct sockaddr *) &ser_addr,sizeof(ser_addr)) < 0){ |
perror("bind"); |
||||
perror("bind"); |
exit(1); |
||||
exit(1); |
} |
||||
} |
|
||||
|
mreq.imr_multiaddr.s_addr = inet_addr(GROUP_ADDR); |
||||
mreq.imr_multiaddr.s_addr = inet_addr(GROUP_ADDR); |
mreq.imr_interface.s_addr = inet_addr(HOST_IF_IP); |
||||
mreq.imr_interface.s_addr = inet_addr(HOST_IF_IP); |
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) { |
||||
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) { |
perror("setsock"); |
||||
perror("setsock"); |
close(sock); |
||||
close(sock); |
exit(1); |
||||
exit(1); |
} |
||||
} |
|
||||
myfile = fopen("output.raw", "w"); |
myfile = fopen("output.raw", "w"); |
||||
while (!got_sig) { |
|
||||
slen = sizeof ser_addr; |
while (!got_sig) { |
||||
memset(&ser_addr, 0, sizeof ser_addr); |
slen = sizeof ser_addr; |
||||
memset(buf, 0, sizeof buf); |
memset(&ser_addr, 0, sizeof ser_addr); |
||||
if ((count = recvfrom(sock, buf, sizeof buf, 0, (struct sockaddr *) &ser_addr,(socklen_t*)&slen)) < 0) { |
memset(buf, 0, sizeof buf); |
||||
perror("recvfrom"); |
if ((count = recvfrom(sock, buf, sizeof buf, 0, (struct sockaddr *) &ser_addr,(socklen_t*)&slen)) < 0) { |
||||
exit(1); |
perror("recvfrom"); |
||||
} |
exit(1); |
||||
|
} |
||||
if (count == 64) { |
printf("recv data %d\n", count); |
||||
if (buf[0] == 0xac && buf[1] == 0x93 && buf[63] == 0x14) { |
if (count == 64) { |
||||
fwrite(buf + 16, 1, 6, myfile); |
if (buf[0] == 0xac && buf[1] == 0x93 && buf[63] == 0x14) { |
||||
} |
fwrite(buf + 16, 1, 6, myfile); |
||||
} |
} |
||||
} |
} |
||||
fclose(myfile); |
//fflush(myfile);
|
||||
return 0; |
} |
||||
} |
printf("---------------\n"); |
||||
|
fclose(myfile); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
Loading…
Reference in new issue