Mercurial > hg > beaglert
comparison resources/network/udp-client.c @ 28:adcb57fd3d75 bbb_network
- added a c script to have the oscillator perform a sine sweep
- added documentation for the network
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sun, 10 May 2015 13:55:43 +0100 |
parents | resources/udp-client.c@ad5cd8dd99b3 |
children |
comparison
equal
deleted
inserted
replaced
27:d358d4410d01 | 28:adcb57fd3d75 |
---|---|
1 /* UDP client in the internet domain */ | |
2 #include <sys/types.h> | |
3 #include <sys/socket.h> | |
4 #include <netinet/in.h> | |
5 #include <arpa/inet.h> | |
6 #include <netdb.h> | |
7 #include <stdio.h> | |
8 #include <stdlib.h> | |
9 #include <unistd.h> | |
10 #include <string.h> | |
11 | |
12 void error(const char *); | |
13 int main(int argc, char *argv[]) | |
14 { | |
15 int sock, n; | |
16 unsigned int length; | |
17 struct sockaddr_in server, from; | |
18 struct hostent *hp; | |
19 char buffer[256]; | |
20 | |
21 if (argc != 3) { printf("Usage: server port\n"); | |
22 exit(1); | |
23 } | |
24 | |
25 server.sin_family = AF_INET; | |
26 hp = gethostbyname(argv[1]); | |
27 if (hp==0) error("Unknown host"); | |
28 | |
29 bcopy((char *)hp->h_addr, | |
30 (char *)&server.sin_addr, | |
31 hp->h_length); | |
32 server.sin_port = htons(atoi(argv[2])); | |
33 length=sizeof(struct sockaddr_in); | |
34 while (1){ | |
35 sock= socket(AF_INET, SOCK_DGRAM, 0); | |
36 if (sock < 0) error("socket"); | |
37 bzero(buffer,256); | |
38 printf("Please enter the message: "); | |
39 fgets(buffer,255,stdin); | |
40 n=sendto(sock,buffer, | |
41 strlen(buffer),0,(const struct sockaddr *)&server,length); | |
42 if (n < 0) error("Sendto"); | |
43 } | |
44 close(sock); | |
45 return 0; | |
46 } | |
47 | |
48 void error(const char *msg) | |
49 { | |
50 perror(msg); | |
51 exit(0); | |
52 } |