I am trying to write a concurrent web server for Linux. I am doing this just for fun (I am not trying to match IIS or Apache). My actual program is quite large so I will only post some highlights.
Printable View
I am trying to write a concurrent web server for Linux. I am doing this just for fun (I am not trying to match IIS or Apache). My actual program is quite large so I will only post some highlights.
Is this just an informative post, or is there some purpose?Quote:
Originally posted by Arrummzen
I am trying to write a concurrent web server for Linux. I am doing this just for fun (I am not trying to match IIS or Apache). My actual program is quite large so I will only post some highlights.
Quzah.
From what I can see this program runs fine. However when I connect with telnet to the HTTP port I only read 'it'. When I put the "Awrite ( socket , " It works!! \n " );" in a for loop it printed it it it it it. Once for ever iteration of the loop! Why did it not write It works!! \n It works!! \n It works!! \n It works!! \n It works!! \n?Code:
void HTTPserver ( int socket , int IPCout , int IPCin )
{
fprintf ( stderr , " the HTTP server is up! \n " );
struct sockaddr_in HTTPSocketRemote;
socklen_t SockInSize;
int ClientSocket;
SockInSize = sizeof ( sockaddr_in );
fd_set read_fds;
struct timeval tv;
int ret=0;
FD_ZERO(&read_fds);
FD_SET(socket , &read_fds); /*listen_sock is socket where are you listening*/
FD_SET(IPCin , &read_fds);
tv.tv_sec = 30;
tv.tv_usec = 0;
while ( 1 )
{
ret = select(socket+1, &read_fds, NULL, NULL, &tv);
if( FD_ISSET ( socket , &read_fds ) )
{
ClientSocket = accept ( socket , ( struct sockaddr *)&HTTPSocketRemote , &SockInSize );
switch ( fork ( ) )
{
case 0: // This is the child proccess
close ( socket );
close ( IPCout );
close ( IPCin );
HTTPdeamon ( ClientSocket );
break;
default: // this is the parrent proccess
close ( ClientSocket );
break;
}
}
}
}
--------------------------------------------------------
void HTTPdeamon ( int socket )
{
Awrite ( socket , " It works!! \ " );
halt ( socket );
close ( socket );
exit ( 0 );
}
--------------------------------------------------------
void halt ( int output )
{
int hold = 0;
Awrite ( output , "Press Enter To Continue \n " );
read ( output , &hold , 1 );
}
--------------------------------------------------------
void Awrite ( int fd , char str[] )
{
write ( fd , str , sizeof ( str ) );
}
--------------------------------------------------------
I hope you can help me.
Thank you for your time,
Arrummzen
WOW quick response. No, I think something went wrong. The sever only posted part of my message.
Thank you for your time,
Arrummzen
You might want to check your return values for write to make sure it's writing the entire block. Write is not guarinteed to write the whole block. It will return the number it was able to write. Commonly you want to use write in a loop to make sure it ends up writing the whole thing.
Quzah.
Thank you. Who would have thought it would be something as simple as that. I was expecting some big error in my multi-tasking error.
Thank you for your time,
Arrummzen
This is not on topic of this thread but Arrummzen are you a common visitor to the HDC BBS?
Yes, I visit the HDC BBS on a fairly regular basis.
Thank you for your time,
Arrummzen