Guest User

Instructions

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