SoDaRadio-5.0.3-master:8901fb5
Sockets_Test.cxx
Go to the documentation of this file.
1 /*
2 Copyright (c) 2013, Matthew H. Reilly (kb1vc)
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #include "IPSockets.hxx"
30 #include "UDSockets.hxx"
31 #include <error.h>
32 #include <stdio.h>
33 
34 void usage()
35 {
36  fprintf(stderr, "Sockets_Test is portnum\n");
37  fprintf(stderr, "Sockets_Test ic portnum hostname\n");
38  fprintf(stderr, "Sockets_Test us pathname\n");
39  fprintf(stderr, "Sockets_Test uc pathname\n");
40  exit(-1);
41 }
42 
43 int main(int argc, char * argv[])
44 {
45  int portnum;
46 
47  if(argc < 3) usage();
48 
49  int empty_count = 0;
50  int iter_count = 0;
51  int found_count = 0;
52 
57  if(strncmp(argv[1], "is", 2) == 0) {
58  // server.
59  portnum = atoi(argv[2]);
60  s = new SoDa::IP::ServerSocket(portnum);
61  int con_count = 0;
62  while(con_count < 20) {
63  while(!s->isReady()) {
64  usleep(1000);
65  }
66  con_count++;
67  char buf[1024];
68  int rsize;
69  while((rsize = s->get(buf, 1024)) >= 0) {
70  iter_count++;
71  if(rsize == 0) {
72  empty_count++;
73  usleep(10000);
74  }
75  if(rsize > 0) {
76  found_count++;
77  const char * okmsg = "ok!";
78  std::cout << "message: [" << buf << "] length = " << rsize << std::endl;
79  std::cout << "iter_count = " << iter_count
80  << " empty_count = " << empty_count
81  << " found_count = " << found_count
82  << " con_count = " << con_count
83  << std::endl;
84  s->put(okmsg, 4);
85  std::cout << "Hmmmm>" << std::endl;
86  }
87  if(rsize < 0) {
88  perror("Hmmm... get returned a negative result.");
89  }
90  }
91  }
92  std::cout << "FINAL iter_count = " << iter_count
93  << " empty_count = " << empty_count
94  << " found_count = " << found_count
95  << std::endl;
96 
97  }
98  else if (strncmp(argv[1], "ic", 2) == 0) {
99  portnum = atoi(argv[2]);
100  c = new SoDa::IP::ClientSocket(argv[3], portnum);
101  const char * msg1 = "SET RX_IF_FREQ D 110e3\n";
102  const char * msg2 = "SET STOP I 2\n";
103  char buf[1024];
104  c->put(msg1, strlen(msg1)+1);
105  c->put(msg2, strlen(msg2)+1);
106  int rs;
107  while((rs = c->get(buf, 1024)) <= 0) {
108  std::cout << "waiting" << std::endl ;
109  usleep(1000000);
110  }
111 
112  std::cout << "message: [" << buf << "] length = " << rs << std::endl;
113  // rs = c->get(buf, 1024);
114  std::cout << "message: [" << buf << "] length = " << rs << std::endl;
115  delete c;
116  }
117  else if(strncmp(argv[1], "us", 2) == 0) {
118  // unix domain server.
119  us = new SoDa::UD::ServerSocket(argv[2]);
120  int con_count = 0;
121  while(con_count < 20) {
122  us->put("Hey there sailor\n\n\n", 17);
123  while(!us->isReady()) {
124  usleep(1000);
125  }
126  std::cerr << "About to get buffer\n";
127  con_count++;
128  char buf[1024];
129  int rsize;
130  while((rsize = us->get(buf, 1024)) >= 0) {
131  iter_count++;
132  if(rsize == 0) {
133  if(!us->isReady()) {
134  std::cerr << "socket no longer ready\n";
135  break;
136  }
137  empty_count++;
138  usleep(10000);
139  if((empty_count % 256) == 0) std::cerr << "Empty buffer\n";
140  }
141  else if(rsize > 0) {
142  found_count++;
143  const char * okmsg = "ok!";
144  std::cout << "message: [" << buf << "] length = " << rsize << std::endl;
145  std::cout << "iter_count = " << iter_count
146  << " empty_count = " << empty_count
147  << " found_count = " << found_count
148  << " con_count = " << con_count
149  << std::endl;
150  us->put(okmsg, 4);
151  std::cout << "Hmmmm>" << std::endl;
152  }
153  else if(rsize < 0) {
154  perror("Hmmm... get returned a negative result.");
155  }
156  }
157  }
158  std::cout << "FINAL iter_count = " << iter_count
159  << " empty_count = " << empty_count
160  << " found_count = " << found_count
161  << std::endl;
162 
163  }
164  else if (strncmp(argv[1], "uc", 2) == 0) {
165  uc = new SoDa::UD::ClientSocket(argv[2]);
166  const char * msg1 = "SET RX_IF_FREQ D 110e3\n";
167  const char * msg2 = "SET STOP I 2\n";
168  char buf[1024];
169  uc->put(msg1, strlen(msg1)+1);
170  uc->put(msg2, strlen(msg2)+1);
171  int rs;
172  while((rs = uc->get(buf, 1024)) <= 0) {
173  std::cout << "waiting" << std::endl ;
174  usleep(1000000);
175  }
176 
177  std::cout << "message: [" << buf << "] length = " << rs << std::endl;
178  // rs = c->get(buf, 1024);
179  std::cout << "message: [" << buf << "] length = " << rs << std::endl;
180  delete uc;
181  }
182 
183 }
int get(void *ptr, unsigned int size)
Definition: UDSockets.hxx:75
int put(const void *ptr, unsigned int size)
Definition: IPSockets.hxx:113
void usage()
int put(const void *ptr, unsigned int size)
Definition: IPSockets.cxx:164
int main(int argc, char *argv[])
int put(const void *ptr, unsigned int size)
Definition: UDSockets.cxx:162
int get(void *ptr, unsigned int size)
Definition: IPSockets.hxx:108
int get(void *ptr, unsigned int size)
Definition: UDSockets.cxx:175
int get(void *ptr, unsigned int size)
Definition: IPSockets.cxx:177
int put(const void *ptr, unsigned int size)
Definition: UDSockets.hxx:80