Friday, March 23, 2012

Server Code to Create ATM PVC

This code opens connection on ATM socket defined for PVC on VPI/VCI = 0/64. QOS is defined over AAL5 UBR Traffic using ATM on Linux distribution from sourceforge.net



#define MAX_SDU     20000
#define MAX_OFFSET  8

void usage(char *name)
{
  fprintf(stderr,"usage: %s [vpi] [vci]\n",name);
  exit(1);
}

int main(int argc, char *argv[])
{
  struct sockaddr_atmpvc addr;
  struct atm_qos         qos;
  int    fd;
  int    pid, lst;
  static unsigned char buffer[30];
  int    num;
  char * test = "Sending test";
 
  /*if (argc != 3) {usage(argv[0]);}
  */
  printf("Creating a pvc with vpi/vci 0/64... \n");

  if ((fd = socket(PF_ATMPVC, SOCK_DGRAM, 0)) < 0)
  {
    perror("socket error");
    return 1;
  }
  printf("created socket with id: ");
  printf("%d",fd);
  printf("\n");

  memset(&addr,0,sizeof(addr));
  addr.sap_family = AF_ATMPVC;
  addr.sap_addr.itf = 0;
  addr.sap_addr.vpi = 0;
  addr.sap_addr.vci = 64;

  memset(&qos,0,sizeof(qos));
  qos.aal = ATM_AAL5;
  qos.txtp.traffic_class = qos.rxtp.traffic_class = ATM_UBR;
  qos.txtp.max_sdu = qos.rxtp.max_sdu = MAX_SDU;
  if (setsockopt(fd,SOL_ATM,SO_ATMQOS,&qos,sizeof(qos)) < 0)
  {
        perror("setsockopt SO_ATMQOS");
        return 1;
  }
  printf("Set socket options to AAL5, UBR traffic...\n");
  if (bind(fd,(struct sockaddr *) &addr, sizeof(addr)) < 0)
  {
       perror("bind error");
       return 1;
  }
  srandom(0); /* we want it to be deterministic */

  printf("Server: listening on vc 0,64...\n");
  while (1)
  {
    memset(buffer,0, sizeof(buffer));
    lst = read(fd, buffer, sizeof(buffer));
    if (lst<0)
      perror("server: listen error\n");
    if (lst > 0)
    {
      printf("Server: received bytes %d\n", lst);
      printf("server: message received = %s\n", buffer);
    }
  }

  return 0;
}

No comments: