Guest User

Untitled

a guest
Oct 17th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.94 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. 1. Character Count :
  56.  
  57.  
  58. Code:
  59.  
  60. #include <stdio.h>
  61. #include <conio.h>
  62. #include <string.h>
  63. void main(){
  64. int size;
  65. int total=0;
  66. int i=1;
  67. int j,k=0;
  68. int fsize;
  69. printf("Enter the no. of characters: ");
  70. scanf("%d",&size);
  71. char str[size];
  72. printf("\nEnter the characters: ");
  73. scanf("%s",&str);
  74. while(k<size)
  75. {
  76. printf("Enter the frame %d size: ",i);
  77. scanf("%d",&fsize);
  78. char frame[fsize+1];
  79. i++;
  80. total+=fsize;
  81. frame[0]=(char)(fsize+48);
  82. for(j=1;j<fsize;j++)
  83. {
  84. if(k<size)
  85. {
  86. printf("%c\n",str[k]);
  87. frame[j]=str[k];
  88. k++;
  89. }
  90. }
  91. frame[fsize]='\0';
  92. printf("%s\n",frame);
  93. }
  94. getch();
  95. }
  96. Output :
  97.  
  98.  
  99.  
  100.  
  101. 2. Character Stuffing
  102.  
  103. Code:
  104.  
  105. #include<stdio.h>
  106. #include<conio.h>
  107. #include<string.h>
  108. void charc(void);
  109. void main()
  110. {i
  111. nt choice;
  112. while(1)
  113. {p
  114. rintf("\n\n\n1.character stuffing");
  115. printf("\n\n2.exit");
  116. printf("\n\n\nenter choice");
  117. scanf("%d",&choice);
  118. printf("%d",choice);
  119. if(choice>2)
  120. printf("\n\n invalid option....please renter");
  121. switch(choice)
  122. {
  123. case 1:
  124. charc();
  125. break;
  126. case 2:
  127. exit(0);
  128. }}}
  129. void charc(void)
  130. { char c[50],d[50],t[50];
  131. int i,m,j;
  132. clrscr();
  133. printf("enter the number of characters\n");
  134. scanf("%d",&m);
  135. printf("\n enter the characters\n");
  136. for(i=0;i<m+1;i++)
  137. {
  138. scanf("%c",&c[i]);
  139. }p
  140. rintf("\n original data\n");
  141. for(i=0;i<m+1;i++)
  142. printf("%c",c[i]);
  143. d[0]='d';
  144. d[1]='l';
  145. d[2]='e';
  146. d[3]='s';
  147. d[4]='t';
  148. d[5]='x';
  149. for(i=0,j=6;i<m+1;i++,j++)
  150. {
  151. if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
  152. {d[j]='d';
  153. j++;
  154. d[j]='l';
  155. j++;
  156. d[j]='e';
  157. j++;
  158. m=m+3;
  159. }d
  160. [j]=c[i];
  161. }
  162. m=m+6;
  163. m++;
  164. d[m]='d';
  165. m++;
  166. d[m]='l';
  167. m++;
  168. d[m]='e';
  169. m++;
  170. d[m]='e';
  171. m++;
  172. d[m]='t';
  173. m++;
  174. d[m]='x';
  175. m++;
  176. printf("\n\n transmitted data: \n");
  177. for(i=0;i<m;i++)
  178. {
  179. printf("%c",d[i]);
  180. }f
  181. or(i=6,j=0;i<m-6;i++,j++)
  182. {
  183. if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e
  184. ')
  185. i=i+3;
  186. t[j]=d[i];
  187. }p
  188. rintf("\n\nreceived data:");
  189. for(i=0;i<j;i++)
  190. {printf("%c",t[i]);
  191. }}
  192.  
  193. Output :
  194.  
  195.  
  196.  
  197. 3.Bit Stuffing
  198.  
  199. Code:
  200.  
  201. #include<stdio.h>
  202. #include<conio.h>
  203. #include<string.h>
  204. void main() {
  205. int i, j,count=0,nl;
  206. char str[100];
  207. clrscr();
  208. printf("enter the bit string: ");
  209. gets(str);
  210. for (i=0;i<strlen(str);i++) {
  211. count=0;
  212. for (j=i;j<=(i+5);j++) {
  213. if(str[j]=='1') {
  214. count++;
  215. }
  216. }
  217. if(count==6) {
  218. nl=strlen(str)+2;
  219. for (;nl>=(i+5);nl--) {
  220. str[nl]=str[nl-1];
  221. }
  222. str[i+5]='0';
  223. i=i+7;
  224. }}
  225. puts(str);
  226. getch();
  227. }
  228. Output:
  229.  
  230. PROGRAM-3 : To Calculate the Checksum
  231.  
  232. Code:
  233.  
  234. #include<stdio.h>
  235. #include<conio.h>
  236. #include<math.h>
  237. int sender(int b[10],int k)
  238. {
  239. intchecksum,sum=0,i;
  240. printf("\n****SENDER****\n");
  241.  
  242. for(i=0;i<k;i++)
  243. sum+=b[i];
  244. printf("SUM IS: %d",sum);
  245.  
  246. checksum=~sum;
  247. printf("\nSENDER's CHECKSUM IS:%d",checksum);
  248. return checksum;
  249. }
  250.  
  251. int receiver(int c[10],intk,intscheck)
  252. {
  253. intchecksum,sum=0,i;
  254. printf("\n\n****RECEIVER****\n");
  255. for(i=0;i<k;i++)
  256. sum+=c[i];
  257. printf(" RECEIVER SUM IS:%d",sum);
  258. sum=sum+scheck;
  259. checksum=~sum;
  260. printf("\nRECEIVER's CHECKSUM IS:%d",checksum);
  261. return checksum;
  262. }
  263. main()
  264. {
  265. int a[10],i,m,scheck,rcheck;
  266. clrscr();
  267. printf("\nENTER SIZE OF THE STRING:");
  268. scanf("%d",&m);
  269. printf("\nENTER THE ELEMENTS OF THE ARRAY:");
  270. for(i=0;i<m;i++)
  271. scanf("%d",&a[i]);
  272. scheck=sender(a,m);
  273. rcheck=receiver(a,m,scheck);
  274. if(rcheck==0)
  275. printf("\n\nNO ERROR IN TRANSMISSION\n\n");
  276. else
  277. printf("\n\nERROR DETECTED");
  278. getch();
  279. }
  280. Output :
  281.  
  282.  
  283. Program-4: Generation and checking of CRC
  284.  
  285. Code:
  286. #include<stdio.h>
