Professional Documents
Culture Documents
Modelos de organizacin de
hilos
a) Modelo del servidor trabajador
b) Modelo de equipo
c) Modelo de entubamiento
<fcntl.h>
<string.h>
<stdlib.h>
<errno.h>
<stdio.h>
<netinet/in.h>
<resolv.h>
<sys/socket.h>
<arpa/inet.h>
<unistd.h>
<pthread.h>
#define PROCESOS 10
#define PUERTO 1101
#define TAMBUFER 1024
memset(&(my_addr.sin_zero), 0, 8);
my_addr.sin_addr.s_addr = INADDR_ANY ;
int hsock;
int *p_int ;
int err;
socklen_t addr_size = 0;
int* csock;
pthread_t thread_id=0;
while(1){
printf("Esperando la conexin\n");
csock = (int*)malloc(sizeof(int));
if((*csock = accept( hsock, (struct sockaddr*)&sadr, &addr_size))!=
-1){
printf("---------------------\n");
printf("Conexin recibida desde
%s\n",inet_ntoa(sadr.sin_addr));
pthread_create(&thread_id,0,&SocketHandler, (void*)csock );
pthread_detach(thread_id);
}
else{
fprintf(stderr, "Error al aceptar una conexin %d\n", errno);
}
}
}
<fcntl.h>
<string.h>
<stdlib.h>
<errno.h>
<stdio.h>
<netinet/in.h>
<resolv.h>
<sys/socket.h>
<arpa/inet.h>
<unistd.h>
p_int = (int*)malloc(sizeof(int));
*p_int = 1;
if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int,
sizeof(int)) == -1 )||
(setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int,
sizeof(int)) == -1 ) ){
printf("Error configurando opciones %d\n",errno);
free(p_int);
exit(-1);
}
free(p_int);
my_addr.sin_family = AF_INET ;
my_addr.sin_port = htons(PUERTO);
memset(&(my_addr.sin_zero), 0, 8);
my_addr.sin_addr.s_addr = inet_addr(DIRECCION);
if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) ==
-1 ){
if((err = errno) != EINPROGRESS){
fprintf(stderr, "Error de conexin del socket %d\n", errno);
exit(-1);
}
}
Ejercicio
Desarrolle un cliente que pueda
trabajar con el siguiente servidor:
servidorsockethilos.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<pthread.h>
<unistd.h>
<errno.h>
<string.h>
<sys/types.h>
<sys/socket.h>
<netinet/in.h>
<arpa/inet.h>
<sys/wait.h>
<signal.h>
int main() {
int ss,s1,s2,n;
fd_set sockfds;
struct timeval tv;
struct sockaddr_in my_addr;
struct sockaddr_in socks[PROCESOS];
pthread_t thread[PROCESOS];
int i=1;
ss = socket( PF_INET, SOCK_STREAM, 0 );
my_addr.sin_family = AF_INET; // Familia de protocolos
my_addr.sin_port = htons(PUERTO); // Nmero de puerto
memset( &(my_addr.sin_zero), '\0', 8 ); // Limpia la memoria
fd = (int)d;
while( 1 ) {
sprintf( buf, "%d\n", i++ );
write( fd, buf, strlen( buf )
+1 );
sleep(4);
}
}
return(0);
}
Prctica 5
SERVIDOR HTTP
Comunicacin
Solicitud HTTP
Lnea de solicitud, especifica el tipo de documento
solicitado. Formado por:
Mtodo (GET, POST, HEAD, etc)
Direccin URL
Versin (HTTP/1.0)
Ejemplo
MTODO VERSIN URL<crlf>
ENCABEZADO: Valor<crlf>
. . . ENCABEZADO: Valor<crlf>
Lnea en blanco <crlf>
CUERPO DE LA SOLICITUD
Respuesta HTTP
Es un conjunto de lineas que se
envian desde el servidor al
navegador, las cuales son:
Linea de estado, especifica versin, el
estado de la solicitud echa y un texto
explicativo
Campos de respuesta, campos
opcionales que permiten aportar
informacin extra sobre la respuesta o el
servidor
Cuerpo de la respuesta, el cual contiene
Ejemplo
VERSIN-HTTP CDIGO EXPLICACIN <crlf>
ENCABEZADO: Valor<crlf>
. . . ENCABEZADO: Valor<crlf>
Lnea en blanco <crlf>
CUERPO DE LA RESPUESTA
Desarrollo
Se implementar un servidor HTTP que
sea capaz de reconocer y servir
peticiones de recursos mediante los
mtodos GET, POST, HEAD (Ver RFC
2616)