/**************************************************\ * Autor: AmorpH * Descripcion: SERVIDOR (STREAM) * Proposito: envio de datos entre ordenadores en red * (internet) para hacer consulta @ CAG * Fecha:27-08-05 \**************************************************/ #include <stdio.h> #include <string.h> #include <errno.h> #include <time.h> #include <unistd.h> #include <winsock.h> #define PORT 6789 #define BACKLOG 10 #define STDIN 0 void guarda_data(char *); void lee_data(char *); void envia_todo(int remfd ,char *buf, int *len); int main(int argc, char *argv[]){ /*iniciamos WINSOCK!*/ WSADATA wsaData; if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
printf("WSAStartup fallo"); exit(1); } /*select()*/ fd_set readfd; fd_set master; FD_ZERO(&readfd); FD_ZERO(&master); FD_SET(STDIN, &master); int fdmax; /*select()-fin*/ char ack[80]; int ackn; int nbytes; short i = 0; char t; char *buffer; buffer = (char *)malloc(250000); int sockfd, remfd; struct sockaddr_in localaddr; struct sockaddr_in remoteaddr;
/*creamos el socket*/ if( (sockfd = socket( AF_INET , SOCK_STREAM , 0)) == -1){ perror("socket"); exit(1); } /*Le damos formato*/ localaddr.sin_family = AF_INET; localaddr.sin_port = htons(PORT); localaddr.sin_addr.s_addr = INADDR_ANY; memset( localaddr.sin_zero , '\0' , 8); if((bind( sockfd , (struct sockaddr *)&localaddr ,sizeof(struct sockaddr) )) == -1){ perror("bind"); exit(1); } /*formateado, vamos a escuchar en el puerto*/ if((listen(sockfd,BACKLOG))== -1){ perror("listen"); exit(1); } printf("El servidor est\xA0 listo, esperando una conexi\xA2n\n\n"); /*aceptemos las conexiones y creamos un bucle para que no deje de buscar*/ /*mas select()*/ FD_SET(sockfd,&master); fdmax = sockfd; while(i == 0){ readfd = master; if((select((fdmax+1), &readfd, NULL, NULL, NULL)) == -1){ perror("select"); exit(1); } if((FD_ISSET(sockfd, &master))){ if((remfd = accept(sockfd, (struct sockaddr*)&remoteaddr, 0))==-1){ perror("accept"); break; } /*Preparamos comprobacion*/ if((nbytes = recv(remfd, ack, sizeof(ack), 0))<= 0){ if(nbytes == 0){ printf("\nConnection closed"); continue; } else{ perror("recv"); continue; } } /*Sabemos el tamaño del archivo recibido*/ ackn = atoi(ack); nbytes = 0; do{ if((nbytes = nbytes + (nbytes = recv(remfd, buffer+nbytes, (sizeof(buffer) - nbytes) , 0)))<= 0){ if(nbytes == 0){ printf("\nConnection closed"); continue; } else{ perror("recv"); break; } } }while(nbytes < ackn); *(buffer+ackn) = '\0'; /*Tenemos todos los datos guardadoes en el buffer*/ /*Los guardamos a: "\correu\NAnalitica.txt"*/ guarda_data(buffer); /*Llama a programa2*/ system("c:\\Lab\\LabServ.exe"); lee_data(buffer); nbytes = sizeof(buffer); envia_todo(remfd, buffer, &nbytes); closesocket(remfd); } /*Comprobamos si se pulsa INTRO (depende de qué ordenador cualquier tecla)*/ else if((FD_ISSET(STDIN, &master))){ printf("Con esto finalizará el servidor, está seguro? (S/N)"); fflush(stdin); t = getchar(); t = toupper(t); if( t == 'S' ){ printf("Finalizando...."); i--; } else{ ; } } else{ ; } } system("PAUSE"); closesocket(sockfd); return 0; }
void guarda_data(char *buffer){ FILE *fitch; if((fitch = fopen("/correu/NAnalitica.txt", "w")) == NULL){ perror("fopen"); printf("Impossible guardar los datos, el programa finalizará"); exit(1); } fprintf(fitch, "%[^\0]", buffer); fprintf(fitch, "\0"); fclose(fitch); return; } void lee_data( char *buffer){ FILE *fitch; if((fitch = fopen("/correu/NAnalitica.txt", "r"))== NULL){ perror("fopen"); printf("Impossible leer los datos, el programa finalizará"); exit(1); } int i = 0; do{ *(buffer+i) = fgetc(fitch); i++; }while( feof(fitch) == 0 ); *(buffer + i) = '\0'; fclose(fitch); return; }
void envia_todo(int remfd ,char *buf, int *len){ int enviados = 0; int bytes_en_falta = *len; int n; while( enviados < *len ){ n = send( remfd , buf+enviados , bytes_en_falta, 0 ); if( n == -1){ perror("send"); break; } enviados += n; bytes_en_falta -= n; } return; }
|