Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Server such as Apache  (Read 2132 times)

0 Members and 1 Guest are viewing this topic.

b1nary

  • Newbie
  • *
  • Posts: 2
    • View Profile
Server such as Apache
« 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;
}
 
« Last Edit: June 10, 2018, 01:50:16 pm by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Server such as Apache
« Reply #1 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

b1nary

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Server such as Apache
« Reply #2 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)


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).
« Last Edit: June 10, 2018, 11:37:46 am by b1nary »