source: icas_send/icas_send.c @ 246

Last change on this file since 246 was 246, checked in by jazz, 13 years ago
  • icas_send : read blacklist to send UDP packets.
  • icas_recv : receive UDP packets and print messages.


File size: 1.8 KB
Line 
1#include <stdio.h>
2#include <sys/socket.h>
3#include <arpa/inet.h>
4#include <errno.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
8#include <signal.h>
9#include <unistd.h>
10#include <netinet/in.h>
11
12void Die(char *mess)
13{
14    perror(mess);
15    exit(1);
16}
17
18int main(int argc, char *argv[])
19{
20    struct  sockaddr_in send_sockopt;
21    int     send_sock;
22    int     send_length = sizeof(send_sockopt);
23    FILE    *fd;
24    char    BLACK_LIST[1024];
25    size_t  len = 0;
26    ssize_t read;
27
28    if (argc <= 2)
29    {
30  fprintf(stderr, "USAGE: %s <RECV_IP> <RECV_PORT>\n", argv[0]);
31  exit(1);
32    }
33
34    /* open blacklist to read black list data */
35    fd = fopen("blacklist","r");
36    if (fd == NULL)
37    {
38      fprintf(stderr, "%s: Couldn't open file %s; %s\n", argv[0], "blacklist", strerror (errno));
39  exit(1);
40    }
41
42    /* Create the UDP socket to send response to icas_recv */
43    if ((send_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
44    {
45  Die("Failed to create socket for sending black list.");
46    }
47
48    /* Construct the server sockaddr_in structure */
49    memset(&send_sockopt,  0, sizeof(send_sockopt));      /* Clear struct */
50    memset(&BLACK_LIST,    0, sizeof(BLACK_LIST));    /* Clear string */
51    sprintf(BLACK_LIST,"This is a test!");
52
53    // socket option of icas_send socket
54    send_sockopt.sin_family = AF_INET;                   /* Internet/IP */
55    send_sockopt.sin_addr.s_addr = inet_addr(argv[1]);   /* IP address */
56    send_sockopt.sin_port = htons(atoi(argv[2]));        /* server port */
57
58    while(fgets(BLACK_LIST,sizeof(BLACK_LIST),fd))
59    {
60  if (sendto(send_sock, BLACK_LIST, sizeof(BLACK_LIST), 0, 
61       (struct sockaddr *) &send_sockopt, 
62       sizeof(send_sockopt)) != sizeof(BLACK_LIST)) 
63  {
64      Die("1: Mismatch in number of sent bytes");
65  }
66    }
67
68    close(send_sock);
69    exit(0);
70}
Note: See TracBrowser for help on using the repository browser.