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;
}