LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   zmq client server program ..my client is working but server is not..pls help (http://www.linuxquestions.org/questions/programming-9/zmq-client-server-program-my-client-is-working-but-server-is-not-pls-help-4175421817/)

batman4 08-13-2012 04:58 AM

zmq client server program ..my client is working but server is not..pls help
 
server program
this program is not working
its not rining the desired results
pls help
Code:

#include<zmq.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>

int main(){
          void *context=zmq_init(1);
        printf("-------------------");
        void *responder=zmq_socket(context,ZMQ_REP);
        zmq_bind(responder ,"tcp://localhost:5555");
        while(1){
                printf("recieving");
                zmq_msg_t request;
                zmq_msg_init (&request);
                zmq_recv(responder,&request,0);
                printf("recievd :%s\n ",(char *)zmq_msg_data (&request));
                zmq_msg_close(&request);
        }
        zmq_term(context);
        return 0;

}

client
Code:


#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<zmq.h>

int main(){
        void *context=zmq_init(1);
        printf("connecting to hello server\n");
        void *requester=zmq_socket(context,ZMQ_REQ);
        zmq_connect(requester,"tcp://localhost:5555");
        int req;
        for(req=0;req!=10;req++){
                zmq_msg_t request;
                zmq_msg_init_data(&request,"Hello",6,NULL,NULL);
                printf("sending data %d\n" ,req);
                zmq_msg_close(&request);
        }
        zmq_close (requester);
        zmq_term(close);
        return 0;
 }


firstfire 08-13-2012 07:02 AM

Hi.

I managed to make it work:

Server:
Code:

#include<zmq.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include <assert.h>

int main(){
          void *context=zmq_init(1);
        printf("-------------------\n");
        void *responder=zmq_socket(context,ZMQ_REP);
        assert( responder );
        int rc = zmq_bind(responder ,"tcp://lo:5555");
        assert( rc == 0 );
       
        zmq_msg_t ok_msg;
        zmq_msg_init_data(&ok_msg, "OK", 2, NULL, NULL);

        while(1){
                printf("recieving\n");
                zmq_msg_t request;
                rc = zmq_msg_init (&request);
                assert(rc == 0);
                rc = zmq_recv(responder,&request, 0);
                assert(rc == 0);
                printf("recievd :%s\n",(char *)zmq_msg_data (&request));
                zmq_msg_close(&request);
                rc = zmq_send(responder,&ok_msg, 0);
                assert(rc == 0);
        }
        zmq_term(context);
        return 0;

}

Client:
Code:

#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<zmq.h>
#include <assert.h>

int main(){
        void *context=zmq_init(1);
        printf("connecting to hello server\n");
        void *requester=zmq_socket(context,ZMQ_REQ);
        int rc = zmq_connect(requester,"tcp://localhost:5555");
        assert( rc == 0 );
        int req;
        for(req=0;req<10;req++){
                zmq_msg_t request;
                rc = zmq_msg_init_data(&request,"Hello",6,NULL,NULL);
                assert( rc == 0 );
                printf("sending data %d\n" ,req);
                rc = zmq_send(requester, &request, 0);
                assert( rc == 0);
                printf("waiting for reply\n");
                rc = zmq_recv(requester, &request, 0);
                assert( rc == 0);
                printf("reply:%s\n",(char *)zmq_msg_data (&request));
                zmq_msg_close(&request);
        }
        zmq_close (requester);
        zmq_term(close);
        return 0;
 }

First of all, it is a good idea to check return status of important calls, at least using assert(), as example code in man pages does.

zmq_bind() with tcp transport requires that endpoint be an interface name followed by a colon and a port number. On debian-based systems localhost corresponds to the lo interface. Or you can use IP address 127.0.0.1 instead. On the other hand zmq_bind() needs peer address, so you can use localhost or 127.0.0.1. See zmq_tcp(7) for details.

Now, the other problem is with the message flow. You use ZMQ_REQ (client) and ZMQ_REP (server) socket types. As described in zmq_socket(3) these types of sockets require specific request-reply pattern to be implemented by the program. Namely, client should send a message and read the reply to it, otherwise the socket will be in inappropriate state when you'll try to send another message (zmq_send() return -1 with errno == EFSM). Of course the server must reply something when he receives a message.

Here is the relevant quote from zmq_socket(3):
Quote:

ZMQ_REQ
A socket of type ZMQ_REQ is used by a client to send requests to and receive replies from a service. This socket type allows only an alternating sequence of zmq_send(request) and subsequent zmq_recv(reply) calls. Each request sent is round-robined among all services, and each reply received is matched with the last issued request.

....

ZMQ_REP
A socket of type ZMQ_REP is used by a service to receive requests from and send replies to a client. This socket type allows only an alternating sequence of zmq_recv(request) and subsequent zmq_send(reply) calls. Each request received is fair-queued from among all clients, and each reply sent is routed to the client that issued the last request. If the original requester doesn’t exist any more the reply is silently discarded.

batman4 08-13-2012 08:44 AM

now my problem is with zmq_send


All times are GMT -5. The time now is 09:07 PM.