Code :
- #include<stdio.h>
- #include<unistd.h>
- #include<arpa/inet.h>
- #include<netdb.h>
- #include<netinet/in.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<termios.h>
- main(argc,argv)
- int argc;
- char** argv;
- {
- int sock_com,nb_caract_lus;
- struct sockaddr_in adresse_distant;
- char buffer[1024];
- char* port="23";
- struct termios term_init,term_com;
- fd_set desc_term_com;
- memset(&adresse_distant,0,sizeof( struct sockaddr_in));
- if((adresse_distant.sin_addr.s_addr=inet_addr(argv[1]))==INADDR_NONE)
- {
- perror ("\nErreur sur l'adresse ip" );
- exit(1);
- }
- adresse_distant.sin_family=AF_INET;
- adresse_distant.sin_port=htons(atoi(port));
- if((sock_com=socket(PF_INET,SOCK_STREAM,0))<0)
- perror("\nerreur sur socket" );
- printf("\nNumero de socket attribuee %d\n",sock_com);
- printf("\nDemande de connection sur distant" );
- if(connect(sock_com,(struct sockaddr*) &adresse_distant, sizeof (struct sockaddr_in))<0)
- perror("\nerreur sur connect" );
- printf("\nconnexion reussie" );
- printf("\nResultat de l'initialistion de la structure sockaddr_in" );
- printf("\nadresse: en hexa %08X\ten decimal pointee %s\nNumero de port en Hexa %08X\ten decimal %d",ntohl(adresse_distant.sin_addr.s_addr),inet_ntoa(adresse_distant.sin_addr),ntohs(adresse_distant.sin_port),ntohs(adresse_distant.sin_port));
- printf("\nRecuperation des attributs de l'entree du terminal" );
- tcgetattr(STDIN_FILENO,&term_init);
- tcgetattr(STDIN_FILENO,&term_com);
- printf("\nPassage en mode brut de term_com" );
- cfmakeraw(&term_com);
- printf("\nApplication des changement de l'entree en mode brut" );
- tcsetattr(STDIN_FILENO,TCSANOW,&term_com);
- while(1)
- {
- FD_ZERO(&desc_term_com);
- FD_SET(sock_com,&desc_term_com);
- FD_SET(STDIN_FILENO,&desc_term_com);
- if(select(sock_com+1,&desc_term_com,NULL,NULL,NULL)<0)
- break;
- if(FD_ISSET(sock_com,&desc_term_com))
- {
- if( nb_caract_lus=read(sock_com,buffer,1024)<=0)
- break;
- write(STDOUT_FILENO,buffer,nb_caract_lus);
- }
- if(FD_ISSET(STDIN_FILENO,&desc_term_com))
- {
- if( nb_caract_lus=read(STDIN_FILENO,buffer,1024)<=0)
- break;
- write(sock_com,buffer,nb_caract_lus);
- }
- }
- printf("\nSortie du while" );
- tcsetattr(STDIN_FILENO,TCSANOW,&term_init);
- printf("\nrestitution du terminal initial" );
- close(sock_com);
- printf("\nfermeture de la socket" );
- }
- "\nrestitution du terminal initial" );
- close(sock_com);
- printf("\nfermeture de la socket" );
- }
|