Dear,

Thanks a lot Mr Kasper. From your code, which sections are needed to be changed 
if I use Xenomai, not RTAI? Because I am using xenomai, not RTAI.

I am sorry if this answer is too be beginner, I am very new with this. Thank 
you so much.

--- On Tue, 2/22/11, Andreas Kasper <a.kas...@kabsi.at> wrote:

From: Andreas Kasper <a.kas...@kabsi.at>
Subject: Re: [RTnet-users] [ask] how to send and receive packet with real time 
network in rtnet
To: "Ramon Rusli" <mon_...@yahoo.com>
Cc: "rtnet" <rtnet-users@lists.sourceforge.net>
Date: Tuesday, February 22, 2011, 7:28 AM

Hello,
  I think you are looking for the following code:
  

  

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>

#include <rtai_lxrt.h>
#include <rtnet.h>

#define LXRTTASKID 4444

static struct sockaddr_in net_client;
static struct sockaddr_in net_canrouter;

int main( int argc, char **argv )
{
    RT_TASK *lxrttask;
    int sock;

    RTIME start;
    RTIME end;

    char welcomemessage[] = "Welcome";

    /* init network strutures */
    memset( &net_client, 0, sizeof( struct sockaddr_in ) );
    memset( &net_canrouter, 0, sizeof( struct sockaddr_in ) );


    /* disable paging for the current process */
    if ( -1 == mlockall( MCL_CURRENT | MCL_FUTURE ) )
    {
        printf("Error: mlockall failed\n");

        exit( EXIT_FAILURE );
    }

    /* local network interface configuration */
    net_client.sin_family = AF_INET;
    net_client.sin_addr.s_addr = INADDR_ANY;
    net_client.sin_port = htons( 333 ); /* local udp port */

    /* udp destinaton configuration */
    net_canrouter.sin_family = AF_INET;
    net_canrouter.sin_addr.s_addr = inet_addr( "192.168.22.1" );
    //printf("INET_ATON: %d\n", inet_aton( "192.168.22.1", 
&net_canrouter.sin_addr) );
    net_canrouter.sin_port= htons( 333 ); /* remode udp port */

    /* create udp socket */
    if ( ( sock = rt_dev_socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 )
    {
        printf("Error: rt_dev_socket\n");

        exit( EXIT_FAILURE );
    }

    /* init realtime task */
    if ( ( lxrttask = rt_task_init( LXRTTASKID, 1, 0, 0 ) ) == NULL )
    {
        rt_dev_close( sock );
 
        printf("Error: rt_task_init\n");

        exit( EXIT_FAILURE );
    }

    printf("switching to hard real time mode\n");


    /* switch to hard real time mode */
    rt_make_hard_real_time( );


    /* bind local rt socket */
    if ( 0 != rt_dev_bind( sock, (struct sockaddr *) &net_client, sizeof( 
struct sockaddr_in ) ) )
    {
        rt_make_soft_real_time( );

        rt_dev_close( sock );

        rt_task_delete( lxrttask );

        printf("Error: rt_dev_bind\n");

        exit( EXIT_FAILURE );
    }

    /* connect can router */
    if ( 0 != rt_dev_connect( sock, (struct sockaddr *) &net_canrouter, sizeof( 
struct sockaddr_in ) ) )
    {
        rt_make_soft_real_time( );

        rt_dev_close( sock );

        rt_task_delete( lxrttask );

        printf("Error: rt_dev_connect\n");

        exit( EXIT_FAILURE );
    }

    /* rt & network initialization completed */

    printf("send\n");


    /* send welcome message to the can router */
    rt_dev_send( sock, welcomemessage, sizeof( welcomemessage ), 0 );


    printf("=> soft realtime\n");

    rt_make_soft_real_time( );

    printf("=> close\n");

    rt_dev_close( sock );

    printf("=> task_delete\n");

    rt_task_delete( lxrttask );

    printf("=> exit\n");

    printf("start: %g\nende: %g\n", start, end );

    exit( EXIT_SUCCESS );
}


  

  You have to use the following libraries:
  -llxrt -lpthread
  

  and the following include pathes:
  -I/usr/realtime/include -I/usr/local/rtnet/include
  

  

  Andreas Kasper

  

  

  

  Zitat Ramon Rusli <mon_...@yahoo.com>:
  
    
      
        
          Dear,

How can I use rtnet to send or receive packet in c? i am a bit confused because 
no examples or tutorial to build the program using rtnet while I search it in 
internet. i want to make a real time packet sending program and a real time 
packet receiver program using rtnet 0.9.12 and xenomai 2.5.4. the operating 
system that I use is linux fedora 13 kernel 2.6.32.11. 

Is it just add some include and some lines from standard udp packet server and 
client program? 

thanks a lot
==========================
//the standard udp packet server that I build (server.c)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include
 <mqueue.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sys/mman.h>
#include <signal.h>
#include <limits.h>
#define PORT 7788
#define BUFLEN 65000
#define NPACK 10000000

void diep (char *s)
{
    perror (s);
    exit (1);
}

int main (void)
{
    struct sockaddr_in si_me, si_other;
    int s, i, slen=sizeof(si_other);
    char buf[BUFLEN];

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))== -1)
    {
        diep("socket");    
    }

    memset ((char *) &si_me, 0, sizeof(si_me));
    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
   
 si_me.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(s, &si_me, sizeof(si_me)) == -1)
    {
        diep("bind");
    }

    for(i = 0; i < NPACK; i++)
    {
        if(recvfrom(s, buf, BUFLEN, 0, &si_other, &slen) == -1)
        {
            diep("recvfrom()");
        }
        printf("received packet from %s:%d\n Data: %s\n\n", 
inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
    }
    close(s);
    return 0;
}


=============================
//the standard udp packet client (client.c)
#include <stdlib.h>
#include
 <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <mqueue.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sys/mman.h>
#include <signal.h>
#include <limits.h>

#define PORT 7788
#define BUFLEN 65000
#define NPACK 10000000
#define SRV_IP "167.205.34.150"

void diep (char *s)
{
    perror (s);
    exit (1);
}

int main ()
{
    struct sockaddr_in si_other;
    int s, i, slen=sizeof(si_other);
    char buf[BUFLEN];

    if((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        diep("socket");
   
 }

    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family=AF_INET;
    si_other.sin_port = htons(PORT);
    if(inet_aton(SRV_IP, &si_other.sin_addr)==0)
    {
        fprintf(stderr, "inet_aton () failed\n");
        exit (1);
    }    

    for(i=0; i< NPACK; i++)
    {
        printf("sending packet %d\n", i);
        sprintf(buf, "This is packet %d\n",i);
        if (sendto(s, buf, BUFLEN, 0, &si_other, slen) == -1)
        {
            diep("sendto()");
       
 }
    }
    close(s);
return 0;
}
============================

        
      
    
 
  


      
------------------------------------------------------------------------------
Free Software Download: Index, Search & Analyze Logs and other IT data in 
Real-Time with Splunk. Collect, index and harness all the fast moving IT data 
generated by your applications, servers and devices whether physical, virtual
or in the cloud. Deliver compliance at lower cost and gain new business 
insights. http://p.sf.net/sfu/splunk-dev2dev 
_______________________________________________
RTnet-users mailing list
RTnet-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rtnet-users

Reply via email to