summaryrefslogtreecommitdiff
path: root/socket/pgmHost/clientTcp.c
blob: 1771aa868f806be535e509249b0a350199b4377e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * clientTcp.c : generate an executable to test the TCP Sockets.
 * the RTEMS target must be configured as server.
 * Don't forget to change the IP address.
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/file.h>
 
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>

#include "../buffer.h"

#define DEFAULT_IP "194.2.81.61" 
#define DEFAULT_LISTEN_IP "194.2.81.126"

int main(int argc, char** argv )
{
  int   sd;
  struct   sockaddr_in server;
  struct  hostent *hp;
  struct sockaddr_in from;
  int fromlen;
  int i,retval;
  char	*tabChar;
  char *server_name= "localhost";
  unsigned short port;
  unsigned long	lenChar;
  struct timespec timeToWait, timeRem;

  printf("main begin...\n");
  port = DEFAULT_PORT_SERVER;
  server_name = DEFAULT_IP;

  bzero(&server, sizeof(server));

  sd = socket (AF_INET,SOCK_STREAM,0);
  printf("socket() result:<%d>\n", sd);

  if (sd<0) {
    printf("opening stream socket\n");
    exit(-1);
  }
  	
  if (inet_aton (server_name, (struct in_addr*)&(server.sin_addr.s_addr)) == 0) {
    printf("%s : IP address: bad format\n",
	   argv[0]);
  } 

  server.sin_family = AF_INET;
  server.sin_port = htons(port);

  /* Connect to TCP/SERVER */
  if ( (retval = connect(sd, &server, sizeof(server))) < 0 ) {
    close(sd);
    perror("connecting stream socket");
    exit(0);
  }
  printf("connect() result:<%d>\n", retval);
  fromlen = sizeof(from);
  if ( (retval = getpeername(sd,&from,&fromlen)) <0){
    perror("could't get peername\n");
    exit(1);
  }
  printf("getpeername() result:<%d>\n", retval);
  printf("Connected to TCP/Server:");
  printf("%s:%d\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port));

  if ((hp = gethostbyaddr((const char *)(&from.sin_addr.s_addr),
			  sizeof(from.sin_addr.s_addr),AF_INET)) == NULL)
    fprintf(stderr, "Can't find host %s\n", inet_ntoa(from.sin_addr));
  else
    printf("(Name is : %s)\n", hp->h_name);

  printf("Now, let's send packets...");

  while(1) {
	
    tabChar = BuildBuffer();
    lenChar = *((unsigned long *)(tabChar));
    
    i = send(sd, tabChar, lenChar, 0);
    if (i == -1) 
      {
	fprintf(stderr,"send() failed (at least locally detected errors)\n");
	return -1;
      }
    timeToWait.tv_sec = 1;
    timeToWait.tv_nsec = 0;
    nanosleep(&timeToWait, &timeRem);
          
    FreeBuffer(tabChar);
   
  }
  
  close(sd);
  exit (0);
}