Pascal/Turbo Pascal - Problema

 
Vista:

Problema

Publicado por anjuta (17 intervenciones) el 02/09/2008 23:56:24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define MYPORT 3490 /* Puerto de escucha */
#define BACKLOG 10 /* Cantidad de conexiones máximas permit.*/
#define MAX 1024 /*tamaño máximo del buff de lectura de archivo*/

int enviototal(int s, char buffer[],int *len);

int main( ){


int sockfd, new_fd;
/* Escuchar sobre sock_fd, nuevas conexiones sobre new_fd*/
struct sockaddr_in dir_local; /*información sobre mi dirección*/
struct sockaddr_in dir_entrante;
/* información sobre la dirección del cliente*/
int yes=1,bytesrec,sin_size;
FILE* archivo;
char nomarch[40],carpeta[40],dir[100],httpmsj[512];
char http200[200];
char http404[200];
char *parse,*p,*ptr;
int len,bytesleidos,cant_nv;
char buffer[1024];
int total,leido;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit (1);
}

if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,
sizeof(int)) == -1)
{
perror("setsockopt");
exit (1);
}

dir_local.sin_family = AF_INET;
// Ordenación de bytes de la máquina
dir_local.sin_port = htons(MYPORT);
// short, Ordenación de bytes de la red
dir_local.sin_addr.s_addr = htonl(INADDR_ANY);
// Rellenar con mi dirección IP
memset(&(dir_local.sin_zero),0, 8);
// Poner a cero el resto de la estructura

if (bind(sockfd, (struct sockaddr *)&dir_local,
sizeof (struct sockaddr))== -1)
{
perror("bind");
exit(1);
}


if (listen(sockfd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}

while(1) {


sin_size =sizeof(struct sockaddr_in);

if ((new_fd = accept(sockfd,
(struct sockaddr *) &dir_entrante,&sin_size)) == -1)
{
perror("accept");
continue;
}

/*recibe nombre del archivo y se extrae del mensaje GET
el nombre para intentar la apertura*/

bytesrec=recv(new_fd,httpmsj,MAX-1,0);
total=bytesrec;
httpmsj[bytesrec]='';
while ((ptr=strstr(httpmsj," ") )== NULL)
{

bytesrec=recv(new_fd,&httpmsj[total],MAX-1-total,0);
total+=bytesrec;
httpmsj[total]='';

}

if(((p=strstr(httpmsj,"1.0"))!=NULL)||
((p=strstr(httpmsj,"1.1"))!=NULL))

{ parse=strtok(httpmsj,"/");

parse=strtok(NULL," ");

strcpy(nomarch,parse);


/*se arma la ruta de acceso*/

strcpy(carpeta,"/etc/");

sprintf(dir,("%s%s"),carpeta,nomarch);


/*apertura de archivo*/

archivo=fopen(dir,"rb");

/*se procede a mandar los mensajes http correspondientes e iniciar la transferencia*/

if(archivo)
{

if (strstr(httpmsj,"1.0"))

{ sprintf(http200,"HTTP/1.0 200 OK Content-Disposition: attachment; filename="%s" Content-type: application/octet-stream ",nomarch);

len=strlen(http200);

if(enviototal(new_fd,http200,&len)==-1)
/*se asegura de que llegue la totalidad del mensaje HTTP 200*/
{ perror("enviototal");
printf("Se enviaron %d bytes a causa del error ",len);
}

while(leido = fread(buffer,1,sizeof(buffer),archivo))
{
total = 0;
while(total != leido)
{
if((cant_nv = send(new_fd,&buffer[total],leido-total,0)) == -1)
{

perror("send");
}
total+=cant_nv;
}
}

}/*fin strstr*/


else

{ sprintf(http200,"HTTP/1.1 200 OK Content-Disposition: attachment; filename="%s" Content-type: application/octet-stream ",nomarch);

len=strlen(http200);

if(enviototal(new_fd,http200,&len)==-1)
/*se asegura de que llegue la totalidad del mensaje HTTP 200*/
{ perror("enviototal");
printf("Se enviaron %d bytes a causa del error ",len);
}

while(leido = fread(buffer,1,sizeof(buffer),archivo))
{
total = 0;
while(total != leido)
{
if((cant_nv = send(new_fd,&buffer[total],leido-total,0)) == -1)
{

perror("send");
}
total+=cant_nv;
}
}

}



} /*fin then*/

else

{
if (strstr(httpmsj,"1.0"))
{
sprintf(http404,"HTTP/1.0 404 Not Found <b>ERROR</b>: %s was not found ",nomarch);

len=strlen(http404);
if(enviototal(new_fd,http404,&len)==-1)
/*asegura que llegue la totalidad del mensaje HTTP 404*/
{ perror("enviototal");
printf("Se enviaron %d bytes a causa del error ",len);
}

}/*fin then strstr*/

else
{
sprintf(http404,"HTTP/1.1 404 Not Found <b>ERROR</b>: %s was not found ",nomarch);

len=strlen(http404);
if(enviototal(new_fd,http404,&len)==-1)
/*asegura que llegue la totalidad del mensaje HTTP 404*/
{ perror("enviototal");
printf("Se enviaron %d bytes a causa del error ",len);
}

}
} /*fin else strstr*/

//cerrar el socket temporal
close(new_fd);


}/*endif*/

}//end while infinito


close(sockfd);

return 0;

}

int enviototal(int s, char buffer[],int *len)
{
int total=0; /*cuantos bytes se enviaron*/
int restantes=*len; /*cuantos bytes quedan en el buffer*/
int n;
while (total<*len) {
n=send(s,buffer+total,restantes,0);
if(n==-1) {break;}
total+=n;
restantes-=n;
} /*fin while*/

*len=total; /*devuelve la cantidad enviada*/
return (n=-1?-1:0); /*devuelve -1 si falla, 0 en cualquier otro caso*/
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

tengo un problema cn un programa de pasacal

Publicado por adrian (8 intervenciones) el 03/09/2008 07:10:50
no se hacer este problema haber si alguien me dice como hacer el codigo:

leer una lista de 10 personas (nombre, sexo y salario) utilizando 3 vectores.
a) listar sexo del mas joven y del mayor
b)listar aquellos nombres q cobren mas de $500
c)listar el nombre de todas las mujeres.
d)calcular y mostrar el promedio del salario de todas las personas.
el programa debe tener un menu con la opcion para cada isiso
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar