SFML community forums

Help => Network => Topic started by: b1nary on June 09, 2018, 11:55:17 pm

Title: Server such as Apache
Post by: b1nary on June 09, 2018, 11:55:17 pm
I've written a little application that sends an index.html to connected clients simultaneously. When I connect to that server in Firefox it displays the website as wished. So far so good.
My problem now is that Firefox then sends more requests to receive more data like images and so on. So I thought: well, I just call receive() on that new TcpSocket to identify the files Firefox wants. But I ended up with the problem that receive() doesn't give me (the server) any output.

Some Code:
void Server::ServeClient( ClientServeParams_t* ServeParams )
{
        ClientServeParams_t ClientServeParams = *ServeParams;
        delete ServeParams;

#ifdef HTTP
        ClientServeParams.ClientSocket->setBlocking( false );
        CCL::String RecvData;
        sf::Socket::Status recvStatus = sf::Socket::Status::NotReady;
        int fails = 0;
        do
        {
                sf::Packet Packet;
                recvStatus = ClientServeParams.ClientSocket->receive( Packet );
                if ( recvStatus == sf::Socket::Status::Done )
                {
                        RecvData.append( (char*) Packet.getData( ) );
                        RecvData.resize( Packet.getDataSize( ) );
                }
                else
                        fails++;
        } while ( recvStatus != sf::Socket::Status::Done && fails < 3 );
        CCL::System::IO::Console::WriteLineFmtColor( CCL::System::IO::Console::Color::RED, "[SERVER] Client % sent: %", { ClientServeParams.Client_ID, RecvData } );
        ClientServeParams.ClientSocket->setBlocking( true );


        char TempPath[MAX_PATH];
        HMODULE hModule = GetModuleHandleA( NULL );
        if ( hModule != NULL )
                GetModuleFileNameA( hModule, TempPath, ( sizeof( TempPath ) ) );
        CCL::String AppPath( TempPath );
        AppPath.resize( AppPath.find_last_of( '\\' ) );

        CCL::System::IO::Directory Root( CCL::String( AppPath ) + "/html/" );
        if ( Root.Exists( ) )
        {
                CCL::System::IO::File Index( Root.GetPath( ) + "index.html" );

                CCL::String Data;
                Index.Read( Data );
                CCL::String SendData = "HTTP/1.1 200 OK\nContent-length: " + CCL::String( Data.size( ) ) + "\nContent-Type: text/html\n\n" + Data;

                sf::Packet Packet;
                Packet.append( SendData.ToConstCharArray( ), SendData.size( ) );
                sf::Socket::Status status = ClientServeParams.ClientSocket->send( Packet );
                if ( status != sf::Socket::Done )
                {
                        CCL::System::IO::Console::WriteLineFmtColor( CCL::System::IO::Console::Color::RED, "[SERVER] Client % lost Connection.", { ClientServeParams.Client_ID } );
                }
        }

#else

        while ( true )
        {
                CCL::String Data = "[" + CCL::String( ClientServeParams.Client_ID ) + "] " + CCL::String( GetTickCount( ) );
                sf::Packet Packet;
                Packet.append( Data.ToConstCharArray( ), Data.size( ) );
                sf::Socket::Status status = ClientServeParams.ClientSocket->send( Packet );
                if ( status != sf::Socket::Done )
                {
                        CCL::System::IO::Console::WriteLineFmtColor( CCL::System::IO::Console::Color::RED, "[SERVER] Client % lost Connection.", { ClientServeParams.Client_ID } );
                        break;
                }
                Sleep( 1000 / 128 );
        }
#endif

        ClientServeParams.ClientSocket->disconnect( );
        delete ClientServeParams.ClientSocket;
}
 
Title: Re: Server such as Apache
Post by: eXpl0it3r on June 10, 2018, 12:32:16 am
Did you debug the code?
Do you establish a connection for the browser's requests?
Have you checked in the browser's developer tool, what the network traffic is showing, i.e. whether the request was accepted or not, etc?
Title: Re: Server such as Apache
Post by: b1nary on June 10, 2018, 11:32:13 am
I am accepting the requests, the image shows one request to localhost:80 from Firefox which then makes another four requests for the data missing(see in network traffic)
(https://i.gyazo.com/c89174431920d379957fce745f87fac2.png)

And here's the network traffic:
(click to show/hide)

I implemented the method to receive data from a TcpSocket in Client to try the system in general. A communication of my Client and my Server worked well. But with the exact same implementation the server doesn't receive anything from firefox (seen in 1st image).