SoDaRadio-5.0.3-master:8901fb5
LineSockets_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 "LineSocket.hxx"
30 #include <error.h>
31 #include <stdio.h>
32 
33 void usage()
34 {
35  fprintf(stderr, "Sockets_Test is portnum\n");
36  fprintf(stderr, "Sockets_Test ic portnum hostname\n");
37  exit(-1);
38 }
39 
40 int main(int argc, char * argv[])
41 {
42  int portnum;
43 
44  if(argc < 3) usage();
45 
46 
48  if(strncmp(argv[1], "is", 2) == 0) {
49  // server.
50  portnum = atoi(argv[2]);
51  s = new SoDa::IP::LineServerSocket(portnum);
52  int con_count = 0;
53  while(con_count < 20) {
54  while(!s->isReady()) {
55  usleep(10000);
56  }
57  con_count++;
58  char buf[1024];
59  int rsize;
60  int retcount = 0;
61  while((rsize = s->getLine(buf, 1024)) >= 0) {
62  retcount++;
63  if(rsize > 0) {
64  std::cout << boost::format("Got line [%s] RC = %d\n") % buf % retcount;
65  retcount = 0;
66  }
67  else {
68  usleep(1000000);
69  }
70  }
71  std::cout << "Disconnect." << std::endl;
72  }
73  }
74  else if (strncmp(argv[1], "ic", 2) == 0) {
75  std::cout << "No idea what to do here." << std::endl;
76  }
77 
78 }
int getLine(char *buf, unsigned int maxsize)
capture a newline-terminated buffer from the socket This is a non-blocking method.
Definition: LineSocket.hxx:84
void usage()
int main(int argc, char *argv[])
This is an IP socket that returns newline-delimited strings arriving on the socket.
Definition: LineSocket.hxx:48