//Funcion start int start_client(const u_short port, const char ip[], const char remoteFilename[], const char localFilename[]) { int socket; socket = newTCPClientSocket4(ip, port); if(socket == -1) { return 1; } start_protocol(socket,remoteFilename,localFilename); closeTCPSocket(socket); debug(2,"Close connection (%s:%u)",ip,port); return 0; }
int start_client(const u_short port, const char ip[], const char remoteFilename[], const char localFilename[]) { //Esta funcion inicia el cliente si no hay errores al crear el socket del cliente int socket; socket = newTCPClientSocket4(ip, port); if(socket == -1) { //Si hubo un error al crear el socket, regresa un 1 return 1; } //si no hubo errores, iniciamos el protocolo beginProtocol(socket,remoteFilename,localFilename); closeTCPSocket(socket); //cerramos el socket //debug(2,"Close connection (%s:%u)",ip,port); return 0; }
int get_LIST(const u_short port, const char ip[]) { int socket; char *list,*end; char *readBuffer = (char *) malloc(255); char *writeBuffer = (char *) malloc(255); int readBytes; socket = newTCPClientSocket4(ip, port); //Creamos el socket del cliente if(socket == -1) { //Si no se pudo, error, regresamos un 1 return 1; } //Si todo salio bien... sprintf(writeBuffer,"LIST\r\n\r\n"); //imprimimos en writeBuffer el header LIST sendLine(socket,writeBuffer); //y lo escribimos en el socket //ahora leemos la respuesta al LIST list = (char *) malloc(254); //list guardarĂ¡ la lista de archivos del cliente while((readBytes = read(socket,readBuffer,1))>0) {//leemos byte por byte readBuffer list = (char *) realloc(list,strlen(list)+readBytes+1); strncat(list,readBuffer,readBytes);//concatenamos en list lo que se haya leido en readBuffer end = list+(strlen(list)-4);//colocamos end 4 bytes antes del final de list if(strcmp(end,"\r\n\r\n")==0) { //si hemos llegado al final del bufer: *end = '\0'; //Colocamos un caracter nulo break; //Solo entonces dejamos de leer } } printf("Esta es la lista de archivos del host:\n"); printf("%s\n",list); //entonces imprimimos la lista entera //Liberamos la memoria de los punteros. free(list); free(readBuffer); free(writeBuffer); ///Y cerramos el socket... //debug(2,"Close connection (%s:%u)",ip,port); closeTCPSocket(socket); return 0; }
//Funcion GET list int get_list(const u_short port, const char ip[]) { int socketf; char *list; char *readBuffer = (char *) malloc(255); char *writeBuffer = (char *) malloc(255); int readBytes; char *ptr; socketf = newTCPClientSocket4(ip, port); if(socketf == -1) { return 1; } sprintf(writeBuffer,"LIST\r\n\r\n"); sendLine(socketf,writeBuffer); //LEEMOS LA RESPUESTA AL GET list = (char *) malloc(254); while((readBytes = read(socketf,readBuffer,1))>0) { list = (char *) realloc(list,strlen(list)+readBytes+1); strncat(list,readBuffer,readBytes); ptr = list+(strlen(list)-4); if(strcmp(ptr,"\r\n\r\n")==0) { *ptr = '\0'; break; } } printf("%s",list); free(list); free(readBuffer); free(writeBuffer); debug(2,"Close connection (%s:%u)",ip,port); closeTCPSocket(socketf); for(;;); return 0; }
//Funcion start int start_client(const u_short port, const char ip[], const char remoteFilename[], const char localFilename[]) { int socketf; socketf = newTCPClientSocket4(ip, port); int sockfd; pid_t pid; struct sockaddr_in servaddr; //if (argc != 2) { // puts("usage: cli <IPaddress>"); // exit(1); //} bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(6000); inet_pton(AF_INET, "192.168.1.255", &servaddr.sin_addr); sockfd = socket(AF_INET, SOCK_DGRAM, 0); if ((pid = fork()) == 0) { dg_cli(stdin, sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); exit(0); } if(socketf == -1) { return 1; } start_protocol(socketf,remoteFilename,localFilename); closeTCPSocket(socketf); debug(2,"Close connection (%s:%u)",ip,port); sleep(2); return 0; }