Hi,
to test my multicast patch I tried the following piece of code, simplier
than the ptp soft, to validate the modifications.
The problem is that the packets are correctly sent and seemed to be
received by the rtnet udp layer at the other side, but never get to the
socket layer receiving ...
Other thing maybe in touch when ^c the little program rtdm complains:
"closing device in real-time mode while creation ran in non real time -
this is not supported! " maybe related to my problem.
something i did not understand about the xenomai/rtnet posix wrap?
Bruno.
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <rtnet.h>
char *dest_ip_s = "224.0.1.117";
char *local_ip_s = "10.0.0.1";
#define RCV_PORT 35999
#define XMT_PORT 36000
#define BUFFER_LENGTH 20
struct in_addr netAddr, localAddr;
struct sockaddr_in addr;
struct ip_mreq imr;
pthread_mutex_t sock_lock;
int action, sock, loop = 1, seq = 0;
char buf[BUFFER_LENGTH];
int fromlen = sizeof(struct sockaddr_in);
void shutalldown(int sig)
{
printf("shutting down\n");
pthread_mutex_lock(&sock_lock);
imr.imr_multiaddr.s_addr = netAddr.s_addr;
imr.imr_interface.s_addr = localAddr.s_addr;
setsockopt(sock, IPPROTO_IP, RT_IP_DROP_MEMBERSHIP, &imr, sizeof(struct ip_mreq));
if(sock>0) close(sock);
loop = 0;
pthread_mutex_unlock(&sock_lock);
}
int main(int argc, char *argv[])
{
signal(SIGTERM, shutalldown);
signal(SIGINT, shutalldown);
signal(SIGHUP, shutalldown);
pthread_mutex_init(&sock_lock, NULL);
while (1) {
switch (getopt(argc, argv, "a:i")) {
case 'i':
local_ip_s = optarg;
break;
case 'a':
action = atoi(optarg);
break;
case -1:
goto end_of_opt;
default:
printf("usage: %s [-a <action(0|1)>] [-i <local_ip>] ",
argv[0]);
return 0;
}
}
end_of_opt:
printf("Local IP: %s\n", local_ip_s);
printf("Dest IP: %s\n", dest_ip_s);
if(!inet_aton(local_ip_s, &localAddr))
{
printf("failed to encode local address: %s\n",local_ip_s );
return 0;
}
if(!inet_aton(dest_ip_s, &netAddr))
{
printf("failed to encode multi-cast address: %s\n", dest_ip_s);
return 0;
}
/* open sockets */
if( (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ) < 0)
{
printf("failed to initalize socket\n");
return 0;
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = localAddr.s_addr;
addr.sin_port = htons(action?RCV_PORT:XMT_PORT);
if(bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) < 0)
{
printf("failed to bind rcv socket\n");
return 0;
}
/* multicast send only on specified interface */
imr.imr_multiaddr.s_addr = netAddr.s_addr;
imr.imr_interface.s_addr = localAddr.s_addr;
/* join multicast group (for receiving) on specified interface */
if( setsockopt(sock, IPPROTO_IP, RT_IP_ADD_MEMBERSHIP, &imr, sizeof(struct ip_mreq)) < 0 )
{
printf("failed to join the multi-cast group\n");
return 0;
}
switch (action) {
case 0:
printf("Sending data to %s / %d ...\n", dest_ip_s, RCV_PORT);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = netAddr.s_addr;
addr.sin_port = htons(RCV_PORT);
while(loop)
{
snprintf(buf, BUFFER_LENGTH-1, "Salut %2d !", seq++);
if(seq > 20) seq = 0;
pthread_mutex_lock(&sock_lock);
if( sendto(sock, buf, BUFFER_LENGTH-1, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0 )
printf("error sending multicast\n");
pthread_mutex_unlock(&sock_lock);
usleep(500000);
}
break;
case 1:
printf("Receiving data from %s on %d\n", dest_ip_s, RCV_PORT);
while(loop)
{
if( recvfrom(sock, buf, BUFFER_LENGTH-1, 0, (struct sockaddr *) &addr, &fromlen) < 0)
printf("error receiving multicast\n");
else {
printf("Msg from %s / %d: %s\n", inet_ntoa(addr.sin_addr),ntohs(addr.sin_port), buf);
}
}
break;
default:
break;
}
return 0;
}
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
RTnet-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rtnet-developers