JomarC
19-05-2008, 21:04
Ora boas,
E assim preciso de fazer um servidor http em linguagem C utilizando “Sockets de Berkeley”.
Vou por o que ja fiz:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void main(void){
struct sockaddr_in me, from;
int newSock,sock;
int adl=sizeof(me);
char dataSize;
int res;
char linha[100];
char *fileNotFound="File Not Found";
FILE *f;
/*Open an TCP socket*/
if ((sock = socket(AF_INET,SOCK_STREAM,0)) < 0) {
perror("Erro ao criar a socket!!");
exit(0);
}
/*bind the local address so that a client can connect*/
bzero((char *)&me,adl);
me.sin_family=AF_INET;
me.sin_addr.s_addr=htonl(INADDR_ANY);
me.sin_port=htons(8454); /* porta local */
if(-1==bind(sock,(struct sockaddr *)&me,adl)){
close(sock);
puts("Porta de servidor ocupada...");
exit(1);
}
listen(sock,5);
for(;;){
/*blocks until a connection request is done*/
/*then returns the new socket descriptor, now complete*/
newSock=accept(sock,(struct sockaddr *)&from,&adl);
if (newSock < 0) {
perror("Erro no newSock!!");
exit(0);
}
/*fork the server so one process handle the connection
and other keeps waiting incoming connection requests*/
if(newSock!=-1){ /* Importante verificar */
res=fork();
if(res){
if(res== -1)
puts("Fork Failed...");
else
printf("New Child Server Process, PID=%i\n",res);
/* Processo PAI continua a receber novas conexoes */
close(newSock);
}
else
{
/* Processo FILHO serve o cliente */
close(sock);
/* E por ai fora********************************************** ***************/
close(newSock);
}
}
}
}
Basicamente a estrutura ta feita! Agora o problema esta quando o processo filho serve o cliente.
Tenho que fazer configurações para o numero de porto a usar e o directorio base para a pesquisa de documentos.
Ninguem da umas luzes ou tutoriais.....agradecido!
E assim preciso de fazer um servidor http em linguagem C utilizando “Sockets de Berkeley”.
Vou por o que ja fiz:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void main(void){
struct sockaddr_in me, from;
int newSock,sock;
int adl=sizeof(me);
char dataSize;
int res;
char linha[100];
char *fileNotFound="File Not Found";
FILE *f;
/*Open an TCP socket*/
if ((sock = socket(AF_INET,SOCK_STREAM,0)) < 0) {
perror("Erro ao criar a socket!!");
exit(0);
}
/*bind the local address so that a client can connect*/
bzero((char *)&me,adl);
me.sin_family=AF_INET;
me.sin_addr.s_addr=htonl(INADDR_ANY);
me.sin_port=htons(8454); /* porta local */
if(-1==bind(sock,(struct sockaddr *)&me,adl)){
close(sock);
puts("Porta de servidor ocupada...");
exit(1);
}
listen(sock,5);
for(;;){
/*blocks until a connection request is done*/
/*then returns the new socket descriptor, now complete*/
newSock=accept(sock,(struct sockaddr *)&from,&adl);
if (newSock < 0) {
perror("Erro no newSock!!");
exit(0);
}
/*fork the server so one process handle the connection
and other keeps waiting incoming connection requests*/
if(newSock!=-1){ /* Importante verificar */
res=fork();
if(res){
if(res== -1)
puts("Fork Failed...");
else
printf("New Child Server Process, PID=%i\n",res);
/* Processo PAI continua a receber novas conexoes */
close(newSock);
}
else
{
/* Processo FILHO serve o cliente */
close(sock);
/* E por ai fora********************************************** ***************/
close(newSock);
}
}
}
}
Basicamente a estrutura ta feita! Agora o problema esta quando o processo filho serve o cliente.
Tenho que fazer configurações para o numero de porto a usar e o directorio base para a pesquisa de documentos.
Ninguem da umas luzes ou tutoriais.....agradecido!