Guest User

Untitled

a guest
Nov 6th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.92 KB | None | 0 0
  1. 1.
  2. #include<iostream>
  3. #include<string.h>
  4. using namespace std;
  5. char stack[6][30];
  6. int top=-1;
  7. void push()
  8. { int j;
  9. if(top==5)
  10. cout<<"stack is full"; //to check whether stack is full or not
  11. else
  12. {
  13. char x[30];
  14. cout<<"Enter layer name: \n";
  15. cin>>x;
  16. top++;
  17. for(j=0;x[j]!='\0';j++)
  18. {
  19. stack[top][j]=x[j]; //Layer names are pushed into the stack
  20. }
  21. }
  22. }
  23. void pop(char st[30])
  24. {int j;
  25. char x[30];
  26. if(top==-1)
  27. cout<<"stack is empty"; //to check whether stack is empty or not
  28. else
  29. {
  30. for(j=0;stack[top][j]!='\0';j++)
  31. {
  32. x[j]=stack[top][j]; //popping out the layer names from the stack
  33. }
  34. top--;
  35. cout<<x;
  36. for(j=0;j<30;j++)
  37. {
  38. x[j]='\0';
  39. }
  40. cout<<" "<<st<<"\n";
  41. cout<<"\n\n";
  42. }
  43. }
  44. void display()
  45. {
  46. char s[30];
  47. cout<<"Enter the string: ";
  48. cin>>s;
  49. cout<<"-----------\n";
  50. cout<<"TRANSMITTER\n";
  51. cout<<"-----------\n\n";
  52. cout<<stack[0]<<"\n\n";
  53. for(int i=1;i<=top;i++)
  54. {
  55. cout<<stack[i]; //Printing the stack
  56. cout<<" "<<s<<"\n";
  57. cout<<"\n\n";
  58. }
  59. cout<<"MESSAGE ENTERED INTO PHYSICAL LAYER AND TRANSMITTED.\n\n";
  60. cout<<"-----------\n";
  61. cout<<"RECEIVER\n";
  62. cout<<"-----------\n\n";
  63. cout<<"MESSAGE ENTERED INTO PHYSICAL LAYER.\n";
  64. for(int i=top;i>=1;i--)
  65. pop(s);
  66. cout<<stack[0]<<"\n\n"; //Printing the first element of the stack
  67. }
  68. int main()
  69. {
  70. int i;
  71. for(i=0;i<6;i++)
  72. push();
  73. cout<<"--------------------\n\n";
  74. display();
  75. return 0;
  76.  
  77. }
  78.  
  79. 2. a) Bit stuffing
  80. #include<stdio.h>
  81. #include<conio.h>
  82. #include<string.h>
  83. void main()
  84. {
  85. inti,j,count=0,nl;
  86. charstr[100];
  87. //clrscr();
  88. printf("enter the bit string: ");
  89. gets(str);
  90. for (i=0;i<strlen(str);i++) {
  91. count=0;
  92. //the following code search the six ones in given string
  93. for (j=i;j<=(i+5);j++) {
  94. if(str[j]=='1') {
  95. count++;
  96. }
  97. }
  98. //if there is six ones then folling code execute to bit stuffing after five ones
  99. if(count==6) {
  100. nl=strlen(str)+2;
  101. for (;nl>=(i+5);nl--) {
  102. str[nl]=str[nl-1];
  103. }
  104. str[i+5]='0';
  105. i=i+7;
  106. }
  107. }
  108. puts(str);
  109. getch();
  110. }
  111.  
  112.  
  113. b) Byte stuffing
  114. /*Implement the DataLinklayer framing methods such as Character
  115. stuffing*/
  116. #include<stdio.h>
  117. #include<string.h>
  118. #include<conio.h>
  119. void main()
  120. {
  121. intj,l,m,c,k;
  122. char a[50],b[50];
  123. printf("Enter the string: ");
  124. scanf("%s",a);
  125. strcpy(b,"DLESTX"); //Copy DLESTX in b
  126. m=strlen(a); //Length of a is stored in m
  127. for(j=0;j<m;)
  128. {
  129. if(a[j]=='d')
  130. {
  131. if(a[j+1]=='l')
  132. {
  133. if(a[j+2]=='e')
  134. {
  135. c=j+2;
  136. for(l=0;l<3;l++)
  137. {
  138. for(k=m;k>c;k--)
  139. {
  140. a[k]=a[k-1];
  141. }
  142. m++;
  143. a[m]=NULL;
  144. c+=1;
  145. }
  146. a[j+3]='d';
  147. a[j+4]='l';
  148. a[j+5]='e';
  149. a[m]=NULL;
  150. j+=5;
  151. }
  152. }
  153. }
  154. j++;
  155. }
  156. strcat(b,a);
  157. strcat(b,"DLEETX");
  158. printf("\n%s",b);
  159. printf("\nReceiver side:"); //Reciever side
  160. m=strlen(a);
  161. for(j=0;j<m;)
  162. {
  163. if(a[j]=='d')
  164. {
  165. if(a[j+1]=='l')
  166. {
  167. if(a[j+2]=='e')
  168. {
  169. c=j;
  170. for(l=0;l<3;l++)
  171. {
  172. for(k=c;k<m;k++)
  173. a[k]=a[k+1];
  174. }
  175. c++;
  176. }
  177. j=c;
  178. }
  179. }
  180. j++;
  181. }
  182. printf("\n%s",a);
  183. getch();
  184. }
  185.  
  186. c) To implement Character Count
  187. Program-
  188. #include <stdio.h>
  189. #include <string.h>
  190. int main(){
  191. int size;
  192. int total=0;
  193. int i=1;
  194. intj,k=0;
  195. intfsize;
  196. printf("Enter the no. of characters: "); //Entering number of characters
  197. scanf("%d",&size);
  198. charstr[size];
  199. printf("\nEnter the characters: ");
  200. scanf("%s",str);
  201. while(k<size)
  202. {
  203. printf("Enter the frame %d size: ",i);
  204. scanf("%d",&fsize);
  205. char frame[fsize+1]; //Incrementing frame size
  206. i++;
  207. total+=fsize;
  208. frame[0]=(char)(fsize+48);
  209. for(j=1;j<fsize;j++)
  210. {
  211. if(k<size)
  212. {
  213. printf("%c\n",str[k]);
  214. frame[j]=str[k];
  215. k++;
  216. }
  217. }
  218. frame[fsize]='\0';
  219. printf("%s\n",frame);
  220. }
  221. return 0;
  222. }
  223.  
  224. 3. CRC
  225. #include<stdlib.h>
  226. #include<conio.h>
  227. #include<stdio.h>
  228. void main()
  229. {
  230. inti,j,n,g,a,arr[20],gen[20],b[20],q[20],s;
  231.  
  232. printf("Transmitter side:");
  233. printf("\nEnter no. of data bits:"); //Number of data bits are entered
  234. scanf("%d",&n);
  235. printf("Enter data:");
  236. for(i=0;i<n;i++)
  237. scanf("%d",&arr[i]); //Data is stored in the form of an array
  238.  
  239. printf("Enter size of generator:");
  240. scanf("%d",&g);
  241. do{
  242. printf("Enter generator:"); //Divisor value is entered by the user
  243. for(j=0;j<g;j++)
  244. scanf("%d",&gen[j]);
  245.  
  246. }
  247. while(gen[0]!=1); //Converting into Matrix
  248. printf("\n\tThe generator matrix:");
  249. for(j=0;j<g;j++)
  250. printf("%d",gen[j]);
  251.  
  252. a=n+(g-1);
  253. printf("\n\tThe appended matrix is:"); //Displaying appended matrix
  254. for(i=0;i< j;++i)
  255. arr[n+i]=0;
  256.  
  257. for(i=0;i< a;++i)
  258.  
  259. printf("%d",arr[i]);
  260.  
  261. for(i=0;i< n;++i)
  262. q[i]= arr[i];
  263.  
  264. for(i=0;i< n;++i)
  265. {
  266. if(arr[i]==0)
  267. {
  268. for(j=i;j<g+i;++j)
  269. arr[j] = arr[j]^0;
  270. }
  271. else //Performing Division
  272. {
  273. arr[i] = arr[i]^gen[0];
  274. arr[i+1]=arr[i+1]^gen[1];
  275. arr[i+2]=arr[i+2]^gen[2];
  276. arr[i+3]=arr[i+3]^gen[3];
  277. }
  278. }
  279. printf("\n\tThe CRC is :");
  280. for(i=n;i< a;++i)
  281. printf("%d",arr[i]);
  282. s=n+a;
  283. for(i=n;i<s;i++)
  284. q[i]=arr[i];
  285. printf("\n");
  286. for(i=0;i<a;i++)
  287. printf("%d",q[i]);
  288. getch();
  289. }
  290.  
  291. 4. Checksum Calculation
  292.  
  293. Code:
  294. #include<stdio.h>
  295. #include<math.h>
  296. int sender(int b[10],int k)
  297. {
  298. intchecksum,sum=0,i;
  299. printf("\n****SENDER****\n");
  300. for(i=0;i<k;i++)
  301. sum+=b[i]; //Performing sum on each value
  302. printf("SUM IS: %d",sum);
  303. checksum=~sum;
  304. printf("\nSENDER's CHECKSUM IS:%d",checksum);
  305. return checksum; //returning value of checksum
  306. }
  307. int receiver(int c[10],intk,intscheck)
  308. { intchecksum,sum=0,i;
  309. printf("\n\n****RECEIVER****\n");
  310. for(i=0;i<k;i++)
  311. sum+=c[i];
  312. printf(" RECEIVER SUM IS:%d",sum);
  313. sum=sum+scheck;
  314. checksum=~sum;
  315. printf("\nRECEIVER's CHECKSUM IS:%d",checksum);
  316. return checksum;
  317. }
  318. int main()
  319. {
  320. int a[10],i,m,scheck,rcheck;
  321. printf("\nENTER SIZE OF THE STRING:");
  322. scanf("%d",&m);
  323. printf("\nENTER THE ELEMENTS OF THE ARRAY:");
  324. for(i=0;i<m;i++)
  325. scanf("%d",&a[i]);
  326. scheck=sender(a,m);
  327. rcheck=receiver(a,m,scheck);
  328. if(rcheck==0)
  329. printf("\n\nNO ERROR IN TRANSMISSION\n\n");
  330. else
  331. printf("\n\nERROR DETECTED");
  332. return 0;
  333. }
  334.  
  335. 5. Hamming Code
  336. Code:
  337. #include<stdio.h>
  338. void main() {
  339. int data[7],rec[7],i,c1,c2,c3,c;
  340. printf("this works for message of 4bits in size \nenter message bit one by
  341. one: ");
  342. scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]); //data is stored
  343. data[6]=data[0]^data[2]^data[4];
  344. data[5]=data[0]^data[1]^data[4];
  345. data[3]=data[0]^data[1]^data[2];
  346. printf("\nthe encoded bits are given below: \n");
  347. for (i=0;i<7;i++) {
  348. printf("%d ",data[i]);
  349. }
  350. printf("\nenter the received data bits one by one: ");
  351. for (i=0;i<7;i++) {
  352. scanf("%d",&rec[i]);
  353. }
  354. c1=rec[6]^rec[4]^rec[2]^rec[0];
  355. c2=rec[5]^rec[4]^rec[1]^rec[0];
  356. c3=rec[3]^rec[2]^rec[1]^rec[0];
  357. c=c3*4+c2*2+c1 ;
  358. if(c==0) {
  359. printf("\ncongratulations there is no error: ");
  360. } else {
  361. printf("\nerror on the postion: %d\nthe correct message is \n",c);
  362. if(rec[7-c]==0)
  363. rec[7-c]=1; else
  364. rec[7-c]=0;
  365. for (i=0;i<7;i++) {
  366. printf("%d ",rec[i]); //Printing the correct message
  367. }
  368. }
  369. }
  370.  
  371. 6.Simulation of Sliding protocol
  372. PROGRAM:
  373. #include<stdio.h>
  374. int main()
  375. {
  376. char sender[50],receiver[50],temp[50];
  377. inti,winsize;
  378.  
  379. printf("\n ENTER THE WINDOWS SIZE : ");
  380. scanf("%d",&winsize);
  381. printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n");
  382. printf("\n ENTER THE DATA TO BE SENT: ");
  383. fflush(stdin);
  384. gets(sender);
  385. for(i=0;i<winsize;i++)
  386. temp[i]= receiver[i];
  387. receiver[i]=sender[i];
  388. receiver[i]=temp[i];
  389. printf("\n MESSAGE SEND BY THE SENDER:\n");
  390. puts(sender);
  391. printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
  392. printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
  393. for(i=0;i<winsize;i++);
  394. printf("\n ACK:%d",i);
  395. printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
  396. puts(receiver);
  397. printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
  398.  
  399. }
  400.  
  401.  
  402. 7. Chat application using TCP/IP
  403.  
  404.  
  405. Code:
  406. // Server Program - Communicating to single client multiple number of times
  407. #include<stdio.h>
  408. #include<netinet/in.h>
  409. #include<netdb.h>
  410. #include<string.h>
  411. #define SERV_TCP_PORT 5035
  412. int main(int argc,char**argv)
  413. {
  414. int sockfd,newsockfd,clength;
  415. struct sockaddr_in serv_addr,cli_addr;
  416. char buffer[4096];
  417. //creating a socket
  418. sockfd=socket(AF_INET,SOCK_STREAM,0);
  419. serv_addr.sin_family=AF_INET;
  420. serv_addr.sin_addr.s_addr=INADDR_ANY;
  421. serv_addr.sin_port=htons(SERV_TCP_PORT);
  422. printf("\nStart");
  423. //bind the socket to port address and any local interface
  424. bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
  425. printf("\nListening...");
  426. printf("\n");
  427. listen(sockfd,5);
  428. clength=sizeof(cli_addr);
  429. newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength);
  430. //newsockfd=accept(sockfd,(struct sockaddr*)NULL,NULL);
  431. printf("\nAccepted");
  432. printf("\n");
  433. while(1)
  434. {
  435. bzero(buffer,4096);
  436. read(newsockfd,buffer,4096);
  437. printf("\nClient message:%s",buffer);
  438. write(newsockfd,buffer,strlen(buffer)+1);
  439. printf("\n");
  440. }
  441. //close(sockfd);
  442. //return 0;
  443. }
  444.  
  445.  
  446. Client Side:
  447. Client Program - Multiple communications to server
  448. #include<stdio.h>
  449. #include<sys/types.h>
  450. #include<sys/socket.h>
  451. #include<netinet/in.h>
  452. #include<netdb.h>
  453. #define SERV_TCP_PORT 5035
  454. int main(int argc,char*argv[])
  455. {
  456. int sockfd;
  457. struct sockaddr_in serv_addr;
  458. struct hostent *server;
  459. char buffer[4096];
  460. int k=5,cnt=0;
  461. sockfd=socket(AF_INET,SOCK_STREAM,0);
  462. serv_addr.sin_family=AF_INET;
  463. serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
  464. serv_addr.sin_port=htons(SERV_TCP_PORT);
  465. printf("\nReady for sending...");
  466. connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
  467. while(cnt <= k)
  468. {
  469. printf("\nEnter the message to send\n");
  470. printf("\nClient: ");
  471. fgets(buffer,4096,stdin);
  472. write(sockfd,buffer,4096);
  473. printf("Serverecho:%s",buffer);
  474. printf("\n");
  475. cnt++;
  476. }
  477. close(sockfd);
  478. return 0;
  479.  
  480. 8. TCP client/server application for transferring a text file from client to server
  481.  
  482. PROGRAM:
  483. Server:
  484. #include<stdio.h>
  485. #include<sys/types.h>
  486. #include<string.h>
  487. #include<stdlib.h>
  488. #include<sys/socket.h>
  489. #include<arpa/inet.h>
  490. #include<unistd.h>
  491. #define SA structsockaddr
  492. #define LISTENQ 5
  493. int main(intargc,char**argv)
  494. {
  495. intfd,sockfd,listenfd,connfd;
  496. pid_tchildpid;
  497. socklen_t client;
  498. structsockaddr_inservaddr,cliaddr;
  499. listenfd=socket(AF_INET,SOCK_STREAM,0);
  500. bzero(&servaddr,sizeof(servaddr));
  501. servaddr.sin_family=AF_INET;
  502. servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
  503. servaddr.sin_port=htons(atoi(argv[1]));
  504. bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
  505. listen(listenfd,LISTENQ);
  506. client=sizeof(cliaddr);
  507. connfd=accept(listenfd,(SA*)&cliaddr,&client);
  508. char buffer[100];
  509. FILE *fp;
  510. read(connfd,buffer,100);
  511. fp=fopen("add1.txt","w");
  512. fprintf(fp,"%s",buffer);
  513. printf("the file was received successfully");
  514. printf("the new file created is add1.txt");
  515. }
  516. Client :
  517. #include<arpa/inet.h>
  518. #include<unistd.h>
  519. #define SA structsockaddr
  520. int main(intargc,char**argv)
  521. {
  522. intsockfd;
  523. charfname[25];
  524. intlen;
  525. structsockaddr_inservaddr,cliaddr;
  526. sockfd=socket(AF_INET,SOCK_STREAM,0);
  527. bzero(&servaddr,sizeof(servaddr));
  528. servaddr.sin_family=AF_INET;
  529. servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
  530. servaddr.sin_port=htons(atoi(argv[1]));
  531. inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
  532. connect(sockfd,(SA*)&servaddr,sizeof(servaddr));
  533. char buffer[100];
  534. FILE *f;
  535. f=fopen("add.txt","r");
  536. fscanf(f,"%s",buffer);
  537. write(sockfd,buffer,100);
  538. printf("the file was sent successfully");
  539. }
  540.  
  541. 9. TCP based server program to authenticate the client’s Username and Password.
  542.  
  543. PROGRAM:
  544. Server:
  545. #include (stdio.h)
  546. #include (stdlib.h)
  547. #include (unistd.h)
  548. #include (errno.h)
  549. #include (string.h)
  550. #include (netdb.h)
  551. #include (sys/types.h)
  552. #include (netinet/in.h)
  553. #include (sys/socket.h)
  554. #include (arpa/inet.h)
  555. int main(intargc, char *argv[])
  556. {
  557. int sock_des,port,n1,n2,s1,s2;
  558. char buffer1[100],buffer2[100],buffer3[100],buffer4[100];
  559. intlength,new_des;
  560. structsockaddr_inserver_addr,client_addr;
  561. bzero(buffer3,sizeof(buffer3));
  562. printf(“\n Input Authentic Username :\n”);
  563. scanf(“%s”,buffer3);
  564. bzero(buffer4,sizeof(buffer4));
  565. printf(“\n Input Authentic Password :\n”);
  566. scanf(“%s”,buffer4);
  567. if(argc!=2)
  568. {
  569. printf(“Usage: ./server port\n”);
  570. exit(1);
  571. }
  572. port= atoi(argv[1]);
  573. if((sock_des=socket(AF_INET,SOCK_STREAM,0))==-1)
  574. {
  575. perror(“socket”);
  576. exit(1);
  577. }
  578. server_addr.sin_family = AF_INET;
  579. server_addr.sin_port = htons(port);
  580. server_addr.sin_addr.s_addr= htonl(INADDR_ANY);
  581. if (bind(sock_des,(conststructsockaddr *)&server_addr,sizeof(structsockaddr))==-1)
  582. {
  583. perror(“bind”);
  584. exit(1);
  585. }
  586. if( listen(sock_des,5)==-1)
  587. {
  588. perror(“listen”);
  589. exit(1);
  590. }
  591. printf(“Server is listening at port %d \n”, port);
  592. while(1)
  593. {
  594. if((new_des=accept(sock_des,(structsockaddr *)&client_addr,&length))==-1)
  595. {
  596. perror(“accept”);
  597. exit(1);
  598. }
  599. bzero(buffer1,sizeof(buffer1));
  600. n1=read(new_des,buffer1,sizeof(buffer1));
  601. buffer1[n1]=’null character’;
  602. if((s1=strcmp(buffer1,buffer3))==0)
  603. {
  604. write(new_des,”UsernameMatch!Enter Password”,30);
  605. printf(“Username Match !!!\n”);
  606. bzero(buffer2,sizeof(buffer2));
  607. n2=read(new_des,buffer2,sizeof(buffer2));
  608. buffer2[n2]=’null character’;
  609. if((s2=strcmp(buffer2,buffer4))==0)
  610. {
  611. write(new_des,”Password Match”,15);
  612. printf(“Password Match !!!\n”);
  613. }
  614. else
  615. {
  616. write(new_des,”Password NOT Match”,19);
  617. printf(“Password NOT Match !!!\n”);
  618. }
  619. }
  620. else
  621. {
  622. write(new_des,”Username Not Match”,19);
  623. printf(“Username Not Match !!!\n”);
  624. }
  625. close(new_des);
  626. }
  627. }
  628. Client:
  629. #include (stdio.h)
  630.  
  631. #include (stdlib.h)
  632.  
  633. #include (unistd.h)
  634.  
  635. #include (errno.h)
  636.  
  637. #include (string.h)
  638.  
  639. #include (netdb.h)
  640.  
  641. #include (sys/types.h)
  642.  
  643. #include (netinet/in.h)
  644.  
  645. #include (sys/socket.h)
  646.  
  647. #include (arpa/inet.h)
  648.  
  649. int main(intargc,char *argv[])
  650.  
  651. {
  652.  
  653. intsock_des,port,n;
  654.  
  655. structhostent *host;
  656.  
  657. char buffer1[100],buffer2[100],buffer3[100],buffer4[100];
  658.  
  659. structsockaddr_inserver_addr;
  660.  
  661. if(argc!=3)
  662.  
  663. {
  664.  
  665. printf(“Usage: ./client hostname port\n”);
  666.  
  667. exit(1);
  668.  
  669. }
  670.  
  671. if((host=gethostbyname(argv[1]))==NULL)
  672.  
  673. {
  674.  
  675. printf(“Unknown Host\n”);
  676.  
  677. exit(1);
  678.  
  679. }
  680.  
  681. port = atoi(argv[2]);
  682.  
  683. if((sock_des=socket(AF_INET,SOCK_STREAM,0))==-1)
  684.  
  685. {
  686.  
  687. perror(“socket”);
  688.  
  689. exit(1);
  690.  
  691. }
  692.  
  693. server_addr.sin_family = AF_INET;
  694.  
  695. server_addr.sin_port = htons(port);
  696.  
  697. server_addr.sin_addr = *((structin_addr *)host->h_addr);
  698.  
  699. if(connect(sock_des,(structsockaddr *)&server_addr,sizeof(structsockaddr))==-1)
  700.  
  701. {
  702.  
  703. perror(“connect”);
  704.  
  705. exit(1);
  706.  
  707. }
  708.  
  709. bzero(buffer1,sizeof(buffer1));
  710.  
  711. printf(“Enter the Username :\n”);
  712.  
  713. scanf(“%s”,buffer1);
  714.  
  715. write(sock_des,buffer1,sizeof(buffer1));
  716.  
  717. bzero(buffer4,sizeof(buffer4));
  718.  
  719. n=read(sock_des,buffer4,sizeof(buffer4));
  720.  
  721. buffer4[n]=’null character’;
  722.  
  723. printf(“Server Sent : %s\n”, buffer4);
  724.  
  725. bzero(buffer2,sizeof(buffer2));
  726.  
  727. printf(“Enter the Password :\n”);
  728.  
  729. scanf(“%s”,buffer2);
  730.  
  731. write(sock_des,buffer2,sizeof(buffer2));
  732.  
  733. bzero(buffer3,sizeof(buffer3));
  734.  
  735. n=read(sock_des,buffer3,sizeof(buffer3));
  736.  
  737. buffer3[n]=’null character’;
  738.  
  739. printf(“Server Sent : %s\n”, buffer3);
  740.  
  741. close(sock_des);
  742.  
  743. }
  744. Download as text
  745.  
  746. 10. Implement a message transfer from client to server process using UDP.
  747.  
  748. /************* UDP SERVER CODE *******************/
  749.  
  750. #include <stdio.h>
  751. #include <sys/socket.h>
  752. #include <netinet/in.h>
  753. #include <string.h>
  754. #include <stdlib.h>
  755.  
  756. int main(){
  757. int udpSocket, nBytes;
  758. char buffer[1024];
  759. struct sockaddr_in serverAddr, clientAddr;
  760. struct sockaddr_storage serverStorage;
  761. socklen_t addr_size, client_addr_size;
  762. int i;
  763.  
  764. /*Create UDP socket*/
  765. udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
  766.  
  767. /*Configure settings in address struct*/
  768. serverAddr.sin_family = AF_INET;
  769. serverAddr.sin_port = htons(7891);
  770. serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  771. memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
  772.  
  773. /*Bind socket with address struct*/
  774. bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
  775.  
  776. /*Initialize size variable to be used later on*/
  777. addr_size = sizeof serverStorage;
  778.  
  779. while(1){
  780. /* Try to receive any incoming UDP datagram. Address and port of
  781. requesting client will be stored on serverStorage variable */
  782. nBytes = recvfrom(udpSocket,buffer,1024,0,(struct sockaddr *)&serverStorage, &addr_size);
  783.  
  784. /*Convert message received to uppercase*/
  785. for(i=0;i<nBytes-1;i++)
  786. buffer[i] = toupper(buffer[i]);
  787.  
  788. /*Send uppercase message back to client, using serverStorage as the address*/
  789. sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
  790. }
  791.  
  792. return 0;
  793.  
  794. /************* UDP CLIENT CODE *******************/
  795.  
  796. #include <stdio.h>
  797. #include <sys/socket.h>
  798. #include <netinet/in.h>
  799. #include <string.h>
  800.  
  801. int main(){
  802. int clientSocket, portNum, nBytes;
  803. char buffer[1024];
  804. struct sockaddr_in serverAddr;
  805. socklen_t addr_size;
  806.  
  807. /*Create UDP socket*/
  808. clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
  809.  
  810. /*Configure settings in address struct*/
  811. serverAddr.sin_family = AF_INET;
  812. serverAddr.sin_port = htons(7891);
  813. serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  814. memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
  815.  
  816. /*Initialize size variable to be used later on*/
  817. addr_size = sizeof serverAddr;
  818.  
  819. while(1){
  820. printf("Type a sentence to send to server:\n");
  821. fgets(buffer,1024,stdin);
  822. printf("You typed: %s",buffer);
  823.  
  824. nBytes = strlen(buffer) + 1;
  825.  
  826. /*Send message to server*/
  827. sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);
  828.  
  829. /*Receive message from server*/
  830. nBytes = recvfrom(clientSocket,buffer,1024,0,NULL, NULL);
  831.  
  832. printf("Received from server: %s\n",buffer);
  833.  
  834. }
  835.  
  836. return 0;
  837. }
  838.  
  839.  
  840. 11. To write a program for UDP echo client server.
  841. ALGORITHM:
  842. SERVER:
  843. STEP 1: Start
  844. STEP 2: Declare the variables for the socket
  845. STEP 3: Specify the family, protocol, IP address and port number
  846. STEP 4: Create a socket using socket() function
  847. STEP 5: Bind the IP address and Port number
  848. STEP 6: Listen and accept the client’s request for the connection
  849. STEP 7: Read and Display the client’s message
  850. STEP 8: Stop
  851. CLIENT:
  852. STEP 1: Start
  853. STEP 2: Declare the variables for the socket
  854. STEP 3: Specify the family, protocol, IP address and port number
  855. STEP 4: Create a socket using socket() function
  856. STEP 5: Call the connect() function
  857. STEP 6: Read the input message
  858. STEP 7: Send the input message to the server
  859. STEP 8: Display the server’s echo
  860. STEP 9: Close the socket
  861. STEP 10: Stop
  862.  
  863. SOURCE CODE:
  864. SERVER:
  865. #include<sys/types.h>
  866. #include<sys/socket.h>
  867. #include<netinet/in.h>
  868. #include<unistd.h>
  869. #include<netdb.h>
  870. #include<stdio.h>
  871. #include<string.h>
  872. #include<arpa/inet.h>
  873. #define MAXLINE 1024
  874. int main(int argc,char **argv)
  875. {
  876. int sockfd;
  877. int n;
  878. socklen_t len;
  879. char msg[1024];
  880. struct sockaddr_in servaddr,cliaddr;
  881. sockfd=socket(AF_INET,SOCK_DGRAM,0);
  882. bzero(&servaddr,sizeof(servaddr));
  883. servaddr.sin_family=AF_INET;
  884. servaddr.sin_addr.s_addr=INADDR_ANY;
  885. servaddr.sin_port=htons(5035);
  886. printf("\n\n Binded");
  887. bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
  888. printf("\n\n Listening...");
  889. for(;;)
  890. {
  891. printf("\n ");
  892. len=sizeof(cliaddr);
  893. n=recvfrom(sockfd,msg,MAXLINE,0,(struct sockaddr*)&cliaddr,&len);
  894. printf("\n Client's Message : %s\n",msg);
  895. if(n<6)
  896. perror("send error");
  897. sendto(sockfd,msg,n,0,(struct sockaddr*)&cliaddr,len);
  898. }
  899. return 0;
  900. }
  901.  
  902. CLIENT:
  903. #include<sys/types.h>
  904. #include<sys/socket.h>
  905. #include<netinet/in.h>
  906. #include<string.h>
  907. #include<arpa/inet.h>
  908. #include<string.h>
  909. #include<arpa/inet.h>
  910. #include<stdio.h>
  911. #define MAXLINE 1024
  912. int main(int argc,char* argv[])
  913. {
  914. int sockfd;
  915. int n;
  916. socklen_t len;
  917. char sendline[1024],recvline[1024];
  918. struct sockaddr_in servaddr;
  919. strcpy(sendline,"");
  920. printf("\n Enter the message : ");
  921. scanf("%s",sendline);
  922. sockfd=socket(AF_INET,SOCK_DGRAM,0);
  923. bzero(&servaddr,sizeof(servaddr));
  924. servaddr.sin_family=AF_INET;
  925. servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
  926. servaddr.sin_port=htons(5035);
  927. connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
  928. len=sizeof(servaddr);
  929. sendto(sockfd,sendline,MAXLINE,0,(struct sockaddr*)&servaddr,len);
  930. n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL);
  931. recvline[n]=0;
  932. printf("\n Server's Echo : %s\n\n",recvline);
  933. return 0;
  934. }
Add Comment
Please, Sign In to add comment