LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-13-2012, 03:58 AM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
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;
 }

Last edited by batman4; 08-13-2012 at 04:32 AM. Reason: zmq
 
Old 08-13-2012, 06:02 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 623

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
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.

Last edited by firstfire; 08-13-2012 at 06:11 AM.
 
1 members found this post helpful.
Old 08-13-2012, 07:44 AM   #3
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
now my problem is with zmq_send
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Client Server program in C fahad.anwar Programming 1 07-24-2012 04:07 AM
[SOLVED] zencafe - server to client pc, all working except client pc getting internt access captain_sensible Linux - Newbie 3 06-08-2010 10:37 AM
Client & server program ZuzooVn Programming 5 12-09-2009 05:39 AM
server client program swift2008 Programming 10 11-26-2008 12:12 PM
pls how do i restrict client from accessing file or folder on the linux server braeasy1 Linux - Security 5 10-23-2008 06:47 AM


All times are GMT -5. The time now is 03:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration