1 | /* |
---|
2 | * clientTcp.c : generate an executable to test the TCP Sockets. |
---|
3 | * the RTEMS target must be configured as server. |
---|
4 | * Don't forget to change the IP address. |
---|
5 | */ |
---|
6 | |
---|
7 | #include <stdio.h> |
---|
8 | #include <string.h> |
---|
9 | #include <errno.h> |
---|
10 | #include <unistd.h> |
---|
11 | #include <sys/time.h> |
---|
12 | |
---|
13 | #include <sys/param.h> |
---|
14 | #include <sys/socket.h> |
---|
15 | #include <sys/file.h> |
---|
16 | |
---|
17 | #include <netinet/in_systm.h> |
---|
18 | #include <netinet/in.h> |
---|
19 | #include <netinet/ip.h> |
---|
20 | #include <arpa/inet.h> |
---|
21 | #include <netinet/ip_icmp.h> |
---|
22 | #include <netdb.h> |
---|
23 | |
---|
24 | #include "../buffer.h" |
---|
25 | |
---|
26 | #define DEFAULT_IP "194.2.81.61" |
---|
27 | #define DEFAULT_LISTEN_IP "194.2.81.126" |
---|
28 | |
---|
29 | int main(int argc, char** argv ) |
---|
30 | { |
---|
31 | int sd; |
---|
32 | struct sockaddr_in server; |
---|
33 | struct hostent *hp; |
---|
34 | struct sockaddr_in from; |
---|
35 | int fromlen; |
---|
36 | int i,retval; |
---|
37 | char *tabChar; |
---|
38 | char *server_name= "localhost"; |
---|
39 | unsigned short port; |
---|
40 | unsigned long lenChar; |
---|
41 | struct timespec timeToWait, timeRem; |
---|
42 | |
---|
43 | printf("main begin...\n"); |
---|
44 | port = DEFAULT_PORT_SERVER; |
---|
45 | server_name = DEFAULT_IP; |
---|
46 | |
---|
47 | bzero(&server, sizeof(server)); |
---|
48 | |
---|
49 | sd = socket (AF_INET,SOCK_STREAM,0); |
---|
50 | printf("socket() result:<%d>\n", sd); |
---|
51 | |
---|
52 | if (sd<0) { |
---|
53 | printf("opening stream socket\n"); |
---|
54 | exit(-1); |
---|
55 | } |
---|
56 | |
---|
57 | if (inet_aton (server_name, (struct in_addr*)&(server.sin_addr.s_addr)) == 0) { |
---|
58 | printf("%s : IP address: bad format\n", |
---|
59 | argv[0]); |
---|
60 | } |
---|
61 | |
---|
62 | server.sin_family = AF_INET; |
---|
63 | server.sin_port = htons(port); |
---|
64 | |
---|
65 | /* Connect to TCP/SERVER */ |
---|
66 | if ( (retval = connect(sd, &server, sizeof(server))) < 0 ) { |
---|
67 | close(sd); |
---|
68 | perror("connecting stream socket"); |
---|
69 | exit(0); |
---|
70 | } |
---|
71 | printf("connect() result:<%d>\n", retval); |
---|
72 | fromlen = sizeof(from); |
---|
73 | if ( (retval = getpeername(sd,&from,&fromlen)) <0){ |
---|
74 | perror("could't get peername\n"); |
---|
75 | exit(1); |
---|
76 | } |
---|
77 | printf("getpeername() result:<%d>\n", retval); |
---|
78 | printf("Connected to TCP/Server:"); |
---|
79 | printf("%s:%d\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port)); |
---|
80 | |
---|
81 | if ((hp = gethostbyaddr((const char *)(&from.sin_addr.s_addr), |
---|
82 | sizeof(from.sin_addr.s_addr),AF_INET)) == NULL) |
---|
83 | fprintf(stderr, "Can't find host %s\n", inet_ntoa(from.sin_addr)); |
---|
84 | else |
---|
85 | printf("(Name is : %s)\n", hp->h_name); |
---|
86 | |
---|
87 | printf("Now, let's send packets..."); |
---|
88 | |
---|
89 | while(1) { |
---|
90 | |
---|
91 | tabChar = BuildBuffer(); |
---|
92 | lenChar = *((unsigned long *)(tabChar)); |
---|
93 | |
---|
94 | i = send(sd, tabChar, lenChar, 0); |
---|
95 | if (i == -1) |
---|
96 | { |
---|
97 | fprintf(stderr,"send() failed (at least locally detected errors)\n"); |
---|
98 | return -1; |
---|
99 | } |
---|
100 | timeToWait.tv_sec = 1; |
---|
101 | timeToWait.tv_nsec = 0; |
---|
102 | nanosleep(&timeToWait, &timeRem); |
---|
103 | |
---|
104 | FreeBuffer(tabChar); |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | close(sd); |
---|
109 | exit (0); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|