1
Network / [Solved] Sending binary files over TCP
« on: September 23, 2011, 08:08:03 pm »
Once again, you've saved the day Laurent. Thanks for the additional tips also. Merci!
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
std::ifstream File(Filename, std::ios::in | std::ios::binary | std::ios::ate);
// Here I got some code to send the filename and size before sending packets
File.seekg(0, std::ios::beg);
EncryptedPacket ToSend; // EncryptedPacket is the same as the one used in the tutorials, for now
Buffer = new char[4096];
while(File.read(Buffer, sizeof(Buffer)))
{
ToSend.Clear(); // Clear the previous packet
ToSend << Buffer; // Add the 4096 bytes we just read to the packet
Socket.Send(ToSend); // Send the packet
std::fill(Buffer, Buffer + sizeof Buffer, 0); // Clear the buffer
}
File.close();
delete[] Buffer;
OutputFile.open(Filename, std::ofstream::binary); // Open a new file for writing, filename comes from the server
while(true)
{
EncryptedPacket Received;
Client.Receive(Received);
std::string Buffer;
Received >> Buffer; // Read the buffer from the packet
OutputFile.write(Buffer.c_str(), sizeof(Buffer)); // Write the buffer to the file
}
#include <iostream>
#include <SFML/Graphics.hpp>
int Opacity = 255;
bool Running = true;
void FadeInThread(void* Data)
{
float Delay = 0.007;
while(Opacity <= 255)
{
std::cout << "Fade is at " << Opacity << std::endl;
Opacity++;
sf::Sleep(Delay);
}
std::cout << "Thread terminated" << std::endl;
}
void FadeIn()
{
Opacity = 0;
sf::Thread Thread(FadeInThread);
Thread.Launch();
}
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");
// Usually this is a video, or image, or text that I fade in and out
sf::Image Image(200, 200, sf::Color(0, 255, 0, Opacity));
sf::Sprite Sprite;
Sprite.SetImage(Image);
FadeIn();
while(Running)
{
// Start - Non related event handling stuff
sf::Event Event;
while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed || (Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
{
Running = false;
}
}
// End - Non related event handling stuff
App.Clear();
// Here I apply the opacity to the image, so that it "fades in"
Sprite.SetColor(sf::Color(0, 255, 0, Opacity));
App.Draw(Sprite);
App.Display();
}
return 0;
}
new Scene();
while(App->IsRunning())
{
... my render stuff, that only starts after the thread is terminated (so after the FadeInThread method is done) ...
}
Scene::Scene(sf::RenderWindow* Window, float Length)
{
... some code ...
this->fadeIn();
}
void Scene::FadeIn()
{
this->Opacity = 0;
sf::Thread Thread(FadeInWrap, this);
Thread.Launch();
// SDL : SDL_CreateThread(FadeInWrap, this);
}
// This is a static method.
void FadeInWrap(void* Scn)
{
((Scene*) Scn)->FadeInThread();
}
void Scene::FadeInThread()
{
float Delay = 0.007;
while(this->Opacity < 255)
{
this->Opacity++;
sf::Sleep(Delay);
// SDL: SDL_Delay(Delay); (Delay was an int calculated differently)
}
}