libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_srcfd.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 #define MSG_SIZE 20
33 
34 #ifdef _WIN32
35 #include <Winsock2.h>
36 #include <Ws2tcpip.h>
37 #else
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netdb.h>
41 #endif
42 
43 int main (void)
44 {
45  int rc;
46 
48  // Create the infrastructure
49  void *ctx = zmq_ctx_new ();
50  assert (ctx);
51 
52  void *rep = zmq_socket (ctx, ZMQ_REP);
53  assert (rep);
54  void *req = zmq_socket (ctx, ZMQ_REQ);
55  assert (req);
56 
57  rc = zmq_bind(rep, "tcp://127.0.0.1:5560");
58  assert (rc == 0);
59 
60  rc = zmq_connect(req, "tcp://127.0.0.1:5560");
61  assert (rc == 0);
62 
63  char tmp[MSG_SIZE];
64  memset (tmp, 0, MSG_SIZE);
65  zmq_send(req, tmp, MSG_SIZE, 0);
66 
67  zmq_msg_t msg;
68  rc = zmq_msg_init(&msg);
69  assert (rc == 0);
70 
71  zmq_recvmsg(rep, &msg, 0);
72  assert(zmq_msg_size(&msg) == MSG_SIZE);
73 
74  // get the messages source file descriptor
75  int srcFd = zmq_msg_get(&msg, ZMQ_SRCFD);
76  assert(srcFd >= 0);
77 
78  rc = zmq_msg_close(&msg);
79  assert (rc == 0);
80 
81  // get the remote endpoint
82  struct sockaddr_storage ss;
83 #ifdef ZMQ_HAVE_HPUX
84  int addrlen = sizeof ss;
85 #else
86  socklen_t addrlen = sizeof ss;
87 #endif
88  rc = getpeername (srcFd, (struct sockaddr*) &ss, &addrlen);
89  assert (rc == 0);
90 
91  char host [NI_MAXHOST];
92  rc = getnameinfo ((struct sockaddr*) &ss, addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST);
93  assert (rc == 0);
94 
95  // assert it is localhost which connected
96  assert (strcmp(host, "127.0.0.1") == 0);
97 
98  rc = zmq_close (rep);
99  assert (rc == 0);
100  rc = zmq_close (req);
101  assert (rc == 0);
102 
103  // sleep a bit for the socket to be freed
105 
106  // getting name from closed socket will fail
107  rc = getpeername (srcFd, (struct sockaddr*) &ss, &addrlen);
108 #ifdef ZMQ_HAVE_WINDOWS
109  assert (rc == SOCKET_ERROR);
110  assert (WSAGetLastError() == WSAENOTSOCK);
111 #else
112  assert (rc == -1);
113  assert (errno == EBADF);
114 #endif
115 
116  rc = zmq_ctx_term (ctx);
117  assert (rc == 0);
118 
119  return 0 ;
120 }
121 
void msleep(int milliseconds)
Definition: testutil.hpp:316
#define MSG_SIZE
Definition: test_srcfd.cpp:32
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SRCFD
Definition: zmq.h:369
#define ZMQ_REP
Definition: zmq.h:250
ZMQ_EXPORT int zmq_recvmsg(void *s, zmq_msg_t *msg, int flags)
Definition: zmq.cpp:501
void setup_test_environment(void)
Definition: testutil.hpp:285
#define SETTLE_TIME
Definition: testutil.hpp:44
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
#define ZMQ_REQ
Definition: zmq.h:249
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
int main(void)
Definition: test_srcfd.cpp:43
Definition: zmq.h:221
ZMQ_EXPORT int zmq_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
ZMQ_EXPORT int zmq_msg_get(zmq_msg_t *msg, int property)
Definition: zmq.cpp:681
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
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
ZMQ_EXPORT size_t zmq_msg_size(zmq_msg_t *msg)
Definition: zmq.cpp:671
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613