MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
socket2stream.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/utils/socket2stream.cc,v 1.26 2017/01/12 15:10:28 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include <cstring>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netdb.h>
39 #include <cstdio>
40 #include <cstdlib>
41 #include <cerrno>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <sys/un.h>
45 #include <arpa/inet.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <signal.h>
49 
50 #include <iostream>
51 #include <sstream>
52 #include <vector>
53 
54 #include "sock.h"
55 #include "s2s.h"
56 
57 int
58 main(int argc, char *argv[])
59 {
60  s2s_t s2s;
61 
62  try {
63  s2s.parse(argc, argv);
64  s2s.prepare();
65 
66  } catch (...) {
67  s2s.shutdown();
68  exit(EXIT_FAILURE);
69  }
70 
71  if (s2s.nChannels == 0) {
72  s2s.nChannels = 1;
73  }
74 
75  s2s.dbuf.resize(s2s.nChannels);
76  const char *sep = " ";
77  while (true) {
78  int len = s2s.recv(0);
79 
80  switch (len) {
81  case -1: {
82  int save_errno = errno;
83 
84  if (save_errno == EAGAIN && !s2s.is_blocking()) {
85  continue;
86  }
87 
88  const char *err_msg = strerror(save_errno);
89  silent_cerr("recv(" << s2s.sock << ",\"" << s2s.buf << "\") "
90  "failed (" << save_errno << ": " << err_msg << ")"
91  << std::endl);
92 
93  // fallthru
94  }
95 
96  case 0:
97  goto done;
98 
99  default:
100  break;
101  }
102 
103  for (int i = 0; i < s2s.nChannels - 1; i++) {
104  std::cout << s2s.dbuf[i] << sep;
105  }
106  std::cout << s2s.dbuf[s2s.nChannels - 1] << std::endl;
107  }
108 
109 done:;
110  s2s.shutdown();
111  exit(EXIT_SUCCESS);
112 }
113 
std::string buf
Definition: s2s.h:68
void prepare(void)
ssize_t recv(int flags)
int nChannels
Definition: s2s.h:56
bool is_blocking(void) const
std::vector< double > dbuf
Definition: s2s.h:69
int sock
Definition: s2s.h:66
static unsigned done
Definition: gust.cc:209
Definition: s2s.h:54
void parse(int argc, char *argv[])
int main(int argc, char *argv[])
void shutdown(void)