Question

I'm having a trouble with receiving binary data to my server (python). It seems that the OS (WIN7) sending big data in several packets without "permission", so when im trying to send from my client (C++) binary data, I have to do some manipulations that will combine all the data. I tried several ways but none of those worked.

here is the sending part (C++ - Works fine for sure):

sendbuf = "2011@" + ReadThisFile("C:\\0x3z4.jpg") + "@"; // buffer should be "2011@<Image Data>@"
// ReadThisFile returns string with binary data from file

vector<char> vect(sendbuf.begin(), sendbuf.end()); // Vector with the image data

iResult = send( ConnectSocket, &vect[0], vect.size(), 0 ); // Sending The Image

here is the receiving part (Python - part of threaded function 'Handler'):

while True:

    Buffer = Sock.recv(self.BufferSize)

    if Buffer[0:4] == "2011":
        self.Print(ADDR[0] + " > 2011 > Capture Screen Response.")

        # Save Image
        Path = datetime.now().strftime("Information\\" + ADDR[0] + "@" + self.Clients[Index].computerName + "\\CaptureScreen.Files\\" + "%d-%m-%Y-%H-%M-%S.png")

        f = open(Path,'wb')
        f.write(Buffer[5:-1])
        data = ""
            # I tried to receive data till i'll find the eof
        while True:
            data += Sock.recv(4096)
            if data.find("EOF"):
                break
        f.write(data)

This question is from Trojan project that me and couple friends working on for our course. Thanks.

Was it helpful?

Solution

You're not sending a the buffer, you're sending the first vector.size() bytes of a vector. Stop the code in a debugger, inspect the memory starting at &vect[0], and compare it to what you're actually recieving, and I think you'll find that your code is behaving correctly (in the sense that it's doing what it's supposed to, not necessarily what you want).

I don'tknow what ReadThisFile does, but you need to actually put the bytes you want to send into a buffer and use that buffer as the argument to send. FWIW, ReadFile would probably be at least as easy for what it appears you're trying to do. Good luck.

Either ReadThisFile needs to tell you how many bytes it read, or you need to get the filesize another way and extrapolate the length of the buffer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top