-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalker.c
76 lines (72 loc) · 2.4 KB
/
talker.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* program: talker
author: Oscar Sanchez
description: Sends a message to be echoed by the listener on a given port.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int main(int argc, char ** argv) {
int verbose=0; //verbose output?
int port=0; //what port we are talking to
char* host; //where the host is located
char* message = argv[argc-1]; //what we are sending
int c; //for processing input arguments
/* run through the program arguments and error check------------------------*/
while((c=getopt(argc,argv,"vp:h:"))!=-1)
switch(c) {
case 'v':
verbose=1; //verbose output true
break;
case 'p':
port=atoi(optarg); //the port we're binding to
break;
case 'h':
host=optarg; //the host
break;
case '?': //error checking
fprintf(stderr,"Usage is: talker [-v] -p port -h hostname\n");
return 1;
default:
abort();
}
//check port again, just in case the input arguments were misread
if(port<=0) {
fprintf(stderr,"Usage is: talker [-v] -p port -h hostname\n");
return 1;
}
/* network connection-------------------------------------------------------*/
int sockfd;
struct sockaddr_in their_addr; //Connector’s address information
struct hostent *he;
int numbytes;
if ((he=gethostbyname(host)) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(port); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr_list[0]);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
if ((numbytes=sendto(sockfd, message, strlen(message), 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("sendto");
exit(1);
}
if(verbose) {
printf("sent packet to %s port %d\n",inet_ntoa(their_addr.sin_addr),port);
}
printf("packet contents:\n%s\n",message);
close(sockfd); //Close the socket!
return 0;
}