libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_system.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "testutil.hpp"
31 
32 #if defined (ZMQ_HAVE_WINDOWS)
33 # include <winsock2.h>
34 # include <stdexcept>
35 #else
36 # include <sys/socket.h>
37 # include <netinet/in.h>
38 # include <unistd.h>
39 #endif
40 
41 #if defined (ZMQ_HAVE_WINDOWS)
42 
43 void initialise_network (void)
44 {
45  WSADATA info;
46  if (WSAStartup(MAKEWORD(2,0), &info) != 0)
47  throw std::runtime_error("Could not start WSA");
48 }
49 
50 int close (int fd)
51 {
52  return closesocket (fd);
53 }
54 
55 #else
56 
57 void initialise_network (void)
58 {
59 }
60 
61 #endif
62 
63 // This test case stresses the system to shake out known configuration
64 // problems. We're direct system calls when necessary. Some code may
65 // need wrapping to be properly portable.
66 
67 int main (void)
68 {
70 
71  // Check that we have local networking via ZeroMQ
72  void *ctx = zmq_ctx_new ();
73  assert (ctx);
74  void *dealer = zmq_socket (ctx, ZMQ_DEALER);
75  if (zmq_bind (dealer, "tcp://127.0.0.1:5670") == -1) {
76  printf ("E: Cannot find 127.0.0.1 -- your system does not have local\n");
77  printf ("E: networking. Please fix this before running libzmq checks.\n");
78  return -1;
79  }
80  // Check that we can create 1,000 sockets
81  int handle [1000];
82  int count;
83  for (count = 0; count < 1000; count++) {
84  handle [count] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
85  if (handle [count] == -1) {
86  printf ("W: Only able to create %d sockets on this box\n", count);
87  printf ("I: Tune your system to increase maximum allowed file handles\n");
88 #if defined (ZMQ_HAVE_OSX)
89  printf ("I: On OS/X, run 'ulimit -n 1200' in bash");
90 #elif defined (ZMQ_HAVE_LINUX)
91  printf ("I: On Linux, run 'ulimit -n 1200' in bash");
92 #endif
93  return -1;
94  }
95  }
96  // Release the socket handles
97  for (count = 0; count < 1000; count++) {
98  close(handle[count]);
99  }
100 
101  zmq_close(dealer);
102  zmq_ctx_term(ctx);
103 }
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_DEALER
Definition: zmq.h:251
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
void initialise_network(void)
Definition: test_system.cpp:57
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
int main(void)
Definition: test_system.cpp:67