1 | /* |
---|
2 | * serverUdp.c : generate an executable to test the UDP Sockets. |
---|
3 | * the RTEMS target must be configured as client. |
---|
4 | * Don't forget to change the IP address. |
---|
5 | * |
---|
6 | * $Id$ |
---|
7 | */ |
---|
8 | |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <string.h> |
---|
12 | #include <errno.h> |
---|
13 | #include <sys/time.h> |
---|
14 | |
---|
15 | #include <sys/param.h> |
---|
16 | #include <sys/socket.h> |
---|
17 | #include <sys/file.h> |
---|
18 | |
---|
19 | #include <netinet/in_systm.h> |
---|
20 | #include <netinet/in.h> |
---|
21 | #include <netinet/ip.h> |
---|
22 | #include <arpa/inet.h> |
---|
23 | |
---|
24 | #include "../buffer.h" |
---|
25 | |
---|
26 | #define DEFAULT_IP "194.2.81.126" |
---|
27 | #define DEFAULT_LISTEN_IP "194.2.81.61" |
---|
28 | |
---|
29 | int main(int argc, char** argv) |
---|
30 | { |
---|
31 | |
---|
32 | int sd; |
---|
33 | struct sockaddr_in server, from; |
---|
34 | int fromlen; |
---|
35 | char *tabChar; |
---|
36 | int rc; |
---|
37 | unsigned long msgId; |
---|
38 | char *server_name= "localhost"; |
---|
39 | unsigned short port; |
---|
40 | |
---|
41 | port = DEFAULT_PORT_CLIENT; |
---|
42 | server_name = DEFAULT_IP; |
---|
43 | |
---|
44 | bzero(&server, sizeof(server)); |
---|
45 | server.sin_family = AF_INET; |
---|
46 | inet_aton (server_name, (struct in_addr*)&(server.sin_addr.s_addr)); |
---|
47 | |
---|
48 | server.sin_port = htons(port); |
---|
49 | |
---|
50 | sd = socket(AF_INET,SOCK_DGRAM, 0); |
---|
51 | if (sd == -1) { |
---|
52 | printf(" socket failed errno = %d\n", errno); |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | rc = bind(sd, (struct sockaddr *)&server, sizeof(server)) ; |
---|
57 | if (rc == -1) { |
---|
58 | printf(" bind failed errno = %d\n", errno); |
---|
59 | return 0; |
---|
60 | } |
---|
61 | |
---|
62 | tabChar = AllocBuffer(); |
---|
63 | |
---|
64 | if(!tabChar) { |
---|
65 | printf("malloc failed ...\n"); |
---|
66 | return 0; |
---|
67 | } |
---|
68 | |
---|
69 | bzero(&from, sizeof(from)); |
---|
70 | from.sin_family = AF_INET; |
---|
71 | inet_aton (DEFAULT_LISTEN_IP, (struct in_addr*)&(from.sin_addr.s_addr)); |
---|
72 | fromlen = sizeof(from); |
---|
73 | |
---|
74 | printf("UDP Server : ready to receive messages...\n"); |
---|
75 | |
---|
76 | msgId = 0; |
---|
77 | |
---|
78 | for(;;){ |
---|
79 | rc=recvfrom(sd, tabChar, SIZE_MAX_RCV_BUFFER, 0, (struct sockaddr *)&from, &fromlen); |
---|
80 | |
---|
81 | if (rc < 0) { |
---|
82 | printf("\n Recvfrom error %d\n\n",errno); |
---|
83 | } |
---|
84 | else{ |
---|
85 | msgId = CheckBuffer(tabChar); |
---|
86 | } |
---|
87 | if (msgId == 0) |
---|
88 | break ; |
---|
89 | } |
---|
90 | |
---|
91 | FreeBuffer(tabChar); |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|