source: network-demos/socket/pgmHost/serverTcp.c @ f3b4c5a

4.11netdemos-4-5-branchnetwork-demos-4-10-branchnetwork-demos-4-6-branchnetwork-demos-4-7-branchnetwork-demos-4-8-branchnetwork-demos-4-9-branchrtems-4-5-branch
Last change on this file since f3b4c5a was f3b4c5a, checked in by Joel Sherrill <joel.sherrill@…>, on Aug 11, 1998 at 1:31:51 PM

New test from Emmanuel Raguet <raguet@…>.

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