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