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

Author Topic: Sending binary file don´t send last bytes  (Read 2143 times)

0 Members and 1 Guest are viewing this topic.

cvkfak

  • Newbie
  • *
  • Posts: 12
    • View Profile
Sending binary file don´t send last bytes
« on: March 06, 2016, 09:35:22 pm »
Hi (sorry for my bad english)
I am currently trying to implement a function to send and receive maps and tilemap, but the problem is when sending the file gets corrupt for lack of approximately 2000 bytes.
I've tried sending other files without success.
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
#include <fstream>

int main(){
    char resp;
    std::cin >> resp;
    sf::TcpSocket socket;
        std::cout << "s = Server      c= Client" << std::endl;
    if(resp=='s')
    {
        sf::TcpListener escucha;
        escucha.listen(43000);
        char Buffer[4096];
        if (escucha.accept(socket) == sf::Socket::Done)
        {
            std::ifstream File("send.bmp", std::ifstream::binary | std::ios::in | std::ios::binary);
            while (File.read(Buffer, sizeof(Buffer)))
            {
                socket.send(&Buffer, sizeof(Buffer));
                //std::cout << Buffer << std::endl;
                //sf::sleep(sf::milliseconds(30));
            }
            std::cout << "Done" << std::endl;
        }
    }
    else if(resp=='c')
    {
          std::cout << "Connecting" << std::endl;
          std::ofstream outfile("receive.bmp",std::ofstream::binary);
          if (socket.connect("127.0.0.1", 43000, sf::seconds(10)) != sf::Socket::Done)
          {
          std::cout << "Conection failed" << std::endl;
          }
          else std::cout << "Connected" << std::endl;
          socket.setBlocking(false);
        while(1){
                char Buffer[4096];
                std::size_t Size = 0;
                socket.receive(&Buffer, sizeof(Buffer),Size);
                outfile.write(Buffer, Size);
                //std::cout << Buffer << std::endl;
            }

    }
    system("PAUSE");
 return 0;
}
 

What is wrong ?
Thanks for you help!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Sending binary file don´t send last bytes
« Reply #1 on: March 06, 2016, 10:03:15 pm »
The way you wrote the server part you will only send full 4096 byte pieces so if file is 5000 then only first 4096 bytes will be sent, if it's 10000 then only first 8192 bytes will be sent and so on.
Back to C++ gamedev with SFML in May 2023

 

anything