Hi,

I am trying to use a context with ZMQ_MAX_SOCKETS to restrict the possible 
number of connections. In my example, I set ZMQ_MAX_SOCKETS to 2 and start a 
server with ZMQ_STREAM sockets. The client uses posix sockets to connect to the 
server. From what I see, it seems that the number of sockets is ignored because 
I can connect more than 2 clients. Am I missing something?

The server code:
#include <zmq.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    void* ctx = zmq_ctx_new();
    int rc = zmq_ctx_set( ctx, ZMQ_MAX_SOCKETS, 2 );
    assert( rc == 0 );

    int major = 0;
    int minor = 0;
    int patch = 0;

    zmq_version(&major, &minor, &patch);
    printf("This is zeroMQ %d.%d.%d\n", major, minor, patch);

    void* s = zmq_socket(ctx, ZMQ_STREAM);
    assert(s);
    rc = zmq_bind(s, "tcp://*:10000");
    assert(rc == 0);

    uint8_t id[256];

    while(1)
    {
        printf("Waiting for client...\n");
        memset(id, 256, '\0');
        int n = zmq_recv(s, id, 256, 0);

        printf("Client connected\n");
        for(int i=0; i != n; ++i)
        {
            printf("%0x", (int)id[i]);
        }
        printf (" (%d)\n", n);

        n = zmq_recv(s, id, 256, 0);
        assert(n == 0);
    }
}

The client code:
int main()
{
    struct sockaddr_in serv_addr;
    int sockfd;
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Error : Could not create socket \n");
    }

    memset(&serv_addr, '0', sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(10000);

    if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
    {
        printf("\n inet_pton error occured\n");
    }

    if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
       printf("\n Error : Connect Failed \n");
    }

    int i; std::cin >> i;

    return 0;
}

Running the server, and stating four clients gives the following output:
This is zeroMQ 4.1.2
Waiting for client...
Client connected
06b8b4567 (5)
Waiting for client...
Client connected
06b8b4568 (5)
Waiting for client...
Client connected
06b8b4569 (5)
Waiting for client...
Client connected
06b8b456a (5)
Waiting for client...

Since I don't see any disconnect messages for the clients, it looks like all 
four clients are connected. netstat confirms this.

Best wishes,
  Jens

--
Jens Auer | CGI | Software-Engineer
CGI (Germany) GmbH & Co. KG
Rheinstraße 95 | 64295 Darmstadt | Germany
T: +49 6151 36860 154
[email protected]<mailto:[email protected]>
Unsere Pflichtangaben gemäß § 35a GmbHG / §§ 161, 125a HGB finden Sie unter 
de.cgi.com/pflichtangaben<http://de.cgi.com/pflichtangaben>.

CONFIDENTIALITY NOTICE: Proprietary/Confidential information belonging to CGI 
Group Inc. and its affiliates may be contained in this message. If you are not 
a recipient indicated or intended in this message (or responsible for delivery 
of this message to such person), or you think for any reason that this message 
may have been addressed to you in error, you may not use or copy or deliver 
this message to anyone else. In such case, you should destroy this message and 
are asked to notify the sender by reply e-mail.
_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to