You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xbox360接收C#服务器.bin文件缺失4KB数据问题求助

问题描述

基于Xbox 360 SDK搭建了PC端C#文件传输服务器,向Xbox 360端的C++接收程序发送.bin文件时,接收的文件始终比原文件小4KB,经HxD工具确认缺失的数据位于文件末尾。具体数据:发送文件大小为17,301,504字节,接收文件大小为17,297,408字节。此外,校验和函数会导致程序崩溃,但这并非当前优先处理的问题。

C#服务器代码
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客户端代码
// 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();

内容的提问来源于stack exchange,提问作者552eden

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 23:18:33