annotate resources/network/udp-client.c @ 346:c6a15a8dee02
prerelease
Remapped analog output channels to match 01234567 order on cape. Note: this will change the behaviour of all these existing examples using analog out, notably d-box and cape_test.
author |
andrewm |
date |
Tue, 07 Jun 2016 18:37:11 +0100 |
parents |
adcb57fd3d75 |
children |
|
rev |
line source |
giuliomoro@24
|
1 /* UDP client in the internet domain */
|
giuliomoro@24
|
2 #include <sys/types.h>
|
giuliomoro@24
|
3 #include <sys/socket.h>
|
giuliomoro@24
|
4 #include <netinet/in.h>
|
giuliomoro@24
|
5 #include <arpa/inet.h>
|
giuliomoro@24
|
6 #include <netdb.h>
|
giuliomoro@24
|
7 #include <stdio.h>
|
giuliomoro@24
|
8 #include <stdlib.h>
|
giuliomoro@24
|
9 #include <unistd.h>
|
giuliomoro@24
|
10 #include <string.h>
|
giuliomoro@24
|
11
|
giuliomoro@24
|
12 void error(const char *);
|
giuliomoro@24
|
13 int main(int argc, char *argv[])
|
giuliomoro@24
|
14 {
|
giuliomoro@24
|
15 int sock, n;
|
giuliomoro@24
|
16 unsigned int length;
|
giuliomoro@24
|
17 struct sockaddr_in server, from;
|
giuliomoro@24
|
18 struct hostent *hp;
|
giuliomoro@24
|
19 char buffer[256];
|
giuliomoro@24
|
20
|
giuliomoro@24
|
21 if (argc != 3) { printf("Usage: server port\n");
|
giuliomoro@24
|
22 exit(1);
|
giuliomoro@24
|
23 }
|
giuliomoro@24
|
24
|
giuliomoro@24
|
25 server.sin_family = AF_INET;
|
giuliomoro@24
|
26 hp = gethostbyname(argv[1]);
|
giuliomoro@24
|
27 if (hp==0) error("Unknown host");
|
giuliomoro@24
|
28
|
giuliomoro@24
|
29 bcopy((char *)hp->h_addr,
|
giuliomoro@24
|
30 (char *)&server.sin_addr,
|
giuliomoro@24
|
31 hp->h_length);
|
giuliomoro@24
|
32 server.sin_port = htons(atoi(argv[2]));
|
giuliomoro@24
|
33 length=sizeof(struct sockaddr_in);
|
giuliomoro@24
|
34 while (1){
|
giuliomoro@24
|
35 sock= socket(AF_INET, SOCK_DGRAM, 0);
|
giuliomoro@24
|
36 if (sock < 0) error("socket");
|
giuliomoro@24
|
37 bzero(buffer,256);
|
giuliomoro@24
|
38 printf("Please enter the message: ");
|
giuliomoro@24
|
39 fgets(buffer,255,stdin);
|
giuliomoro@24
|
40 n=sendto(sock,buffer,
|
giuliomoro@24
|
41 strlen(buffer),0,(const struct sockaddr *)&server,length);
|
giuliomoro@24
|
42 if (n < 0) error("Sendto");
|
giuliomoro@24
|
43 }
|
giuliomoro@24
|
44 close(sock);
|
giuliomoro@24
|
45 return 0;
|
giuliomoro@24
|
46 }
|
giuliomoro@24
|
47
|
giuliomoro@24
|
48 void error(const char *msg)
|
giuliomoro@24
|
49 {
|
giuliomoro@24
|
50 perror(msg);
|
giuliomoro@24
|
51 exit(0);
|
giuliomoro@24
|
52 }
|