#include #include #include #include #include #include #include #include #define PORTNUMBER 8189 int main(int argc, char * argv[]) { int n, s, len; char buf[1024]; char op1[]="ON"; char op2[]="OFF"; char hostname[64]; struct hostent *hp; struct sockaddr_in name; switch(argc) { case 1: /* Get our local host name. */ gethostname(hostname, sizeof(hostname)); break; case 2: /* Get name from command line */ strcpy(hostname, argv[1]); break; default: printf("Usage: %s [server_name]\n",argv[0]); exit(-1); } /* Look up our host's network address.*/ hp = gethostbyname(hostname); /* Create a socket in the INET domain.*/ s = socket(AF_INET, SOCK_STREAM, 0); /* Create the address of the server. */ name.sin_family = AF_INET; name.sin_port = htons(PORTNUMBER); memcpy(&name.sin_addr, hp->h_addr_list[0], hp->h_length); len = sizeof(struct sockaddr_in); /* Connect to the server. */ connect(s, (struct sockaddr *) &name, len); /* Read from standard input, and copy the * data to the socket. */ printf("Bienvenido al Switch en linea\n"); printf("Para encender luces ingrese ON, para apagarlas ingrese OFF, para desconectarse del servidor ingrese BYE\n"); printf("Como medida de seguridad, al inicar el cliente automáticamente apagará las luces de su casa\n"); while ((n = read(0, buf, sizeof(buf))) > 0) { if (send(s, buf, n, 0) < 0) { perror("send"); exit(1); } if((strcmp(buf,op1))==0) printf("Ha encendido las luces\nIngrese su opción: "); if((strcmp(buf,op2))==0) printf("Ha apagado las luces\nIngrese su opcion: "); } close(s); exit(0); }