Guest User

Untitled

a guest
Nov 6th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.42 KB | None | 0 0
  1. Program-1 : OSI Model Simulation
  2.  
  3. Code :
  4.  
  5. #include<iostream>
  6. #include<string.h>
  7. #include<stdio.h>
  8. using namespace std;
  9. void transmitter( char* str1)
  10. { char a[]="AH",b[]="PH",c[]="SH",d[]="TH",e[]="NH",f[]="DH";
  11. strcat(a,str1);
  12. cout<<"Application Layer : "<<a<<"\n";
  13. strcat(b,a);
  14. cout<<"Presentation Layer : "<<b<<"\n";
  15. strcat(c,b);
  16. cout<<"Session Layer : "<<c<<"\n";
  17. strcat(d,c);
  18. cout<<"Transport Layer : "<<d<<"\n";
  19. strcat(e,d);
  20. cout<<"Network Layer : "<<e<<"\n";
  21. strcat(f,e);
  22. cout<<"Datalink Layer : "<<f<<"\n";
  23. cout<<"\nMESSAGE ENTERED INTO PHYSICAL LAYER AND TRANSMITTED.\n";
  24. cout<<"\nRECIEVER\n\n";
  25. cout<<"MESSAGE ENTERED INTO PHYSICAL LAYER\n\n";
  26. cout<<"Datalink Layer : "<<f<<"\n";
  27. cout<<"Network Layer : ";
  28. for(int i=2;i<strlen(f);i++)
  29. cout<<f[i];
  30. cout<<"\nTransport Layer : ";
  31. for(int i=4;i<strlen(f);i++)
  32. cout<<f[i];
  33. cout<<"\nSession Layer : ";
  34. for(int i=6;i<strlen(f);i++)
  35. cout<<f[i];
  36. cout<<"\nPresentation Layer : ";
  37. for(int i=8;i<strlen(f);i++)
  38. cout<<f[i];
  39. cout<<"\nApplication Layer : ";
  40. for(int i=10;i<strlen(f);i++)
  41. cout<<f[i];
  42. }
  43. main()
  44. { char str[10],add[10];
  45. cout<<"Input String : ";
  46. cin>>str;
  47. cout<<"Input Address : ";
  48. cin>>add;
  49. cout<<"\nTRANSMITTER\n\n";
  50. transmitter(str);
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. PROGRAM-2 : Data link layer framing methods
  61.  
  62. 1. Character count
  63. 2. Character stuffing
  64. 3. Bit stuffing
  65.  
  66. 1. Character Count :
  67.  
  68.  
  69. Code:
  70.  
  71. #include <stdio.h>
  72. #include <conio.h>
  73. #include <string.h>
  74. void main(){
  75. int size;
  76. int total=0;
  77. int i=1;
  78. int j,k=0;
  79. int fsize;
  80. printf("Enter the no. of characters: ");
  81. scanf("%d",&size);
  82. char str[size];
  83. printf("\nEnter the characters: ");
  84. scanf("%s",&str);
  85. while(k<size)
  86. {
  87. printf("Enter the frame %d size: ",i);
  88. scanf("%d",&fsize);
  89. char frame[fsize+1];
  90. i++;
  91. total+=fsize;
  92. frame[0]=(char)(fsize+48);
  93. for(j=1;j<fsize;j++)
  94. {
  95. if(k<size)
  96. {
  97. printf("%c\n",str[k]);
  98. frame[j]=str[k];
  99. k++;
  100. }
  101. }
  102. frame[fsize]='\0';
  103. printf("%s\n",frame);
  104. }
  105. getch();
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. 2. Character Stuffing
  116.  
  117. Code:
  118.  
  119. #include<stdio.h>
  120. #include<conio.h>
  121. #include<string.h>
  122. void charc(void);
  123. void main()
  124. {i
  125. nt choice;
  126. while(1)
  127. {p
  128. rintf("\n\n\n1.character stuffing");
  129. printf("\n\n2.exit");
  130. printf("\n\n\nenter choice");
  131. scanf("%d",&choice);
  132. printf("%d",choice);
  133. if(choice>2)
  134. printf("\n\n invalid option....please renter");
  135. switch(choice)
  136. {
  137. case 1:
  138. charc();
  139. break;
  140. case 2:
  141. exit(0);
  142. }}}
  143. void charc(void)
  144. { char c[50],d[50],t[50];
  145. int i,m,j;
  146. clrscr();
  147. printf("enter the number of characters\n");
  148. scanf("%d",&m);
  149. printf("\n enter the characters\n");
  150. for(i=0;i<m+1;i++)
  151. {
  152. scanf("%c",&c[i]);
  153. }p
  154. rintf("\n original data\n");
  155. for(i=0;i<m+1;i++)
  156. printf("%c",c[i]);
  157. d[0]='d';
  158. d[1]='l';
  159. d[2]='e';
  160. d[3]='s';
  161. d[4]='t';
  162. d[5]='x';
  163. for(i=0,j=6;i<m+1;i++,j++)
  164. {
  165. if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
  166. {d[j]='d';
  167. j++;
  168. d[j]='l';
  169. j++;
  170. d[j]='e';
  171. j++;
  172. m=m+3;
  173. }d
  174. [j]=c[i];
  175. }
  176. m=m+6;
  177. m++;
  178. d[m]='d';
  179. m++;
  180. d[m]='l';
  181. m++;
  182. d[m]='e';
  183. m++;
  184. d[m]='e';
  185. m++;
  186. d[m]='t';
  187. m++;
  188. d[m]='x';
  189. m++;
  190. printf("\n\n transmitted data: \n");
  191. for(i=0;i<m;i++)
  192. {
  193. printf("%c",d[i]);
  194. }f
  195. or(i=6,j=0;i<m-6;i++,j++)
  196. {
  197. if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e
  198. ')
  199. i=i+3;
  200. t[j]=d[i];
  201. }p
  202. rintf("\n\nreceived data:");
  203. for(i=0;i<j;i++)
  204. {printf("%c",t[i]);
  205. }}
  206.  
  207. Output :
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. 3.Bit Stuffing
  217.  
  218. Code:
  219.  
  220. #include<stdio.h>
  221. #include<conio.h>
  222. #include<string.h>
  223. void main() {
  224. int i, j,count=0,nl;
  225. char str[100];
  226. clrscr();
  227. printf("enter the bit string: ");
  228. gets(str);
  229. for (i=0;i<strlen(str);i++) {
  230. count=0;
  231. for (j=i;j<=(i+5);j++) {
  232. if(str[j]=='1') {
  233. count++;
  234. }
  235. }
  236. if(count==6) {
  237. nl=strlen(str)+2;
  238. for (;nl>=(i+5);nl--) {
  239. str[nl]=str[nl-1];
  240. }
  241. str[i+5]='0';
  242. i=i+7;
  243. }}
  244. puts(str);
  245. getch();
  246. }
  247. Output:
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256. PROGRAM-3 : To Calculate the Checksum
  257.  
  258. Code:
  259.  
  260. #include<stdio.h>
  261. #include<conio.h>
  262. #include<math.h>
  263. int sender(int b[10],int k)
  264. {
  265. intchecksum,sum=0,i;
  266. printf("\n****SENDER****\n");
  267.  
  268. for(i=0;i<k;i++)
  269. sum+=b[i];
  270. printf("SUM IS: %d",sum);
  271.  
  272. checksum=~sum;
  273. printf("\nSENDER's CHECKSUM IS:%d",checksum);
  274. return checksum;
  275. }
  276.  
  277. int receiver(int c[10],intk,intscheck)
  278. {
  279. intchecksum,sum=0,i;
  280. printf("\n\n****RECEIVER****\n");
  281. for(i=0;i<k;i++)
  282. sum+=c[i];
  283. printf(" RECEIVER SUM IS:%d",sum);
  284. sum=sum+scheck;
  285. checksum=~sum;
  286. printf("\nRECEIVER's CHECKSUM IS:%d",checksum);
  287. return checksum;
  288. }
  289. main()
  290. {
  291. int a[10],i,m,scheck,rcheck;
  292. clrscr();
  293. printf("\nENTER SIZE OF THE STRING:");
  294. scanf("%d",&m);
  295. printf("\nENTER THE ELEMENTS OF THE ARRAY:");
  296. for(i=0;i<m;i++)
  297. scanf("%d",&a[i]);
  298. scheck=sender(a,m);
  299. rcheck=receiver(a,m,scheck);
  300. if(rcheck==0)
  301. printf("\n\nNO ERROR IN TRANSMISSION\n\n");
  302. else
  303. printf("\n\nERROR DETECTED");
  304. getch();
  305. }
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313. Program-4: Generation and checking of CRC
  314.  
  315. Code:
  316. #include<stdio.h>
  317. #include<conio.h>
  318. #define N strlen(g)
  319.  
  320. char t[128], cs[128], g[]="100010000";
  321. int a, e, c;
  322.  
  323. void xor() {
  324. for(c=1;c}
  325.  
  326. void crc() {
  327. for(e=0;e do {
  328. if(cs[0]=='1') xor();
  329. for(c=0;c cs[c]=t[e++];
  330. }while(e<=a+N-1);
  331. }
  332.  
  333. void main() {
  334. clrscr();
  335. printf("\nEnter poly : "); scanf("%s",t);
  336. printf("\nGenerating Polynomial is : %s",g);
  337. a=strlen(t);
  338. for(e=a;e
  339. printf("\nModified t[u] is : %s",t);
  340. crc();
  341. printf("\nChecksum is : %s",cs);
  342. for(e=a;e printf("\nFinalCodeword is : %s",t);
  343. printf("\nTest Error detection 0(yes) 1(no) ? : ");
  344. scanf("%d",&e);
  345. if(e==0) {
  346. printf("Enter position where error is to inserted : ");
  347. scanf("%d",&e);
  348. t[e]=(t[e]=='0')?'1':'0';
  349. printf("Errorneous data : %s\n",t);
  350. }
  351. crc();
  352. for (e=0;(e<n-1)&&(cs[e]!='1');e++);
  353. if(e
  354. else printf("No Error Detected.");
  355. getch();
  356. }
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364. Program-5: Generation of hamming code
  365.  
  366. Code:
  367. #include<stdio.h>
  368. #include<conio.h>
  369. void main() {
  370. int data[7],rec[7],i,c1,c2,c3,c;
  371. printf("this works for message of 4bits in size \nenter message bit one by one: ");
  372. scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);
  373. data[6]=data[0]^data[2]^data[4];
  374. data[5]=data[0]^data[1]^data[4];
  375. data[3]=data[0]^data[1]^data[2];
  376. printf("\nthe encoded bits are given below: \n");
  377. for (i=0;i<7;i++) {
  378. printf("%d ",data[i]);
  379. }
  380. printf("\nenter the received data bits one by one: ");
  381. for (i=0;i<7;i++) {
  382. scanf("%d",&rec[i]);
  383. }
  384. c1=rec[6]^rec[4]^rec[2]^rec[0];
  385. c2=rec[5]^rec[4]^rec[1]^rec[0];
  386. c3=rec[3]^rec[2]^rec[1]^rec[0];
  387. c=c3*4+c2*2+c1 ;
  388. if(c==0) {
  389. printf("\ncongratulations there is no error: ");
  390. } else {
  391. printf("\nerron on the postion: %d\nthe correct message is \n",c);
  392. if(rec[7-c]==0)
  393. rec[7-c]=1; else
  394. rec[7-c]=0;
  395. for (i=0;i<7;i++) {
  396. printf("%d ",rec[i]);
  397. }
  398. }
  399. getch();
  400. }
  401.  
  402. Output:
  403.  
  404.  
  405.  
  406.  
  407. Program 6: Sliding Window Protocol
  408.  
  409.  
  410.  
  411.  
  412. Code:
  413.  
  414. #include<stdio.h>
  415. #include<stdlib.h>
  416. void main()
  417. {
  418. char sender[50],receiver[50];
  419. inti,winsize;
  420. printf("\n ENTER THE WINDOWS SIZE : ");
  421. scanf("%d",&winsize);
  422. printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n");
  423. printf("\n ENTER THE DATA TO BE SENT: ");
  424.  
  425. scanf("%s",sender);
  426. for(i=0;i<winsize;i++)
  427. receiver[i]=sender[i];
  428. receiver[i]='\0';
  429. printf("\n MESSAGE SEND BY THE SENDER:\n");
  430. printf("%s",sender);
  431. printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
  432. printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
  433. for(i=0;i<winsize;i++);
  434. printf("\n ACK:%d",i);
  435. printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
  436. printf("%s",receiver);
  437. printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
  438. }
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451. Program 7: Chat Application using TCP/IP
  452.  
  453.  
  454.  
  455. Program:
  456.  
  457. // Server Program - Communicating to single client multiple number of times
  458. #include<stdio.h>
  459. #include<netinet/in.h>
  460. #include<netdb.h>
  461. #include<string.h>
  462. #define SERV_TCP_PORT 5035
  463. int main(intargc,char**argv)
  464. {
  465. intsockfd,newsockfd,clength;
  466. structsockaddr_inserv_addr,cli_addr;
  467. char buffer[4096];
  468. //creating a socket
  469. sockfd=socket(AF_INET,SOCK_STREAM,0);
  470. serv_addr.sin_family=AF_INET;
  471. serv_addr.sin_addr.s_addr=INADDR_ANY;
  472. serv_addr.sin_port=htons(SERV_TCP_PORT);
  473. printf("\nStart");
  474. //bind the socket to port address and any local interface
  475. bind(sockfd,(structsockaddr*)&serv_addr,sizeof(serv_addr));
  476. printf("\nListening...");
  477. printf("\n");
  478. listen(sockfd,5);
  479. clength=sizeof(cli_addr);
  480. newsockfd=accept(sockfd,(structsockaddr*)&cli_addr,&clength);
  481. //newsockfd=accept(sockfd,(structsockaddr*)NULL,NULL);.
  482. printf("\nAccepted");
  483. printf("\n");
  484. while(1)
  485. {
  486. bzero(buffer,4096);
  487. read(newsockfd,buffer,4096);
  488. printf("\nClient message:%s",buffer);
  489. write(newsockfd,buffer,strlen(buffer)+1);
  490. printf("\n");
  491. }
  492. close(sockfd);
  493. return 0;
  494. }
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508. Client Side:
  509. Client Program - Multiple communications to server
  510. #include<stdio.h>
  511. #include<sys/types.h>
  512. #include<sys/socket.h>
  513. #include<netinet/in.h>
  514. #include<netdb.h>
  515. #define SERV_TCP_PORT 5035
  516. int main(intargc,char*argv[])
  517. {
  518. intsockfd;
  519. structsockaddr_inserv_addr;
  520. structhostent *server;
  521. char buffer[4096];
  522. int k=5,cnt=0;
  523. sockfd=socket(AF_INET,SOCK_STREAM,0);
  524. serv_addr.sin_family=AF_INET;
  525. serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
  526. serv_addr.sin_port=htons(SERV_TCP_PORT);
  527. printf("\nReady for sending...");
  528. connect(sockfd,(structsockaddr*)&serv_addr,sizeof(serv_addr));
  529. while(cnt<= k)
  530. {
  531. printf("\nEnter the message to send\n");
  532. printf("\nClient: ");
  533. fgets(buffer,4096,stdin);
  534. write(sockfd,buffer,4096);
  535. printf("Serverecho:%s",buffer);
  536. printf("\n");
  537. cnt++;
  538. }
  539. close(sockfd);
  540. return 0;
  541. }
  542.  
  543. Screenshot:
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552. 8.TCP Client/server application for transferring a text file from client to server.
  553.  
  554. Fileser.c
  555.  
  556. #include<sys/socket.h>
  557. #include<netinet/in.h>
  558. #include<arpa/inet.h>
  559. #include<stdio.h>
  560. #include<stdlib.h>
  561. #include<unistd.h>
  562. #include<errno.h>
  563. #include<string.h>
  564. #include<sys/types.h>
  565. int main()
  566. {
  567. int listenfd=0;int connfd=0;
  568. struct sockaddr_in serv_addr;
  569. char sendbuff[1025];
  570. int numrv;
  571. listenfd=socket(AF_INET,SOCK_STREAM,0);
  572. printf("socket retrieve success\n");
  573. memset(&serv_addr,'0',sizeof(serv_addr));
  574. memset(sendbuff,'0',sizeof(sendbuff));
  575. serv_addr.sin_family=AF_INET;
  576. serv_addr.sin_addr.s_addr=htons(INADDR_ANY);
  577. serv_addr.sin_port=htons(5000);
  578. bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
  579. if(listen(listenfd,10)==-1){
  580. printf("failed to listen\n");
  581. return -1;
  582. }
  583. while(1)
  584. {
  585. connfd=accept(listenfd,(struct sockaddr*)NULL,NULL);
  586.  
  587. FILE *fp=fopen("sample_file.txt","rb");
  588. if(fp==NULL)
  589. {
  590. printf("file open error");
  591. return 1;
  592. }
  593. while(1)
  594. {
  595. unsigned char buff[256]={0};
  596. int nread=fread(buff,1,256,fp);
  597. printf("bytes read %d \n",nread);
  598. if(nread>0)
  599. {
  600. printf("sending\n");
  601. write(connfd,buff,nread);
  602. }
  603. if(nread<256)
  604. {
  605. if(feof(fp))
  606. printf("end of file\n");
  607. if(ferror(fp))
  608. printf("error reading");
  609. break;
  610. }
  611. }}
  612. close(connfd);
  613. sleep(1);
  614. }
  615.  
  616.  
  617. fclient.c
  618.  
  619. #include<sys/socket.h>
  620. #include<netinet/in.h>
  621. #include<arpa/inet.h>
  622. #include<stdio.h>
  623. #include<stdlib.h>
  624. #include<unistd.h>
  625. #include<errno.h>
  626. #include<string.h>
  627. #include<sys/types.h>
  628. int main(){
  629. int sockfd=0;
  630. int bytesreceived=0;
  631. char recvbuff[256];
  632. memset(recvbuff,'0',sizeof(recvbuff));
  633. struct sockaddr_in serv_addr;
  634. if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
  635. {
  636. printf("\nerror:could not create socket\n");
  637. return 1;
  638. }
  639. serv_addr.sin_family=AF_INET;
  640. serv_addr.sin_port=htons(5000);
  641. serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
  642. if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
  643. {
  644. printf("\nerror:connect failed\n");
  645. return 1;
  646. }
  647. FILE *fp;
  648. fp=fopen("sample_file.txt","ab");
  649. if(fp==NULL)
  650. {
  651. printf("error opening file");
  652. return 1;
  653. }
  654. while((bytesreceived=read(sockfd,recvbuff,256))>0)
  655. {
  656. printf("bytes received %d \n",bytesreceived);
  657. recvbuff[bytesreceived]=0;
  658. fwrite(recvbuff,1,bytesreceived,fp);
  659. printf("%s\n",recvbuff);
  660. }
  661. if(bytesreceived<0)
  662. {
  663. printf("\nread error");
  664. }
  665. return 0;
  666. }
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675. 9. TCP based server program in C to authenticate the client’s Username and Password
  676.  
  677. SERVER :
  678. #include <sys/types.h>
  679. #include <sys/socket.h>
  680. #include <unistd.h>
  681. #include <stdio.h>
  682. #include <string.h>
  683. #include <netinet/in.h>
  684. #include <arpa/inet.h>
  685. #include <errno.h>
  686. #define ANSI_COLOR_GREEN "\x1b[32m"
  687. #define ANSI_COLOR_RESET "\x1b[0m"
  688. struct user {
  689. char *username;
  690. char *password;
  691. };
  692. int main() {
  693. /*login details*/
  694. struct user singleUser;
  695. singleUser.username = "omijn";
  696. singleUser.password = "qwerty";
  697. /*messages*/
  698. char * userOK_enterPass = "Please enter your password : ";
  699. char * userNotFound = "This username does not exist.";
  700. char * userAuthenticated = "User authenticated!";
  701. char * invalidPassword = "Invalid password.";
  702. /*errors*/
  703. int bindError = 0;
  704. int serverSocketFD = 0, clientFD = 0, n = 0, auth = 0;
  705. char receiveBuffer[1024] = "", sendBuffer[1024] = "";
  706. struct sockaddr_in serverAddress;
  707. system("clear");
  708. //create socket
  709. if((serverSocketFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  710. printf("> Socket creation failed.\n");
  711. else
  712. printf("> Socket created.\n");
  713. //clear memory
  714. memset(receiveBuffer, '0', sizeof(receiveBuffer));
  715. memset(sendBuffer, '0', sizeof(sendBuffer));
  716. memset(&serverAddress, '0', sizeof(serverAddress));
  717. //server address
  718. serverAddress.sin_family = AF_INET;
  719. serverAddress.sin_port = htons(2884);
  720. serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
  721. printf("> Address created.\n");
  722. //assign name to server
  723. if((bind(serverSocketFD, (struct sockaddr*)&serverAddress,
  724. sizeof(serverAddress))) == -1) {
  725. printf("> Bind error.\n");
  726. bindError = errno;
  727. printf("%s\n", strerror(bindError));
  728. return -1;
  729. }
  730. else
  731. printf("> Binding successful.\n");
  732. //listen
  733. if((listen(serverSocketFD, 5)) == -1)
  734. printf("> Listen error.\n");
  735. else
  736. printf("> Server listening.\n");
  737. //accept connection
  738. if((clientFD = accept(serverSocketFD, (struct sockaddr *)NULL,
  739. NULL)) != 0)
  740. printf(ANSI_COLOR_GREEN "> Client connected!\n"
  741. ANSI_COLOR_RESET);
  742. else
  743. printf("> Client connect error.\n");
  744. while(!auth) {
  745. /* receive
  746. -------------------------------------------------------------------
  747. */
  748. while((n = read(clientFD, receiveBuffer,
  749. sizeof(receiveBuffer) - 1)) > 0) {
  750. if(n == -1)
  751. printf("> Read error.\n");
  752. receiveBuffer[n] = 0;
  753. break;
  754. }
  755. if(strcmp(receiveBuffer, singleUser.username) == 0) {
  756. write(clientFD, userOK_enterPass, strlen(userOK_enterPass));
  757. auth = 0;
  758. while(!auth) {
  759. while((n = read(clientFD, receiveBuffer, sizeof(receiveBuffer) - 1))
  760. > 0) {
  761. if(n == -1)
  762. printf("> Read error.\n");
  763. receiveBuffer[n] = 0;
  764. break;
  765. }
  766. if(strcmp(receiveBuffer, singleUser.password) == 0) {
  767. /*user authenticated*/
  768. auth = 1;
  769. printf("\n%s", userAuthenticated);
  770. write(clientFD, userAuthenticated,
  771. strlen(userAuthenticated));
  772. }
  773. else {
  774. auth = 0;
  775. printf("\n%s", invalidPassword);
  776. write(clientFD, invalidPassword,
  777. strlen(invalidPassword));
  778. }
  779. }
  780. }
  781. else {
  782. printf("\n%s", userNotFound);
  783. write(clientFD, userNotFound, strlen(userNotFound));
  784. }
  785. printf(ANSI_COLOR_GREEN "\n%s" ANSI_COLOR_RESET,
  786. receiveBuffer);
  787. //clear receive buffer for next use
  788. strcpy(receiveBuffer, "");
  789. //clear send buffer for next use
  790. strcpy(sendBuffer, "");
  791. }
  792. printf("\n");
  793. return 0;
  794. }
  795.  
  796.  
  797. CLIENT :
  798.  
  799. #include <sys/types.h>
  800. #include <sys/socket.h>
  801. #include <unistd.h>
  802. #include <stdio.h>
  803. #include <string.h>
  804. #include <netinet/in.h>
  805. #include <arpa/inet.h>
  806. #include <errno.h>
  807. #define ANSI_COLOR_GREEN "\x1b[32m"
  808. #define ANSI_COLOR_RESET "\x1b[0m"
  809. struct user {
  810. char *username;
  811. char *password;
  812. };
  813. int main() {
  814. /*login details*/
  815. struct user singleUser;
  816. singleUser.username = "omijn";
  817. singleUser.password = "qwerty";
  818. /*messages*/
  819. char * userOK_enterPass = "Please enter your password : ";
  820. char * userNotFound = "This username does not exist.";
  821. char * userAuthenticated = "User authenticated!";
  822. char * invalidPassword = "Invalid password.";
  823. /*errors*/
  824. int bindError = 0;
  825. int serverSocketFD = 0, clientFD = 0, n = 0, auth = 0;
  826. char receiveBuffer[1024] = "", sendBuffer[1024] = "";
  827. struct sockaddr_in serverAddress;
  828. system("clear");
  829. //create socket
  830. if((serverSocketFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  831. printf("> Socket creation failed.\n");
  832. else
  833. printf("> Socket created.\n");
  834. //clear memory
  835. memset(receiveBuffer, '0', sizeof(receiveBuffer));
  836. memset(sendBuffer, '0', sizeof(sendBuffer));
  837. memset(&serverAddress, '0', sizeof(serverAddress));
  838. //server address
  839. serverAddress.sin_family = AF_INET;
  840. serverAddress.sin_port = htons(2884);
  841. serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
  842. printf("> Address created.\n");
  843. //assign name to server
  844. if((bind(serverSocketFD, (struct sockaddr*)&serverAddress,
  845. sizeof(serverAddress))) == -1) {
  846. printf("> Bind error.\n");
  847. bindError = errno;
  848. printf("%s\n", strerror(bindError));
  849. return -1;
  850. }
  851. else
  852. printf("> Binding successful.\n");
  853. //listen
  854. if((listen(serverSocketFD, 5)) == -1)
  855. printf("> Listen error.\n");
  856. else
  857. printf("> Server listening.\n");
  858. //accept connection
  859. if((clientFD = accept(serverSocketFD, (struct sockaddr *)NULL,
  860. NULL)) != 0)
  861. printf(ANSI_COLOR_GREEN "> Client connected!\n"
  862. ANSI_COLOR_RESET);
  863. Else
  864. printf("> Client connect error.\n");
  865. while(!auth) {
  866. /* receive
  867. -------------------------------------------------------------------
  868. */
  869. while((n = read(clientFD, receiveBuffer,
  870. sizeof(receiveBuffer) - 1)) > 0) {
  871. if(n == -1)
  872. printf("> Read error.\n");
  873. receiveBuffer[n] = 0;
  874. break;
  875. }
  876. if(strcmp(receiveBuffer, singleUser.username) == 0) {
  877. write(clientFD, userOK_enterPass, strlen(userOK_enterPass));
  878. auth = 0;
  879. while(!auth) {
  880. while((n = read(clientFD, receiveBuffer, sizeof(receiveBuffer) - 1)) > 0) {
  881. if(n == -1)
  882. printf("> Read error.\n");
  883. receiveBuffer[n] = 0;
  884. break;
  885. }
  886. if(strcmp(receiveBuffer, singleUser.password) == 0) {
  887. /*user authenticated*/
  888. auth = 1;
  889. printf("\n%s", userAuthenticated);
  890. write(clientFD, userAuthenticated,
  891. strlen(userAuthenticated));
  892. }
  893. else {
  894. auth = 0;
  895. printf("\n%s", invalidPassword);
  896. write(clientFD, invalidPassword,
  897. strlen(invalidPassword));
  898. }
  899. }
  900. }
  901. else {
  902. printf("\n%s", userNotFound);
  903. write(clientFD, userNotFound, strlen(userNotFound));
  904. }
  905. printf(ANSI_COLOR_GREEN "\n%s" ANSI_COLOR_RESET,
  906. receiveBuffer);
  907. //clear receive buffer for next use
  908. strcpy(receiveBuffer, "");
  909. //clear send buffer for next use
  910. strcpy(sendBuffer, "");
  911. }
  912. printf("\n");
  913. return 0;
  914. }
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925. Ques 10:--- Implement a message transfer from client to server process using UDP
  926.  
  927. Code:--
  928.  
  929. Server:-
  930.  
  931.  
  932. #include <stdio.h>
  933. #include <sys/socket.h>
  934. #include <netinet/in.h>
  935. #include <string.h>
  936. #include <stdlib.h>
  937.  
  938. int main(){
  939. int udpSocket, nBytes;
  940. char buffer[1024];
  941. struct sockaddr_in serverAddr, clientAddr;
  942. struct sockaddr_storage serverStorage;
  943. socklen_t addr_size, client_addr_size;
  944. int i;
  945.  
  946. /*Create UDP socket*/
  947. udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
  948.  
  949. /*Configure settings in address struct*/
  950. serverAddr.sin_family = AF_INET;
  951. serverAddr.sin_port = htons(7891);
  952. serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  953. memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
  954.  
  955. /*Bind socket with address struct*/
  956. bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
  957.  
  958. /*Initialize size variable to be used later on*/
  959. addr_size = sizeof serverStorage;
  960.  
  961. while(1){
  962. /* Try to receive any incoming UDP datagram. Address and port of
  963. requesting client will be stored on serverStorage variable */
  964. nBytes = recvfrom(udpSocket,buffer,1024,0,(struct sockaddr *)&serverStorage, &addr_size);
  965.  
  966. /*Convert message received to uppercase*/
  967. for(i=0;i<nBytes-1;i++)
  968. buffer[i] = toupper(buffer[i]);
  969.  
  970. /*Send uppercase message back to client, using serverStorage as the address*/
  971. sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
  972. }
  973.  
  974. return 0;
  975. }
  976.  
  977.  
  978.  
  979. Output
  980.  
  981. Server
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997. Code:-
  998.  
  999. Client :-
  1000.  
  1001. #include <stdio.h>
  1002. #include <sys/socket.h>
  1003. #include <netinet/in.h>
  1004. #include <string.h>
  1005.  
  1006. int main(){
  1007. int clientSocket, portNum, nBytes;
  1008. char buffer[1024];
  1009. struct sockaddr_in serverAddr;
  1010. socklen_t addr_size;
  1011.  
  1012. /*Create UDP socket*/
  1013. clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
  1014.  
  1015. /*Configure settings in address struct*/
  1016. serverAddr.sin_family = AF_INET;
  1017. serverAddr.sin_port = htons(7891);
  1018. serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  1019. memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
  1020.  
  1021. /*Initialize size variable to be used later on*/
  1022. addr_size = sizeof serverAddr;
  1023.  
  1024. while(1){
  1025. printf("Type a sentence to send to server:\n");
  1026. fgets(buffer,1024,stdin);
  1027. printf("You typed: %s",buffer);
  1028.  
  1029. nBytes = strlen(buffer) + 1;
  1030.  
  1031. /*Send message to server*/
  1032. sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);
  1033.  
  1034. /*Receive message from server*/
  1035. nBytes = recvfrom(clientSocket,buffer,1024,0,NULL, NULL);
  1036.  
  1037. printf("Received from server: %s\n",buffer);
  1038.  
  1039. }
  1040.  
  1041. return 0;
  1042. }
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049. Output
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055. AIM: To write a program for UDP echo client server.
  1056. CODE :
  1057. SERVER:
  1058. #include<sys/types.h>
  1059. #include<sys/socket.h>
  1060. #include<netinet/in.h>
  1061. #include<unistd.h>
  1062. #include<netdb.h>
  1063. #include<stdio.h>
  1064. #include<string.h>
  1065. #include<arpa/inet.h>
  1066. #define MAXLINE 1024
  1067. int main(int argc,char **argv)
  1068. {
  1069. int sockfd;
  1070. int n;
  1071. socklen_t len;
  1072. char msg[1024];
  1073. struct sockaddr_in servaddr,cliaddr;
  1074. sockfd=socket(AF_INET,SOCK_DGRAM,0);
  1075. bzero(&servaddr,sizeof(servaddr));
  1076. servaddr.sin_family=AF_INET;
  1077. servaddr.sin_addr.s_addr=INADDR_ANY;
  1078. servaddr.sin_port=htons(5035);
  1079. printf("\n\n Binded");
  1080. bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
  1081. printf("\n\n Listening...");
  1082. for(;;)
  1083. {
  1084. printf("\n ");
  1085. len=sizeof(cliaddr);
  1086. n=recvfrom(sockfd,msg,MAXLINE,0,(struct
  1087. sockaddr*)&cliaddr,&len);
  1088. printf("\n Client's Message : %s\n",msg);
  1089. if(n<6)
  1090. perror("send error");
  1091. sendto(sockfd,msg,n,0,(struct sockaddr*)&cliaddr,len);
  1092. }
  1093. return 0;
  1094. }
  1095. CLIENT:
  1096. #include<sys/types.h>
  1097. #include<sys/socket.h>
  1098. #include<netinet/in.h>
  1099. #include<string.h>
  1100. #include<arpa/inet.h>
  1101. #include<string.h>
  1102. #include<arpa/inet.h>
  1103. #include<stdio.h>
  1104. #define MAXLINE 1024
  1105. int main(int argc,char* argv[])
  1106. {
  1107. int sockfd;
  1108. int n;
  1109. socklen_t len;
  1110. char sendline[1024],recvline[1024];
  1111. struct sockaddr_in servaddr;
  1112. strcpy(sendline,"");
  1113. printf("\n Enter the message : ");
  1114. scanf("%s",sendline);
  1115. sockfd=socket(AF_INET,SOCK_DGRAM,0);
  1116. bzero(&servaddr,sizeof(servaddr));
  1117. servaddr.sin_family=AF_INET;
  1118. servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
  1119. servaddr.sin_port=htons(5035);
  1120. connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
  1121. len=sizeof(servaddr);
  1122. sendto(sockfd,sendline,MAXLINE,0,(struct sockaddr*)&servaddr,len);
  1123. n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL);
  1124. recvline[n]=0;
  1125. printf("\n Server's Echo : %s\n\n",recvline);
  1126. return 0;
  1127. }
  1128. OUTPUT
Add Comment
Please, Sign In to add comment