recieved file is 4kb smaller than sent file using winsocX on xbox 360

55 Views Asked by At

I mangled together a c# server that sits on my pc and sends a .bin file to a receiving c++ app on my Xbox 360 using the Xbox 360 sdk. but the received file is always 4kb smaller than the origin file and HxD says that the missing data is in the end of the file.

C# server code:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace FileTransferServer
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 4343;
            IPAddress ipAddr = IPAddress.Any;
            IPEndPoint localEndPoint = new IPEndPoint(ipAddr, port);
            Socket listener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                Console.WriteLine("Waiting for incoming connections...");

                while (true)
                {
                    Socket clientSocket = listener.Accept();
                    Console.WriteLine("Accepted new connection from {0}", clientSocket.RemoteEndPoint);

                    // Receive the filename
                    byte[] filenameBuffer = new byte[1024];
                    int bytesReceived = clientSocket.Receive(filenameBuffer);
                    string filename = System.Text.Encoding.ASCII.GetString(filenameBuffer, 0, bytesReceived);
                    Console.WriteLine("Received filename: {0}", filename);

                    // Send the expected file size
                    FileInfo fileInfo = new FileInfo(filename);
                    byte[] sizeBuffer = System.Text.Encoding.ASCII.GetBytes(fileInfo.Length.ToString());
                    clientSocket.Send(sizeBuffer);
                    Console.WriteLine("file size sent is:"+sizeBuffer);

                    // Open the file
                    FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);

                    // Send the file
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        clientSocket.Send(buffer, bytesRead, SocketFlags.None);
                    }

                    Console.WriteLine("File sent successfully");

                    // Clean up
                    fileStream.Close();
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

C++ Xbox 360 client code:

// Initialize WinsockX
                WSADATA wsaData;
                int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
                if (result != 0) {
                    printf("WSAStartup failed with error: %d\n", result);
                    //return 1;
                }

                // Create a socket
                SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                if (sock == INVALID_SOCKET) {
                    printf("socket creation failed with error: %d\n", WSAGetLastError());
                    WSACleanup();
                    //return 1;
                }

                // Connect to the server
                SOCKADDR_IN target;
                target.sin_family = AF_INET;
                target.sin_port = htons(SERVER_PORT);
                target.sin_addr.s_addr = inet_addr(SERVER_IP);
                result = connect(sock, (SOCKADDR*)&target, sizeof(target));
                if (result == SOCKET_ERROR) {
                    printf("connect failed with error: %d\n", WSAGetLastError());
                    closesocket(sock);
                    WSACleanup();
                    //return 1;
                }

                printf("Connected to server %s:%d\n", SERVER_IP, SERVER_PORT);

                // Send the name of the binary file to receive
                result = send(sock, SENTFILENAME, strlen(SENTFILENAME), 0);
                if (result == SOCKET_ERROR) {
                    printf("send failed with error: %d\n", WSAGetLastError());
                    closesocket(sock);
                    WSACleanup();
                    //return 1;
                }

                // Receive the expected file size
                char sizeBuffer[1024];
                result = recv(sock, sizeBuffer, 1024, 0);
                dprintf("recieved file size from server is:",result);
                if (result == SOCKET_ERROR) {
                    printf("recv failed with error: %d\n", WSAGetLastError());
                    closesocket(sock);
                    WSACleanup();
                    //return 1;
                }
                sizeBuffer[result] = '\0';
                long long expectedFileSize = _atoi64(sizeBuffer);
                printf("Expected file size: %lld\n", expectedFileSize);

                // Open the file for writing
                std::ofstream outputFile(FILENAME, std::ios::out | std::ios::binary);
                if (!outputFile.is_open()) {
                    printf("Failed to create file %s\n", FILENAME);
                    closesocket(sock);
                    WSACleanup();
                    //return 1;
                }

                // Receive the file and compute the checksum
                char buffer[BUFFER_SIZE];
                unsigned int checksum = 0;
                int bytesRead;
                long long totalBytesRead = 0;
                while ((bytesRead = recv(sock, buffer, BUFFER_SIZE, 0)) > 0) {
                    outputFile.write(buffer, bytesRead);
                    checksum += computeChecksum(buffer, bytesRead);
                    totalBytesRead += bytesRead;
                }

                // Check for errors
                if (bytesRead == SOCKET_ERROR) {
                    printf("recv failed with error: %d\n", WSAGetLastError());
                }
                else {
                    printf("File received successfully\n");
                    if (expectedFileSize != totalBytesRead) {
                        printf("Received file size %lld is different from expected file size %lld\n", totalBytesRead, expectedFileSize);
                    }
                    else {
                        /*
                        // Compare the expected checksum to the actual checksum
                        if (checksum == expectedChecksum) {
                            printf("Checksums match, file received successfully\n");
                        }
                        else {
                            printf("Checksums do not match, file may be corrupted\n");
                        }*/
                    }
                }

                // Close the file, socket and cleanup WinsockX
                outputFile.close();


                closesocket(sock);
                WSACleanup();

also, the checksum function is crashing my app but that is not a priority right now

in summary: I tried to send a .bin file from the c# server and receive it on the c++ client. but the received file is 4kb smaller than the sent file to be exact: sent file size:17,301,504 bytes received file size: 17,297,408 bytes

0

There are 0 best solutions below