artdaq  v3_12_02
TCP_listen_fd.cc
1 // This file (TCP_listen_fd.cpp) was created by Ron Rechenmacher <ron@fnal.gov> on
2 // Apr 20, 2010. "TERMS AND CONDITIONS" governing this file are in the README
3 // or COPYING file. If you do not have such a file, one can be obtained by
4 // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
5 // $RCSfile: TCP_listen_fd.cpp,v $
6 // rev="$Revision: 1.3 $$Date: 2010/06/04 14:00:32 $";
7 
9 
10 #include "TRACE/tracemf.h"
11 #include "artdaq/DAQdata/Globals.hh"
12 
13 #define TRACE_NAME (app_name + "_TCP_listen_fd").c_str()
14 
15 #include <arpa/inet.h> /* inet_aton */
16 #include <netdb.h> /* gethostbyname */
17 #include <netinet/in.h> /* inet_aton, struct sockaddr_in */
18 #include <strings.h> // bzero
19 #include <sys/socket.h> /* inet_aton, socket, bind, listen, accept */
20 #include <cerrno> // errno
21 #include <cstdio> // printf
22 #include <cstdlib> // exit
23 
24 int TCP_listen_fd(int port, int rcvbuf)
25 {
26  int sts;
27  int listener_fd;
28  struct sockaddr_in sin;
29 
30  listener_fd = socket(PF_INET, SOCK_STREAM, 0); /* man TCP(7P) */
31  if (listener_fd == -1)
32  {
33  TLOG(TLVL_ERROR) << "Could not open listen socket! Exiting with code 1!";
34  perror("socket error");
35  exit(1);
36  }
37 
38  int opt = 1; // SO_REUSEADDR - man socket(7)
39  sts = setsockopt(listener_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
40  if (sts == -1)
41  {
42  TLOG(TLVL_ERROR) << "Could not set SO_REUSEADDR! Exiting with code 2!";
43  perror("setsockopt SO_REUSEADDR");
44  return (2);
45  }
46 
47  memset(static_cast<void *>(&sin), 0, sizeof(sin));
48  sin.sin_family = AF_INET;
49  sin.sin_port = htons(port);
50  sin.sin_addr.s_addr = INADDR_ANY;
51 
52  // printf( "bind..." );fflush(stdout);
53  sts = bind(listener_fd, reinterpret_cast<struct sockaddr *>(&sin), sizeof(sin)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
54  if (sts == -1)
55  {
56  TLOG(TLVL_ERROR) << "Could not bind socket for port " << port << "! Exiting with code 3!";
57  perror("bind error");
58  exit(3);
59  }
60  // printf( " OK\n" );
61 
62  int len = 0;
63  socklen_t arglen = sizeof(len);
64  sts = getsockopt(listener_fd, SOL_SOCKET, SO_RCVBUF, &len, &arglen);
65  TLOG(TLVL_INFO) << "RCVBUF initial: " << len << " sts/errno=" << sts << "/" << errno << " arglen=" << arglen << " rcvbuf=" << rcvbuf << " listener_fd=" << listener_fd;
66  if (rcvbuf > 0)
67  {
68  len = rcvbuf;
69  sts = setsockopt(listener_fd, SOL_SOCKET, SO_RCVBUF, &len, arglen);
70  if (sts == -1)
71  {
72  TLOG(TLVL_ERROR) << "Error with setsockopt SNDBUF " << errno;
73  }
74  len = 0;
75  sts = getsockopt(listener_fd, SOL_SOCKET, SO_RCVBUF, &len, &arglen);
76  if (len < (rcvbuf * 2))
77  {
78  TLOG(TLVL_WARNING) << "RCVBUF " << len << " not expected (" << rcvbuf << " sts/errno=" << sts << "/" << errno;
79  }
80  else
81  {
82  TLOG(TLVL_DEBUG + 32) << "RCVBUF " << len << " sts/errno=" << sts << "/" << errno;
83  }
84  }
85 
86  // printf( "listen..." );fflush(stdout);
87  sts = listen(listener_fd, 5 /*QLEN*/);
88  if (sts == -1)
89  {
90  TLOG(TLVL_ERROR) << "Error calling listen! errno=" << errno << " (" << strerror(errno) << ")";
91  perror("listen error");
92  exit(1);
93  }
94  // printf( " OK\n" );
95 
96  return (listener_fd);
97 } // TCP_listen_fd
int TCP_listen_fd(int port, int rcvbuf)
Create a TCP listening socket on the given port and INADDR_ANY, with the given receive buffer...