How to add real applications into NCTUNS?

 

[Goal]

 In this tutorial, UDP-based socket programs which contain one echo client and one echo server program. The echo client sends the “hello world” to the echo server. Then the echo server sends the same message “hello world” back to the echo client. In the meantime, there is a monitor pc which runs the tcpdump application on it. After simulation, the transmitted packets can be watched via wireshark.

 

[Preparation]

1.      Prepare the echo client and echo server program. One can directly store these two files under /usr/local/nctuns/tools. Or create the links to the programs.

(udp_c.c)

#include <sys/types.h>

#include <sys/socket.h>

#include <string.h>

#include <netinet/in.h>

#include <stdio.h>

#include <stdlib.h>

#include <arpa/inet.h>

#include <unistd.h>

 

#define MAXLINE 80

#define SERV_PORT 8888

 

void do_cli(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen)

{

  int n;

  char sendline[MAXLINE], recvline[MAXLINE + 1];

 

  /* connect to server */

  if(connect(sockfd, (struct sockaddr *)pservaddr, servlen) == -1)

  {

    perror("connect error");

    exit(1);

  }

 

    /* read a line and send to server */

    strcpy(sendline,"hello world");

    write(sockfd, sendline, strlen(sendline));

    /* receive data from server */

    n = read(sockfd, recvline, MAXLINE);

    if(n == -1)

    {

      perror("read error");

      exit(1);

    }

    recvline[n] = 0; /* terminate string */

    fputs(recvline, stdout);

}

 

int main(int argc, char **argv)

{

  int sockfd;

  struct sockaddr_in servaddr;

 

  /* check args */

  if(argc != 2)

  {

    printf("usage: udpclient <IPaddress>\n");

    exit(1);

  }

 

  /* init servaddr */

  bzero(&servaddr, sizeof(servaddr));

  servaddr.sin_family = AF_INET;

  servaddr.sin_port = htons(SERV_PORT);

  if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)

  {

    printf("[%s] is not a valid IPaddress\n", argv[1]);

    exit(1);

  }

 

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);

 

  do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

 

  return 0;

}

 

udp_s.c

#include <sys/types.h>

#include <sys/socket.h>

#include <string.h>

#include <netinet/in.h>

#include <stdio.h>

#include <stdlib.h>

 

#define MAXLINE 80

#define SERV_PORT 8888

 

void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)

{

  int n;

  socklen_t len;

  char mesg[MAXLINE];

 

  for(;;)

  {

    len = clilen;

    /* waiting for receive data */

    n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);

    /* sent data back to client */

    sendto(sockfd, mesg, n, 0, pcliaddr, len);

  }

}

 

int main(void)

{

  int sockfd;

  struct sockaddr_in servaddr, cliaddr;

 

  sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */

 

  /* init servaddr */

  bzero(&servaddr, sizeof(servaddr));

  servaddr.sin_family = AF_INET;

  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

  servaddr.sin_port = htons(SERV_PORT);

 

  /* bind address and port to socket */

  if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)

  {

    perror("bind error");

    exit(1);

  }

 

  do_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));

 

  return 0;

}

 

 

2.      Install wireshark (If the system has installed the wireshark, it will show the following message. Otherwise, it will start to install wireshark.)

 

[Simulation]

1.      Create a topology that contains one sender (node 1), one receiver (node 3), and one monitor (node 2). These three nodes are connected via a hub.

 

2.      Change to “E” to add the application.

 

 

 

3.       Click to “R” and start simulation.

 

4.      After simulation, open the log file via wireshark to see what are captured.

 

 

 

 

Dr. Chih-Heng Ke (http://csie.nqu.edu.tw/smallko), smallko@gmail.com

Department of Computer Science and Information Engineering, National Quemoy University, Taiwan