Advertisement
Atul_Kumar

CNLAB_PART-A

Aug 4th, 2022
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.56 KB | None | 0 0
  1. CN LAB
  2. PART A
  3.  
  4. 1.Write a program for error detecting code using 16 bits CRC-CCITT
  5. # include <stdio.h>
  6. # include <string.h>
  7. # include <stdlib.h>
  8. # define MAX 30
  9. void crc(char *data, char *gen, char *rem)
  10. {
  11.  int i, j ;
  12.  int dwordSize = strlen(data)-(strlen(gen)-1) ; // dataword size
  13.  char out[MAX]; // xored val after each step
  14.  strcpy(out, data);
  15.  
  16.  /* Perform XOR on the msg */
  17.  for(i=0; i<dwordSize ; i++)
  18.  {
  19.  if(out[i]=='0') continue ;
  20.  for(int j = 0 ; j<strlen(gen) ; j++){
  21.  out[i+j] = (gen[j] == out[i+j] ? '0' : '1') ;
  22.  }
  23.  }
  24.  for(i=0;i<strlen(gen)-1;i++)
  25.  {
  26.  rem[i]=out[dwordSize+i];
  27.  }
  28. }
  29. int main()
  30. {
  31.  int i, j;
  32.  char dword[MAX]; // dataword k bits
  33.  char augWord[MAX]; // augmented dataword
  34.  char cword[MAX]; // codeword n bits
  35.  char rem[MAX]; // remainder from crc
  36.  char recv[MAX]; // received message
  37.  char gen[MAX] = "10001000000100001"; // crc-16 (17 bits)
  38.  printf("\nCRC-16 Generator : x^16 + x^12 + x^5 + 1 ");
  39.  printf("\nBinary Form : %s", gen);
  40.  printf("\n\nEnter Dataword : ");
  41.  scanf("%s", dword);
  42.  strcpy(augWord, dword);
  43.  for(i=0; i<strlen(gen)-1; i++)
  44.  {
  45.  strcat(augWord, "0");
  46.  }
  47.  printf("\nAugmented dataword is : %s",augWord);
  48.  crc(augWord, gen, rem);
  49.  strcpy(cword, dword);
  50.  strcat(cword, rem);
  51.  printf("\n\nFinal data transmitted : %s", cword);
  52.  printf("\n\nEnter the data received : ");
  53.  scanf("%s", recv);
  54.  if(strlen(recv) < strlen(cword))
  55.  {
  56.  printf("\n Invalid input \n");
  57.  exit(0);
  58.  }
  59.  crc(recv, gen, rem);
  60.  printf("\nSyndrome = %s ", rem);
  61.  for(i=0; i<strlen(rem); i++)
  62.  {
  63.  if( rem[i] == '1')
  64.  {
  65.  printf("\nError occured !!! Corrupted data received.\n");
  66.  exit(0);
  67.  }
  68.  }
  69.  printf("\nNo Error. Data received successfully.\n");
  70. }
  71.  
  72.  
  73. --------------------------------------------------------------------------------------------------------------
  74. 2.For the given network graph, write a program to implement Link state routing
  75. algorithm to build a routing table for the given node.
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <stdbool.h>
  79. #define INFINITY 999
  80. #define MAX 100
  81. int cost[MAX][MAX];
  82. int distance[MAX];
  83. int visited[MAX] = {0};
  84. int parent[MAX];
  85. int source;
  86. int n;
  87. void initialize()
  88. {
  89.  int i;
  90.  visited[source] = 1;
  91.  parent[source] = source;
  92.  for(i=0; i<n; i++)
  93.  {
  94.  distance[i] = cost[source][i];
  95.  if( cost[source][i] != INFINITY )
  96.  {
  97.  parent[i] = source;
  98.  }
  99.  }
  100. }
  101. int GetMin()
  102. {
  103.  int minIdx = -1;
  104.  int minDist = INFINITY;
  105.  int i;
  106.  for(i=0; i<n; i++)
  107.  {
  108.  if( !visited[i] && minDist >= distance[i] )
  109.  {
  110.  minIdx = i;
  111.  minDist = distance[i];
  112.  }
  113.  }
  114.  return minIdx;
  115. }
  116. void updateTable(int node)
  117. {
  118.  int i;
  119.  for(i=0; i<n; i++)
  120.  {
  121.  if( cost[node][i] != INFINITY && distance[i] > distance[node]+cost[node][i] )
  122.  {
  123.  distance[i] = distance[node] + cost[node][i];
  124.  parent[i] = node;
  125.  }
  126.  }
  127. }
  128. void display()
  129. {
  130.  int i;
  131.  int node;
  132.  printf("\nNode \t Distance from source \t Path \n");
  133.  for(i=0; i<n; i++)
  134.  {
  135.  printf("%d \t\t %d \t\t", i, distance[i]);
  136.  node = i;
  137.  printf("%d", node);
  138.  while( node != source)
  139.  {
  140.  printf(" <- %d", parent[node]);
  141.  node = parent[node];
  142.  }
  143.  printf("\n");
  144.  }
  145. }
  146. int main()
  147. {
  148.  int i, j, node;
  149.  printf("Enter the number of nodes: ");
  150.  scanf("%d", &n);
  151.  printf("Enter the source node : ");
  152.  scanf("%d", &source);
  153.  printf("\nEnter the cost matrix: \n");
  154.  for(i=0; i<n; i++)
  155.  {
  156.  for(j=0; j<n; j++)
  157.  {
  158.  scanf("%d", &cost[i][j]);
  159.  if(cost[i][j]==0 && i!=j)
  160.  {
  161.  cost[i][j]=INFINITY;
  162.  }
  163.  }
  164.  }
  165.  initialize();
  166.  for(i=0; i<n-1; i++)
  167.  {
  168.  node = GetMin();
  169.  visited[node] = 1;
  170.  updateTable(node);
  171.  }
  172.  display();
  173.  return 0;
  174. }
  175.  
  176.  
  177.  
  178. -----------------------------------------------------------------------------------------------------------------------
  179. 3.Write a program to divide the message into variable length frames and sort
  180. them and display the message at the receiving side
  181. #include<stdio.h>
  182. #include<stdlib.h>
  183. #include<string.h>
  184. #include<time.h>
  185. #define MAX 100
  186. typedef struct{
  187.  int id;
  188.  char data[MAX];
  189. }frame;
  190. // Fisher yates algorithm to shuffle the frame
  191. void shuffleFrames(frame f[MAX], int n)
  192. {
  193.  int i;
  194.  for(i=n-1; i>=0; i--)
  195.  {
  196.  int j = rand()%(i+1);
  197.  frame temp = f[j];
  198.  f[j] = f[i];
  199.  f[i] = temp;
  200.  }
  201. }
  202. void sortFrames(frame f[MAX], int n)
  203. {
  204.  int i, j;
  205.  for(i=1; i<n; i++)
  206.  {
  207.  frame t = f[i];
  208.  j = i-1;
  209.  while(j>=0 && f[j].id > t.id)
  210.  {
  211.  f[j+1] = f[j];
  212.  j = j-1;
  213.  }
  214.  f[j+1] = t;
  215.  }
  216. }
  217. void showFrames(frame f[MAX] , int n ){
  218. int i;
  219.  printf("\nframe_id \t frame_data \n");
  220.  printf("----------------------------\n");
  221.  for(i=0 ; i < n; i++)
  222.  {
  223.  printf("%d \t\t %s \n", f[i].id, f[i].data);
  224.  }
  225. }
  226. int main()
  227. {
  228.  frame f[MAX];
  229.  int n = 0; // no of frames
  230.  int fsize; // size of frame
  231.  char msg[MAX];
  232.  int m = 0; // message iterator
  233.  int i, j;
  234.  printf("Enter a message : ");
  235.  fgets(msg , MAX, stdin);
  236.  msg[strlen(msg)-1] = '\0'; // to remove '\n' from string
  237.  
  238.  srand(time(NULL));
  239.  // Divide the message into frames
  240.  for(i=0 ; m < strlen(msg) ; i++)
  241.  {
  242.  f[i].id = i;
  243.  fsize = rand()%5+1; // variable Frame size in range [1,5]
  244.  n++; // count number of frames
  245.  
  246.  strncpy(f[i].data , msg+m , fsize) ;
  247.  m = m+fsize ;
  248.  }
  249.  shuffleFrames(f, n);
  250.  printf("\nShuffled frames:");
  251.  showFrames(f,n);
  252.  
  253.  sortFrames(f, n);
  254.  printf("\nSorted frames:");
  255.  showFrames(f,n);
  256.  
  257.  printf("\nfinal message : ");
  258.  for(i=0; i< n; i++)
  259.  {
  260.  printf("%s", f[i].data);
  261.  }
  262.  printf("\n");
  263. }
  264.  
  265.  
  266. -------------------------------------------------------------------------------------------------------------------
  267. 4.Using TCP/IP sockets, write a client – server program, the client sends the file
  268. name and the server sends back the requested text file if present.
  269. Server.c
  270. #include <stdio.h>
  271. #include <stdlib.h>
  272. #include <fcntl.h>
  273. #include <arpa/inet.h>
  274. #include <unistd.h>
  275. int main()
  276. {
  277.  int sersock, sock, fd, n;
  278.  char buffer[1024], fname[50];
  279.  
  280.  sersock = socket(AF_INET, SOCK_STREAM, 0);//Domain=AF_INET
  281. (Ipv4),type=SOCK_STREAM(TCP),protocol=0(IP)
  282.  struct sockaddr_in addr = { AF_INET, htons(1234), inet_addr("127.0.0.1") };
  283.  // Forcefully connecting to same port everytime
  284.  int reuse = 1;
  285. setsockopt(sersock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
  286.  /* attaching socket to port */
  287.  bind(sersock, (struct sockaddr *) &addr, sizeof(addr));
  288.  printf("\nServer is Online");
  289.  listen(sersock, 5); // listen(int sockfd, int backlog)
  290.  sock = accept(sersock, NULL, NULL);
  291.  /* receive the filename from client */
  292.  recv(sock, fname, 50, 0);
  293.  printf("\nRequesting for file: %s\n", fname);
  294.  fd = open(fname, O_RDONLY); // open file in read-only mode
  295.  if (fd < 0)
  296.  {
  297.  send(sock, "\nFile not found\n", 15, 0); // strlen(\nFile not found)=15
  298.  exit(0);
  299.  }
  300.  while ((n = read(fd, buffer, sizeof(buffer))) > 0)
  301.  {
  302.  send(sock, buffer, n, 0);
  303.  }
  304.  printf("\nFile content sent\n");
  305.  
  306.  close(fd);
  307.  return 0;
  308. }
  309. client.c
  310. #include <stdio.h>
  311. #include <stdlib.h>
  312. #include <fcntl.h>
  313. #include <arpa/inet.h>
  314. #include <unistd.h>
  315. int main()
  316. {
  317.  int sock, n;
  318.  char buffer[1024], fname[50];
  319.  sock = socket(AF_INET, SOCK_STREAM, 0);//Domain=AF_INET
  320. (Ipv4),type=SOCK_STREAM(TCP),protocol=0(IP)
  321.  struct sockaddr_in addr = { AF_INET, htons(1234), inet_addr("127.0.0.1") };
  322.  /* keep trying to esatablish connection with server */
  323.  while(connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) ;
  324.  printf("\nClient is connected to Server");
  325.  /* send the filename to the server */
  326.  printf("\nEnter file name: ");
  327.  scanf("%s", fname);
  328.  send(sock, fname, sizeof(fname), 0);
  329.  printf("\nRecieved file data\n");
  330.  printf("---------------------------------------------------------\n");
  331.  /* keep printing any data received from the server */
  332.  while ((n = recv(sock, buffer, sizeof(buffer), 0)) > 0)
  333.  {
  334.  buffer[n] = '\0' ;
  335.  printf("%s", buffer);
  336.  }
  337. printf("---------------------------------------------------------\n");
  338.  return 0;
  339. }
  340.  
  341.  
  342. ----------------------------------------------------------------------------------------------------------------------
  343. 5. Using FIFOs as IPC channels, write a client – server program, the client sends
  344. the file name and the server sends back the requested text file if present
  345. server.c
  346. #include <stdio.h>
  347. #include <unistd.h>
  348. #include <fcntl.h> // used for file handling
  349. #include <sys/stat.h> // used for mkfifo function
  350. #include <sys/types.h> // mkfifo() has dependency on both types.h and stat.h
  351. int main()
  352. {
  353.  char fname[50], buffer[1025];
  354.  int req, res, n, file;
  355.  
  356.  mkfifo("req.fifo", 0777);
  357.  mkfifo("res.fifo", 0777);
  358.  
  359.  printf("Waiting for request...\n");
  360.  req = open("req.fifo", O_RDONLY);
  361.  res = open("res.fifo", O_WRONLY);
  362.  
  363.  read(req, fname, sizeof(fname));
  364.  printf("Received request for %s\n", fname);
  365.  
  366.  file = open(fname, O_RDONLY);
  367.  if (file < 0)
  368.  {
  369.  write(res, "File not found\n", 15);
  370.  }
  371.  else
  372.  {
  373.  while((n = read(file, buffer, sizeof(buffer))) > 0)
  374.  {
  375.  write(res, buffer, n);
  376.  }
  377.  }
  378.  
  379.  close(req);
  380.  close(res);
  381.  
  382.  unlink("req.fifo");
  383.  unlink("res.fifo");
  384.  
  385.  return 0;
  386. }
  387. client.c
  388. #include <stdio.h>
  389. #include <unistd.h>
  390. #include <stdlib.h>
  391. #include <fcntl.h>
  392. #include <sys/stat.h>
  393. #include <sys/types.h>
  394. int main()
  395. {
  396.  char fname[50], buffer[1025];
  397.  int req, res, n;
  398.  req = open("req.fifo", O_WRONLY);
  399.  res = open("res.fifo", O_RDONLY);
  400.  if(req < 0 || res < 0)
  401.  {
  402.  printf("Please Start the server first\n");
  403.  exit(-1);
  404.  }
  405.  printf("Enter filename to request : ");
  406.  scanf("%s", fname);
  407.  // write file name to request file
  408.  write(req, fname, sizeof(fname));
  409.  printf("Received response\n");
  410.  printf("-------------------------------------------\n");
  411.  while((n = read(res, buffer, sizeof(buffer)))>0)
  412.  {
  413.  buffer[n]= '\0' ;
  414.  printf("%s", buffer);
  415.  }
  416.  printf("-------------------------------------------\n");
  417.  
  418.  close(req);
  419.  close(res);
  420.  return 0;
  421. }
  422.  
  423.  
  424.  
  425. -------------------------------------------------------------------------------------------------------
  426. 6. Using UDP, write a client – server program, to exchange messages between
  427. client and the server.
  428. Server.c
  429. #include <stdio.h>
  430. #include <stdlib.h>
  431. #include <unistd.h>
  432. #include <string.h>
  433. #include <sys/types.h>
  434. #include <sys/socket.h>
  435. #include <arpa/inet.h>
  436. #include <netinet/in.h>
  437. #define MAX 1024
  438. int main()
  439. {
  440. char buffer[MAX], msg[MAX];
  441. struct sockaddr_in servaddr, cliaddr;
  442. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  443. // Forcefully connecting to same port everytime
  444. int reuse = 1;
  445. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
  446. // Initialize servaddr and cliaddr with zero
  447. memset(&servaddr, 0, sizeof(servaddr));
  448. memset(&cliaddr, 0, sizeof(cliaddr));
  449. int len = sizeof(cliaddr);
  450. // Filling server information
  451. servaddr.sin_family = AF_INET; // IPv4
  452. servaddr.sin_addr.s_addr = INADDR_ANY; //INADDR_ANY listen on all available
  453. interfaces
  454. servaddr.sin_port = htons(1234);
  455. // Bind the socket with the server address
  456. bind(sock, (const struct sockaddr *)&servaddr, sizeof(servaddr));
  457. printf("Waiting for message from client...\n");
  458. while(1)
  459. {
  460.  int n = recvfrom(sock, (char *)buffer, sizeof(buffer), 0, ( struct sockaddr *) &cliaddr,
  461. &len);
  462.  buffer[n] = '\0';
  463.  printf("Client : %s", buffer);
  464.  printf("Server : ");
  465.  fgets(msg, MAX, stdin);
  466.  sendto(sock, (const char *)msg, strlen(msg), 0, (const struct sockaddr *) &cliaddr, len);
  467. }
  468. return 0; }
  469. client.c
  470. #include <stdio.h>
  471. #include <stdlib.h>
  472. #include <unistd.h>
  473. #include <string.h>
  474. #include <sys/types.h>
  475. #include <sys/socket.h>
  476. #include <arpa/inet.h>
  477. #include <netinet/in.h>
  478. #define MAX 1024
  479. int main()
  480. {
  481. char buffer[MAX], msg[MAX];
  482. struct sockaddr_in servaddr;
  483. // Creating socket file descriptor
  484. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  485. memset(&servaddr, 0, sizeof(servaddr));
  486. // Filling server information
  487. servaddr.sin_family = AF_INET;
  488. servaddr.sin_port = htons(1234); // htons(port)
  489. servaddr.sin_addr.s_addr = INADDR_ANY;
  490. while(1)
  491. {
  492. printf("Client : ");
  493. fgets(msg, MAX, stdin);
  494. sendto(sock, (const char *)msg, strlen(msg), 0, (const struct sockaddr *) &servaddr,
  495. sizeof(servaddr));
  496. int n = recvfrom(sock, (char *)buffer, sizeof(buffer), 0, NULL, NULL);
  497. buffer[n] = '\0';
  498. printf("Server : %s", buffer);
  499. }
  500. return 0;
  501. }
  502.  
  503.  
  504. -------------------------------------------------------------------------------------------------------------
  505. 7. Write a socket program to demonstrate IP multicasting which provides the
  506. capability for an application to send a single IP datagram that a group of hosts
  507. in a network can receive.
  508. Server.c
  509. #include <stdio.h>
  510. #include <stdlib.h>
  511. #include <unistd.h>
  512. #include <string.h>
  513. #include <sys/types.h>
  514. #include <sys/socket.h>
  515. #include <arpa/inet.h>
  516. #include <netinet/in.h>
  517. int main ()
  518. {
  519.  int sock;
  520.  char msg[1024];
  521.  struct sockaddr_in groupaddr;
  522.  struct in_addr localInterface;
  523.  sock = socket(AF_INET, SOCK_DGRAM, 0);
  524.  memset(&groupaddr, 0, sizeof(groupaddr));
  525.  groupaddr.sin_family = AF_INET;
  526.  groupaddr.sin_addr.s_addr = inet_addr("226.1.1.1");
  527.  groupaddr.sin_port = htons(1234);
  528.  localInterface.s_addr = inet_addr("127.0.0.1"); // or system ip address
  529.  setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface,
  530. sizeof(localInterface));
  531.  printf("Enter message : ");
  532.  fgets(msg, 1024, stdin);
  533.  msg[strlen(msg)-1] = '\0'; // to remove '\n' from string
  534.  sendto(sock, msg, sizeof(msg), 0, (struct sockaddr*)&groupaddr, sizeof(groupaddr));
  535.  printf("Message Sent.\n");
  536.  
  537.  return 0;
  538. }
  539. client.c
  540. #include <stdio.h>
  541. #include <stdlib.h>
  542. #include <unistd.h>
  543. #include <string.h>
  544. #include <sys/types.h>
  545. #include <sys/socket.h>
  546. #include <arpa/inet.h>
  547. #include <netinet/in.h>
  548. int main()
  549. {
  550. int sock, reuse = 1;
  551. char msg[1024];
  552. struct sockaddr_in addr;
  553. struct ip_mreq group;
  554. sock = socket(AF_INET, SOCK_DGRAM, 0);
  555. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
  556. memset(&addr, 0, sizeof(addr));
  557. addr.sin_family = AF_INET;
  558. addr.sin_port = htons(1234);
  559. addr.sin_addr.s_addr = INADDR_ANY; // to listen on all available interfaces.
  560. bind(sock, (struct sockaddr*)&addr, sizeof(addr));
  561. group.imr_multiaddr.s_addr = inet_addr("226.1.1.1");
  562. group.imr_interface.s_addr = inet_addr("127.0.0.1");
  563. setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group));
  564. printf("Waiting for message from server.....");
  565. // recvfrom(sock, msg, sizeof(msg), 0, NULL, NULL);
  566. read(sock, msg, sizeof(msg));
  567. printf("\nThe message from multicast server is : %s \n", msg);
  568. close(sock);
  569. return 0;
  570. }
  571.  
  572.  
  573. ---------------------------------------------------------------------------------------------------------
  574. 8.Write a program to implement sliding window protocol between two hosts.
  575. Server.c
  576. #include<stdio.h>
  577. #include<stdlib.h>
  578. #include<string.h>
  579. #include<time.h>
  580. #include<unistd.h>
  581. #include<sys/socket.h>
  582. #include<sys/types.h>
  583. #include<arpa/inet.h>
  584. #define MAX 20
  585. int main()
  586. {
  587. int sersock = socket(AF_INET, SOCK_STREAM, 0);
  588. struct sockaddr_in addr = { AF_INET, htons(1234), inet_addr("127.0.0.1") };
  589. // Forcefully connecting to same port everytime
  590. int reuse = 1;
  591. setsockopt(sersock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
  592. bind(sersock, (struct sockaddr *) &addr, sizeof(addr));
  593. printf("\nServer is Online\n");
  594. listen(sersock, 5);
  595. int sock = accept(sersock, NULL, NULL);
  596. char frame[MAX];
  597. char res[MAX]; // to store all bytes that are recieved successfully
  598. int ack;
  599. int k=0; // iterator for res
  600. srand(time(NULL));
  601. while(1)
  602. {
  603. int recvsize = 5;
  604. memset(frame, 0, MAX); // re-initialise frame buffer with 0
  605. recv(sock, frame, recvsize, 0); // recv(socket, buffer, length, flag)
  606. if(strcmp(frame, "Exit") == 0) break; // at end exit frame is recieved
  607. if(strlen(frame) < recvsize)
  608. {
  609. recvsize = strlen(frame);
  610. }
  611. int err_idx = rand()%8; // probability of byte to get corrupted = 50%
  612. if(err_idx < recvsize)
  613. {
  614. recvsize = err_idx;
  615. frame[err_idx]='x';
  616. printf("\nError occured at = %d", err_idx+1);
  617. }
  618. int j;
  619. for(j=0; j<recvsize ; j++)
  620. {
  621. res[k++] = frame[j];
  622. }
  623. printf("\nPacket received = %s", frame);
  624. printf("\nReceiving window: ");
  625. printf("\n start seqno = %d", k-recvsize);
  626. printf("\n end seqno = %d", k-1);
  627. ack = k ;
  628. printf("\nSending ack = %d", ack);
  629. send(sock, &ack, sizeof(ack), 0) ;
  630. printf("\n");
  631. }
  632. res[k] = '\0';
  633. printf("\n\nFinal string recieved at Destination = ");
  634. fputs(res, stdout);
  635. printf("\n\n");
  636. close(sock); close(sersock);
  637. }
  638. client.c
  639. #include<stdio.h>
  640. #include<stdlib.h>
  641. #include<string.h>
  642. #include<unistd.h>
  643. #include<sys/socket.h>
  644. #include<sys/types.h>
  645. #include<arpa/inet.h>
  646. #define MAX 20
  647. int main()
  648. {
  649.  int sock = socket(AF_INET, SOCK_STREAM, 0);
  650.  struct sockaddr_in addr = { AF_INET, htons(1234), inet_addr("127.0.0.1") };
  651.  /* keep trying to establish connection with server */
  652.  while(connect(sock, (struct sockaddr *) &addr, sizeof(addr))) ;
  653.  printf("\nClient is connected to Server\n");
  654.  char msg[MAX];
  655.  printf("\nEnter message to send : ");
  656.  fgets(msg, MAX, stdin);
  657.  msg[strlen(msg)-1] = '\0';
  658.  char frame[MAX];
  659.  int i = 0;
  660.  int ack;
  661.  
  662.  while(i<strlen(msg))
  663.  {
  664.  int sendsize = 5;
  665.  memset(frame, 0, MAX); // re-initialise frame buffer with 0
  666.  // strncpy(destination , source , length)
  667.  strncpy(frame, msg+i, sendsize); //copy msg to frame
  668.  if( sendsize > strlen(frame) )
  669.  {
  670.  sendsize = strlen(frame);
  671.  }
  672.  printf("\n\nSending packet = %s", frame);
  673.  printf("\nSending window: ");
  674.  printf("\n start seqno = %d", i);
  675.  printf("\n end seqno = %d", i+sendsize-1);
  676.  send(sock, frame, sendsize, 0);
  677.  recv(sock, &ack, sizeof(ack), 0);
  678.  printf("\nreceived ack no = %d ",ack);
  679.  i = ack; // next data seq no = incoming ack no
  680.  }
  681.  send(sock, "Exit", strlen("Exit"), 0);
  682.  close(sock); printf("\n\n");
  683. }
  684.  
  685. ----------------------------------------------------- THE END------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement