[f3b4c5a] | 1 | /* |
---|
| 2 | * serverTcp.c : generate an executable to test the TCP Sockets. |
---|
| 3 | * the RTEMS target must be configured as client. |
---|
| 4 | * Don't forget to change the IP address. |
---|
[7ba2ac5] | 5 | * |
---|
| 6 | * $Id$ |
---|
[f3b4c5a] | 7 | */ |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <string.h> |
---|
| 12 | #include <errno.h> |
---|
| 13 | #include <unistd.h> |
---|
| 14 | #include <sys/time.h> |
---|
| 15 | |
---|
| 16 | #include <sys/param.h> |
---|
| 17 | #include <sys/socket.h> |
---|
| 18 | #include <sys/file.h> |
---|
| 19 | |
---|
| 20 | #include <netinet/in_systm.h> |
---|
| 21 | #include <netinet/in.h> |
---|
| 22 | #include <netinet/ip.h> |
---|
| 23 | #include <arpa/inet.h> |
---|
| 24 | #include <netinet/ip_icmp.h> |
---|
| 25 | #include <netdb.h> |
---|
| 26 | |
---|
| 27 | #include "../buffer.h" |
---|
| 28 | |
---|
| 29 | #define DEFAULT_IP "194.2.81.126" |
---|
| 30 | #define DEFAULT_LISTEN_IP "194.2.81.61" |
---|
| 31 | |
---|
| 32 | int main(int argc, char** argv) |
---|
| 33 | { |
---|
| 34 | int sd, psd; |
---|
| 35 | struct sockaddr_in server; |
---|
| 36 | struct sockaddr_in from; |
---|
| 37 | int fromlen; |
---|
| 38 | char *tabChar; |
---|
| 39 | int rc; |
---|
| 40 | int i; |
---|
| 41 | char *server_name= "localhost"; |
---|
| 42 | unsigned short port; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | printf("main begin...\n"); |
---|
| 46 | port = DEFAULT_PORT_CLIENT; |
---|
| 47 | server_name = DEFAULT_IP; |
---|
| 48 | |
---|
| 49 | bzero(&server, sizeof(server)); |
---|
| 50 | server.sin_family = AF_INET; |
---|
| 51 | |
---|
| 52 | if (inet_aton (server_name, (struct in_addr*)&(server.sin_addr.s_addr)) == 0) { |
---|
| 53 | printf("Usage : %s IP address in ascii format : xxx.xxx.xxx.xxx\n", |
---|
| 54 | argv[0]); |
---|
| 55 | return 1; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | server.sin_port = htons(port); |
---|
| 59 | |
---|
| 60 | sd = socket (PF_INET,SOCK_STREAM,0); |
---|
| 61 | printf("socket() returns <%d>\n", sd); |
---|
| 62 | |
---|
| 63 | if (sd<0) { |
---|
| 64 | printf("socket() call failure: opening stream socket\n"); |
---|
| 65 | exit(-1); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | rc = bind( sd, &server, sizeof(server) ) ; |
---|
| 69 | if (rc == -1) { |
---|
| 70 | printf("%s : bind failed errno = %d\n", argv[0], errno); |
---|
| 71 | close(sd); |
---|
| 72 | return 1; |
---|
| 73 | } |
---|
| 74 | printf("bind() returns <%d>\n", rc); |
---|
| 75 | |
---|
| 76 | printf("Server Port is: %d\n", ntohs(server.sin_port)); |
---|
| 77 | rc = listen(sd,5); |
---|
| 78 | printf("listen() returns <%d>\n", rc); |
---|
| 79 | fromlen = sizeof(from); |
---|
| 80 | |
---|
| 81 | tabChar = AllocBuffer(); |
---|
| 82 | if(!tabChar) { |
---|
| 83 | printf("malloc failed ...\n"); |
---|
| 84 | return 1; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | psd = accept(sd, &from, &fromlen); |
---|
| 88 | printf("accept() returns <%d>\n", psd); |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | printf("TCP Server : ready to receive messages...\n"); |
---|
| 92 | |
---|
| 93 | for(;;){ |
---|
| 94 | rc = recv(psd, tabChar, SIZE_MAX_RCV_BUFFER, 0 ); |
---|
| 95 | |
---|
| 96 | if (rc == -1) { |
---|
| 97 | printf("%s : recv() failed errno = %d\n", argv[0], errno); |
---|
| 98 | close(sd); |
---|
| 99 | return 1; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | if (rc == 0) { |
---|
| 103 | continue; |
---|
| 104 | } |
---|
| 105 | if (CheckBuffer(tabChar) == 0){ |
---|
| 106 | printf("Received %d bytes, data [%s] from client\n",rc,tabChar); |
---|
| 107 | printf("Packet received:<%d> bytes\n", rc); |
---|
| 108 | printf("Received:<"); |
---|
| 109 | tabChar += 8; |
---|
| 110 | for (i=0; i<rc ; i++) printf("%c",tabChar[i]); |
---|
| 111 | printf(">\n"); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | FreeBuffer(tabChar); |
---|
| 116 | |
---|
| 117 | } |
---|
| 118 | |
---|