[zeromq-dev] UDP message with zeromq 2.0beta
Guo, Yanchao
Yanchao.Guo at sac.com
Thu Jan 21 10:24:24 CET 2010
Tried this simple UDP multicast example in the same manner, and it is successful everytime on the same machine. I guess there might be some issue either with the code or with my compilation.
Can you please try this out? Thanks.
server.cpp:
// Compile : gcc -o server server.c
//
// This code has NOT been tested
//
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXBUFSIZE 65536 // Max UDP Packet size is 64 Kbyte
int main()
{
int sock, status, socklen;
char buffer[MAXBUFSIZE];
struct sockaddr_in saddr;
struct ip_mreq imreq;
// set content of struct saddr and imreq to zero
memset(&saddr, 0, sizeof(struct sockaddr_in));
memset(&imreq, 0, sizeof(struct ip_mreq));
// open a UDP socket
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if ( sock < 0 ){
perror("Error creating socket");
return 0;
}
saddr.sin_family = PF_INET;
saddr.sin_port = htons(4096); // listen on port 4096
saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind socket to any interface
status = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));
if ( status < 0 ){
perror("Error binding socket to interface");
return 0;
}
imreq.imr_multiaddr.s_addr = inet_addr("226.0.0.1");
imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface
// JOIN multicast group on default interface
status = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(const void *)&imreq, sizeof(struct ip_mreq));
socklen = sizeof(struct sockaddr_in);
// receive packet from socket
status = recvfrom(sock, buffer, MAXBUFSIZE, 0,
(struct sockaddr *)&saddr, (socklen_t*)&socklen);
// shutdown socket
shutdown(sock, 2);
// close socket
close(sock);
return 0;
}
client:
// Compile : gcc -o client client.c
//
// This code has NOT been tested
//
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXBUFSIZE 65536 // Max UDP Packet size is 64 Kbyte
int main()
{
int sock, status, socklen;
char buffer[MAXBUFSIZE];
struct sockaddr_in saddr;
struct in_addr iaddr;
unsigned char ttl = 3;
unsigned char one = 1;
// set content of struct saddr and imreq to zero
memset(&saddr, 0, sizeof(struct sockaddr_in));
memset(&iaddr, 0, sizeof(struct in_addr));
// open a UDP socket
sock = socket(PF_INET, SOCK_DGRAM, 0);
if ( sock < 0 ){
perror("Error creating socket") ;
return 0;
}
saddr.sin_family = PF_INET;
saddr.sin_port = htons(0); // Use the first free port
saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind socket to any interface
status = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));
if ( status < 0 ){
perror("Error binding socket to interface");
return 0;
}
iaddr.s_addr = INADDR_ANY; // use DEFAULT interface // Set the outgoing interface to DEFAULT
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &iaddr,
sizeof(struct in_addr));
// Set multicast packet TTL to 3; default TTL is 1
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
sizeof(unsigned char));
// send multicast traffic to myself too
status = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP,
&one, sizeof(unsigned char));
// set destination multicast address
saddr.sin_family = PF_INET;
saddr.sin_addr.s_addr = inet_addr("226.0.0.1");
saddr.sin_port = htons(4096);
// put some data in buffer
strcpy(buffer, "Hello world\n");
socklen = sizeof(struct sockaddr_in);
// receive packet from socket
status = sendto(sock, buffer, strlen(buffer), 0,
(struct sockaddr *)&saddr, socklen);
// shutdown socket
shutdown(sock, 2);
// close socket
close(sock);
return 0;
}
-----Original Message-----
From: Martin Sustrik [mailto:sustrik at 250bpm.com]
Sent: Thu 1/21/2010 3:08 AM
To: Guo, Yanchao
Cc: zeromq-dev at lists.zeromq.org
Subject: Re: [zeromq-dev] UDP message with zeromq 2.0beta
Guo,
> Hi Martin, I am trying the simple test program for udp, and I setup a
> simple pair of server/client on my machine. Out of 10 messages, I only
> get like 4 or 5 messages successfully received. I understand that UDP
> doesn't guarantee that all message to be delivered , but I think the
> success rate is just too low. Did you see the same for your testing?
There's no real storage for the messages on the network switch or
elsewhere, so you won't get historical messages. For example: Send a
message, close the sender application. Open the receiver application,
ask for message => you'll get nothing. You have to have both
applications running to guarantee lossless semantics.
Martin
DISCLAIMER: This e-mail message and any attachments are intended solely for the use of the individual or entity to which it is addressed and may contain information that is confidential or legally privileged. If you are not the intended recipient, you are hereby notified that any dissemination, distribution, copying or other use of this message or its attachments is strictly prohibited. If you have received this message in error, please notify the sender immediately and permanently delete this message and any attachments.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20100121/2bfb7367/attachment.htm>
More information about the zeromq-dev
mailing list