better names for client/server
[irssi-notify.git] / notify.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <netdb.h>
7 #include <sys/types.h>
8 #include <netinet/in.h>
9 #include <sys/socket.h>
10
11 #include <arpa/inet.h>
12
13 #define PORT "3490" // the port client will be connecting to
14
15 #define MAXDATASIZE 100 // max number of bytes we can get at once
16
17 // get sockaddr, IPv4 or IPv6:
18 void *get_in_addr(struct sockaddr *sa)
19 {
20 if (sa->sa_family == AF_INET) {
21 return &(((struct sockaddr_in*)sa)->sin_addr);
22 }
23
24 return &(((struct sockaddr_in6*)sa)->sin6_addr);
25 }
26
27 int main(int argc, char *argv[])
28 {
29 int sockfd, numbytes;
30 char buf[MAXDATASIZE];
31 struct addrinfo hints, *servinfo, *p;
32 int rv;
33 char s[INET6_ADDRSTRLEN];
34
35 if (argc != 3) {
36 fprintf(stderr,"usage: client hostname 'message'\n");
37 exit(1);
38 }
39
40 memset(&hints, 0, sizeof hints);
41 hints.ai_family = AF_UNSPEC;
42 hints.ai_socktype = SOCK_STREAM;
43
44 if ((rv = getaddrinfo(argv[1], PORT, &hints, &servinfo)) != 0) {
45 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
46 return 1;
47 }
48
49 // loop through all the results and connect to the first we can
50 for(p = servinfo; p != NULL; p = p->ai_next) {
51 if ((sockfd = socket(p->ai_family, p->ai_socktype,
52 p->ai_protocol)) == -1) {
53 perror("client: socket");
54 continue;
55 }
56
57 if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
58 close(sockfd);
59 perror("client: connect");
60 continue;
61 }
62
63 break;
64 }
65
66 if (p == NULL) {
67 fprintf(stderr, "client: failed to connect\n");
68 return 2;
69 }
70
71 if (send(sockfd, argv[2], strlen(argv[2])+1, 0) == -1)
72 perror("send");
73
74 close(sockfd);
75
76 return 0;
77 }