#include<conio.h>
#define N strlen(g)

char t[128], cs[128], g[]="100010000";
int a, e, c;

void xor() {
for(c=1;c}

void crc() {
for(e=0;e do {
if(cs[0]=='1') xor();
for(c=0;c cs[c]=t[e++];
}while(e<=a+N-1);
}

void main() {
clrscr();
printf("\nEnter poly : "); scanf("%s",t);
printf("\nGenerating Polynomial is : %s",g);
a=strlen(t);
for(e=a;e
 printf("\nModified t[u] is : %s",t);
crc();
printf("\nChecksum is : %s",cs);
for(e=a;e printf("\nFinalCodeword is : %s",t);
printf("\nTest Error detection 0(yes) 1(no) ? : ");
scanf("%d",&e);
if(e==0) {
printf("Enter position where error is to inserted : ");
scanf("%d",&e);
t[e]=(t[e]=='0')?'1':'0';
printf("Errorneous data : %s\n",t);
}
crc();
for (e=0;(e<n-1)&&(cs[e]!='1');e++);
 if(e
 else printf("No Error Detected.");
getch();
}
  287. Output:
  288.  
  289. Program-5: Generation of hamming code
  290.  
  291. Code:
  292. #include<stdio.h>
  293. #include<conio.h>
  294. void main() {
  295. int data[7],rec[7],i,c1,c2,c3,c;
  296. printf("this works for message of 4bits in size \nenter message bit one by one: ");
  297. scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);
  298. data[6]=data[0]^data[2]^data[4];
  299. data[5]=data[0]^data[1]^data[4];
  300. data[3]=data[0]^data[1]^data[2];
  301. printf("\nthe encoded bits are given below: \n");
  302. for (i=0;i<7;i++) {
  303. printf("%d ",data[i]);
  304. }
  305. printf("\nenter the received data bits one by one: ");
  306. for (i=0;i<7;i++) {
  307. scanf("%d",&rec[i]);
  308. }
  309. c1=rec[6]^rec[4]^rec[2]^rec[0];
  310. c2=rec[5]^rec[4]^rec[1]^rec[0];
  311. c3=rec[3]^rec[2]^rec[1]^rec[0];
  312. c=c3*4+c2*2+c1 ;
  313. if(c==0) {
  314. printf("\ncongratulations there is no error: ");
  315. } else {
  316. printf("\nerron on the postion: %d\nthe correct message is \n",c);
  317. if(rec[7-c]==0)
  318. rec[7-c]=1; else
  319. rec[7-c]=0;
  320. for (i=0;i<7;i++) {
  321. printf("%d ",rec[i]);
  322. }
  323. }
  324. getch();
  325. }
  326.  
  327.  
  328.  
  329. //sliding window protocol-go back n arq
  330.  
  331.  
  332.  
  333. #include<iostream>
  334.  
  335. #include<math.h>
  336.  
  337. using namespace std;
  338.  
  339. int main()
  340.  
  341. {
  342.  
  343. int m,ws,check=1,count=0;
  344.  
  345. cout<<"enter the value of m"<<endl; //what is m???
  346.  
  347. cin>>m;
  348.  
  349. //calculating window size
  350.  
  351. ws=pow(2,m)-1;
  352.  
  353. cout<<"ws= "<<ws<<endl;
  354.  
  355. int sn=0,sf=0,rn=0,ack,time;
  356.  
  357. cout<<"enter time out value "<<endl;
  358.  
  359. cin>>time;
  360.  
  361. while(check)
  362.  
  363. {
  364.  
  365. //cout<<sn<<" "<<ws<<endl;
  366.  
  367. if(sn == ws)
  368.  
  369. {
  370.  
  371. cout<<"window full cannot enter anymore data"<<endl;
  372.  
  373. break;
  374.  
  375. }
  376.  
  377. cout<<"press 1 to transmit data else press 0"<<endl;
  378.  
  379. cin>>check;
  380.  
  381. sn++;
  382.  
  383. cout<<"press 1 if acknowledgement is received else press 0"<<endl;
  384.  
  385. cin>>ack;
  386.  
  387. if(ack)
  388.  
  389. {
  390.  
  391. rn++;
  392.  
  393. sf++;
  394.  
  395. time=3;
  396.  
  397. }
  398.  
  399. else
  400.  
  401. {
  402.  
  403. time--;
  404.  
  405. if(time==0)
  406.  
  407. {
  408.  
  409. cout<<"time out transmit again from frame "<<sf<<endl;
  410.  
  411. sn=sf;
  412.  
  413. }
  414.  
  415. }
  416.  
  417. cout<<"data frames sent= "<<sn<<endl;
  418.  
  419. cout<<"data frames received= "<<rn<<endl;
  420.  
  421. }
  422.  
  423.  
  424.  
  425.  
  426.  
  427. }
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434. #client chat app using tcp ip
  435.  
  436. chatappserver.c
  437. #include<sys/socket.h>
  438. #include<sys/types.h>
  439. #include<stdio.h>
  440. #include<arpa/inet.h>
  441. #include<netinet/in.h>
  442. #include<string.h>
  443. #include<unistd.h>
  444. #define SER_PORT 1200
  445. int main()
  446. {
  447. int a,sersock,newsock,n;
  448. char str[25],str2[25];
  449. struct sockaddr_in seraddr;
  450. struct sockaddr_in cliinfo;
  451. socklen_t csize=sizeof(cliinfo);
  452. seraddr.sin_family=AF_INET;
  453. seraddr.sin_port=htons(SER_PORT);
  454. seraddr.sin_addr.s_addr=htonl(INADDR_ANY);
  455. if((sersock=socket(AF_INET,SOCK_STREAM,0))<0)
  456. {
  457. error("\n socket");
  458. exit(0);
  459. }
  460. if(bind(sersock,(struct sockaddr *)&seraddr,sizeof(seraddr))<0)
  461. {
  462. error("\nBIND");
  463. exit(0);
  464. }
  465. if(listen(sersock,1)<0)
  466. {
  467. error("\n LISTEN");
  468. }
  469. if((newsock=accept(sersock,(struct sockaddr *)&cliinfo,&csize))<0)
  470. {
  471. error("\n ACCEPT");
  472. exit(0);
  473. }
  474. else
  475. printf("\n now connected to %s\n",inet_ntoa(cliinfo.sin_addr));
  476. read(newsock,str,sizeof(str));
  477. do
  478. {
  479. printf("\n client msg:%s",str);
  480. printf("\n server msg:");
  481. scanf("%s",str2);
  482. write(newsock,str2,sizeof(str2));
  483. listen(newsock,1);
  484. read(newsock,str,sizeof(str));
  485. n=strcmp(str,"BYE");
  486. a=strcmp(str2,"BYE");
  487.  
  488. }
  489. while(n!=0||a!=0);
  490. close(newsock);
  491. close(sersock);
  492. return 0;
  493. }
  494.  
  495.  
  496. chatappclient.c
  497. #include<stdio.h>
  498. #include<sys/socket.h>
  499. #include<sys/types.h>
  500. #include<arpa/inet.h>
  501. #include<netinet/in.h>
  502. #include<unistd.h>
  503. #define SER_PORT 1200
  504. int main(int count,char*arg[])
  505. {
  506. int a,clisock;
  507. char str[20],str2[20];
  508. struct sockaddr_in cliaddr;
  509. cliaddr.sin_port=htons(SER_PORT);
  510. cliaddr.sin_family=AF_INET;
  511. cliaddr.sin_addr.s_addr=inet_addr(arg[1]);
  512. clisock=socket(AF_INET,SOCK_STREAM,0);
  513. if(clisock<0)
  514. {
  515. perror("\n SOCKET");
  516. exit(0);
  517. }
  518. if(connect(clisock,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0)
  519. {
  520. perror("\n CONNECT");
  521. exit(0);
  522. }
  523. printf("\nclient connected to %s",arg[1]);
  524. printf("\nCLIENT");
  525. scanf("%s",&str);
  526. if(write(clisock,str,sizeof(str))<0)
  527. {
  528. printf("\n data could not be sent");
  529. }
  530. do
  531. {
  532. listen(clisock,1);
  533. read(clisock,str2,sizeof(str2));
  534. printf("\nserver msg:%s",str2);
  535. printf("\nclient msg:");
  536.  
  537. scanf("%s",&str);
  538. a=strcmp(str2,"BYE");
  539. write(clisock,str2,sizeof(str2));
  540. }
  541. while(a!=0);
  542. close(clisock);
  543. return 0;
  544. }
  545.  
  546.  
  547.  
  548. Activity 8
  549. Q8. Develop a TCP Client/Server application for transferring a text file from client to server.
  550.  
  551. SERVER CODE :#include<stdio.h>
  552. #include<stdlib.h>
  553. #include<unistd.h>
  554. #include<errno.h>
  555. #include<string.h>
  556. #include<netdb.h>
  557. #include<sys/types.h>
  558. #include<netinet/in.h>
  559. #include<sys/socket.h>
  560. int main(int argc, char *argv[])
  561. {
  562. int sock_des, port,n,length,new_des,r;
  563. FILE *fp;
  564. char buffer[512],s[100],ch;
  565. struct sockaddr_in server_addr, client_addr;
  566. if (argc != 2)
  567. {
  568. printf("Usage: ./server port\n");
  569. exit(1);
  570. port= atoi(argv[1]);
  571. if ((sock_des = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  572. {
  573. perror("socket");
  574. exit(1);
  575. }
  576. server_addr.sin_family = AF_INET;
  577. server_addr.sin_port = htons(port);
  578. server_addr.sin_addr.s_addr= htonl(INADDR_ANY);
  579. if (bind(sock_des,(const struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
  580. {
  581. perror("bind");
  582. exit(1);
  583. }
  584. if(listen(sock_des,5)==-1)
  585. {
  586. perror("listen");
  587. exit(1);
  588. }
  589. printf("Server is listening at port %d \n", port );
  590. length=sizeof(struct sockaddr);
  591. if((new_des= accept(sock_des, (struct sockaddr *)&client_addr,&length)) ==-1 )
  592. {
  593. perror("accept");
  594.  
  595. exit(1);
  596. }
  597. fp=fopen("s1.txt","w");
  598. while(1)
  599. {
  600. r=read(new_des,&ch,1);
  601. if(r==1)
  602. fputc(ch,fp);
  603. else
  604. break;
  605. }
  606. fclose(fp);
  607. printf("Success");
  608. close(new_des);
  609. return 0;
  610. }
  611. OUTPUT :-
  612.  
  613. CLIENT CODE :#include<stdio.h>
  614. #include<stdlib.h>
  615. #include<unistd.h>
  616. #include<errno.h>
  617. #include<string.h>
  618. #include<netdb.h>
  619. #include<sys/types.h>
  620. #include<netinet/in.h>
  621. #include<sys/socket.h>
  622. int main(int argc, char *argv[])
  623. {
  624. int sock_des,port,n;
  625. char ch;
  626. FILE *fp;
  627. struct sockaddr_in server_addr;
  628. struct hostent *host;
  629.  
  630. if (argc != 3)
  631. {
  632. printf("Usage: ./client hostname port\n");
  633. exit(1);
  634. }
  635. if ((host=gethostbyname(argv[1])) == NULL)
  636. {
  637. printf("Unknown Host\n");
  638. exit(1);
  639. }
  640. port = atoi(argv[2]);
  641. if ((sock_des = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  642. {
  643. perror("socket");
  644. exit(1);
  645. }
  646. server_addr.sin_family = AF_INET;
  647. server_addr.sin_port = htons(port);
  648. server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  649. if(connect(sock_des,(struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
  650. {
  651. perror("connect");
  652. exit(1);
  653. }
  654. fp=fopen("s2.txt","r");
  655. while((ch=fgetc(fp))!=EOF)
  656. {
  657. write(sock_des,&ch,1);
  658. }
  659. fclose(fp);
  660. close(sock_des);
  661. return 0;
  662. }
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669. ACTIVITY 9
  670. Question :
  671.  
  672. Implement a TCP based server program to authenticate the client’s
  673. Username and
  674. Password. The authenticity of the client must be sent as
  675. the reply mess age to the client and display the
  676. same on the standard output.
  677.  
  678. Code :Server :
  679. #include <stdio.h>
  680. #include <stdlib.h>
  681. #include <unistd.h>
  682. #include <errno.h>
  683. #include <string.h>
  684. #include <netdb.h>
  685. #include <sys/types.h>
  686. #include <netinet/in.h>
  687. #include <sys/socket.h>
  688. #include <arpa/inet.h>
  689. int main(int argc, char *argv[])
  690. {
  691. int sock_des,port,n1,n2,s1,s2;
  692. char null_character;
  693. char buffer1[100],buffer2[100],buffer3[100],buffer4[100];
  694. int length,new_des;
  695. struct sockaddr_in server_addr,client_addr;
  696. bzero(buffer3,sizeof(buffer3));
  697. printf("\n Input Authentic Username :\n");
  698. scanf("%s",buffer3);
  699. bzero(buffer4,sizeof(buffer4));
  700. printf("\n Input Authentic Password :\n");
  701. scanf("%s",buffer4);
  702. if(argc!=2)
  703. {
  704. printf("Usage: ./server port\n");
  705. exit(1);
  706. }
  707. port= atoi(argv[1]);
  708. if((sock_des=socket(AF_INET,SOCK_STREAM,0))==-1)
  709. {
  710. perror("socket");
  711. exit(1);
  712. }
  713.  
  714. server_addr.sin_family = AF_INET;
  715. server_addr.sin_port = htons(port);
  716. server_addr.sin_addr.s_addr= htonl(INADDR_ANY);
  717. if (bind(sock_des,(const struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
  718. {
  719. perror("bind");
  720. exit(1);
  721. }
  722. if( listen(sock_des,5)==-1)
  723. {
  724. perror("listen");
  725. exit(1);
  726. }
  727. printf("Server is listening at port %d \n", port);
  728. while(1)
  729. {
  730. if((new_des=accept(sock_des,(struct sockaddr *)&client_addr,&length))==-1)
  731. {
  732. perror("accept");
  733. exit(1);
  734. }
  735. bzero(buffer1,sizeof(buffer1));
  736. n1=read(new_des,buffer1,sizeof(buffer1));
  737. buffer1[n1]=null_character;
  738. if((s1=strcmp(buffer1,buffer3))==0)
  739. {
  740. write(new_des,"Username Match!Enter Password",30);
  741. printf("Username Match !!!\n");
  742. bzero(buffer2,sizeof(buffer2));
  743. n2=read(new_des,buffer2,sizeof(buffer2));
  744. buffer2[n2]=null_character;
  745. if((s2=strcmp(buffer2,buffer4))==0)
  746. {
  747. write(new_des,"Password Match",15);
  748. printf("Password Match !!!\n");
  749. }
  750. else
  751. {
  752. write(new_des,"Password NOT Match",19);
  753. printf("Password NOT Match !!!\n");
  754. }
  755. }
  756. else
  757. {
  758. write(new_des,"Username Not Match",19);
  759. printf("Username Not Match !!!\n");
  760. }
  761. close(new_des);
  762. }
  763. }
  764.  
  765. Client :-
  766.  
  767. #include <stdio.h>
  768. #include <stdlib.h>
  769. #include <unistd.h>
  770. #include <errno.h>
  771. #include <string.h>
  772. #include <netdb.h>
  773. #include <sys/types.h>
  774. #include <netinet/in.h>
  775. #include <sys/socket.h>
  776. #include <arpa/inet.h>
  777. int main(int argc,char *argv[])
  778. {
  779. int sock_des,port,n;
  780. char null_character;
  781. struct hostent *host;
  782. char buffer1[100],buffer2[100],buffer3[100],buffer4[100];
  783. struct sockaddr_in server_addr;
  784. if(argc!=3)
  785. {
  786. printf("Usage: ./client hostname port\n");
  787. exit(1);
  788. }
  789. if((host=gethostbyname(argv[1]))==NULL)
  790. {
  791. printf("Unknown Host\n");
  792. exit(1);
  793. }
  794. port = atoi(argv[2]);
  795. if((sock_des=socket(AF_INET,SOCK_STREAM,0))==-1)
  796. {
  797. perror("socket");
  798. exit(1);
  799. }
  800. server_addr.sin_family = AF_INET;
  801. server_addr.sin_port = htons(port);
  802. server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  803. if(connect(sock_des,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
  804. {
  805. perror("connect");
  806. exit(1);
  807. }
  808. bzero(buffer1,sizeof(buffer1));
  809. printf("Enter the Username :\n");
  810. scanf("%s",buffer1);
  811. write(sock_des,buffer1,sizeof(buffer1));
  812. bzero(buffer4,sizeof(buffer4));
  813.  
  814. n=read(sock_des,buffer4,sizeof(buffer4));
  815. buffer4[n]= null_character;
  816. printf("Server Sent : %s\n", buffer4);
  817. bzero(buffer2,sizeof(buffer2));
  818. printf("Enter the Password :\n");
  819. scanf("%s",buffer2);
  820. write(sock_des,buffer2,sizeof(buffer2));
  821. bzero(buffer3,sizeof(buffer3));
  822. n=read(sock_des,buffer3,sizeof(buffer3));
  823. buffer3[n]=null_character;
  824. printf("Server Sent : %s\n", buffer3);
  825. close(sock_des);
  826. }
Add Comment
Please, Sign In to add comment