/* NAME: cliente SYNOPSIS: cliente hostid portnumber nickname */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MAXHOSTNAME 80 #define BUFSIZE 1024 #define FIFONAME "myfifo" /* IMPORTANTE Se debe modificar XTERM si se encuentra en otro sistema operativo */ #define XTERM "/usr/X11R6/bin/xterm" void bcopy(const void *, void *, size_t ); unsigned long int inet_addr(const char *); char *inet_ntoa(struct in_addr ); void cleanup(char *); void pop_nl(char *); int atoi(const char *); int strncmp(const char *, const char *, size_t ); pid_t waitpid(pid_t , int *, int ); char buf[BUFSIZE],rbuf[BUFSIZE],margen[] = "\n%"; int main(int argc, char *argv[]) { pid_t pid; pid_t pid_sapo; /* Variables para la FIFO */ int fd,sd,fromlen,rc, cc; char buf[1024],buf_send[1024],user[512],ThisHost[80], Titulo[80]; struct sockaddr_in server; struct hostent *hp, *gethostbyname(); struct servent *sp; struct sockaddr_in from; struct sockaddr_in addr; if(argc !=4 ){ printf("Coloque los datos necesarios para comenzar la sesion\ncliente hostid portnumber nickname \n "); exit(1); } if (signal(SIGTSTP, SIG_IGN) == SIG_ERR) { printf("signal(SIGINT) error"); exit(1); } strcpy(user, argv[3]); sp = getservbyname("echo", "tcp"); gethostname(ThisHost, MAXHOSTNAME); printf(" Cliente en host : %s\n", ThisHost); if ( (hp = gethostbyname(ThisHost)) == NULL ) { fprintf(stderr, "No puedo encontrar el host %s\n", ThisHost); exit(-1); } bcopy ( hp->h_addr, &(server.sin_addr), hp->h_length); printf(" Cliente INET ADDRESS : %s )\n", inet_ntoa(server.sin_addr)); if ( (hp = gethostbyname(argv[1])) == NULL ) { addr.sin_addr.s_addr = inet_addr(argv[1]); if ((hp = gethostbyaddr((void *)&addr.sin_addr.s_addr, sizeof(addr.sin_addr.s_addr),AF_INET)) == NULL) { fprintf(stderr, "No puedo encontrar %s\n", argv[1]); exit(-1); } } printf(" Server en host : %s\n", hp->h_name); bcopy ( hp->h_addr, &(server.sin_addr), hp->h_length); printf(" Server INET ADDRESS : %s \n", inet_ntoa(server.sin_addr)); /* Construct name of socket to send to. */ server.sin_family = hp->h_addrtype; server.sin_port = htons(atoi(argv[2])); /* Create socket on which to send and receive */ sd = socket (hp->h_addrtype,SOCK_STREAM,0); if (sd<0) { perror("opening stream socket"); exit(-1); } /* Connect to SERVER */ if ( connect(sd, (struct sockaddr *)&server, sizeof(server)) < 0 ) { close(sd); perror("connecting stream socket"); exit(0); } fromlen = sizeof(from); if (getpeername(sd, (struct sockaddr *)&from,&fromlen)<0){ perror("could't get peername\n"); exit(1); } /* Enviar Datos */ sprintf(buf_send,"USER:%s",user); if (send(sd, buf_send, strlen(buf_send), 0) <0 ) perror("enviando data"); if ((hp = gethostbyaddr((void *)&from.sin_addr.s_addr, sizeof(from.sin_addr.s_addr),AF_INET)) == NULL) fprintf(stderr, "No puede localizar el host %s\n", inet_ntoa(from.sin_addr)); else printf("(Charla Server : %s en %s:%d)\n", hp->h_name ,inet_ntoa(from.sin_addr) ,ntohs(from.sin_port)); /* * Crea un subproceso que muestra un xterm que recibe los mensajes * del servidor */ /* * Crea la FIFO para comunicarse con el programa fifo_srv que * se esta ejecutando en el xterm del hijo */ unlink(FIFONAME); if (mkfifo(FIFONAME, 0666) < 0) { perror("mkfifo"); exit(1); } if ((pid = fork()) < 0) { perror("fork"); exit(1); } if (pid == 0) { sprintf(Titulo,"Mensajes recibidos por \"%s\" desde \"%s\"",user,hp->h_name ) ; execl(XTERM,"xterm", "-T",Titulo,"-e","./fifo_srv",0); perror("exec"); _exit(127); } if ((pid_sapo = fork()) < 0) { perror("fork"); exit(1); } if (pid_sapo == 0) { if ((fd = open(FIFONAME, O_WRONLY)) < 0) { perror("open"); exit(1); } for(;;) { cleanup(rbuf); if ((cc=recv(sd, rbuf, sizeof(rbuf), 0)) < 0) perror("recibiendo data"); write(fd, rbuf, sizeof(rbuf)); } close(fd); exit(0); } /*Toma los datos del Usuario,y la envia al Server. */ printf("\nEscriba el mensaje seguido de RETURN.\n !quit o CTRL-D para salir\n"); for(;;) { cleanup(buf); cleanup(rbuf); write(1,margen, sizeof(margen)); rc=read(0,buf, sizeof(buf)) ; if (rc == 0) break; /*Envia mensajes del usuario */ sprintf(buf_send,"<%s> %s",user,buf); if (send(sd, buf_send, strlen(buf_send), 0) <0 ) perror("enviando data"); if( strncmp("!quit",buf,5) == 0 ) break; } printf ("... exit, gracias por participar en Charla\n"); kill (pid_sapo, SIGINT); if (waitpid(pid, NULL, 0) != pid) /*esperar por el xterm */ perror("no puedo ejecutar xterm, verifique ruta"); unlink(FIFONAME); close(sd); exit (0); } void cleanup(char *buf) { int i; for(i=0; i