奇怪的文件输出套接字文件传输

时间:2016-10-21 09:44:17

标签: c

所以,我正在制作一个socket程序来连接两个程序,到目前为止一切正常;设置winsock,连接和交互。但是,从客户端发送到服务器端的文件中包含奇怪的字符。

示例:

File: test.txt
Contents: Hey, how are you?
** Client sends file over to Server **
Expected Output: Hey, how are you?
Actual Output:  7_ Ä _

服务器代码:

int main() {
    WSADATA Winsock;
    SOCKET Socket, Sub;
    Addr addr;
    IncomingAddress incomingAddress;
    int AddressLen = sizeof(IncomingAddress);

    // Start up Winsock
    WSAStartup(MAKEWORD(2, 2), &Winsock);

    if (LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2) {
        WSACleanup();
        return 0;
    }

    Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    // Memset alternative
    ZeroMemory(&addr, sizeof(Addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(portNumber);
    bind(Socket, (sockaddr*) &addr, sizeof(Addr));

    if (listen(Socket, 1) == SOCKET_ERROR) {
        printf("Listening error!\n");
    } else {
        printf("Listening...\n");
    }

    if (Sub = accept(Socket, (sockaddr*) &incomingAddress, &AddressLen)) {
        int Size;
    char *Filesize = malloc(MAX_PATH);

    if (recv(Socket, Filesize, MAX_PATH, 0)) { // File Size
        Size = atoi((const char*) Filesize);
        printf("File size: %d\n", Size);
    }

    char *Buffer = malloc(Size);

    int Offset = 0;
    while (Size > Offset) {
        printf("TESST");
        int Amount = recv(Socket, Buffer + Offset, Size - Offset, 0);

        if (Amount <= 0) {
            printf("Error: " + WSAGetLastError());
            break;
        } else {
            Offset += Amount;
        }
    }

    FILE *File;
    File = fopen("test.txt", "wb+");
    fwrite(Buffer, 1, Size, File);
    fclose(File);

    getchar();
    closesocket(Socket);
    WSACleanup();

    }
 }
 return 0;
}

客户代码:

int main() {
    WSADATA Winsock;
    SOCKET Socket, Sub;
    Addr addr;
    IncomingAddress incomingAddress;
    int AddressLen = sizeof(IncomingAddress);

    WSAStartup(MAKEWORD(2, 2), &Winsock);

    if (LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2) {
        WSACleanup();
        return 0;
    }

    Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    ZeroMemory(&addr, sizeof(Addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(6000);

    if (connect(Socket, (sockaddr*) &addr, sizeof(Addr)) < 0) {
        printf("Connection Failed!\n");
        getchar();
        return 0;
    }

        char *ClientIP = inet_ntoa(incomingAddress.sin_addr);
        int ClientPort = ntohs(incomingAddress.sin_port);
        printf("Client connected!\n");
        printf("IP: %s:%d\n", ClientIP, ClientPort);

        printf("Sending file... \n");

        FILE *File;
        char *Buffer;
        unsigned long Size;

        File = fopen("test.txt", "rb+");
        if (!File) {
            printf("Error while reading the file!\n");
            getchar();
            return 0;
        }

        fseek(File, 0, SEEK_END);
        Size = ftell(File);
        fseek(File, 0, SEEK_SET);

        Buffer = malloc(Size);

        fread(Buffer, Size, 1, File);
        char cSize[MAX_PATH];
        sprintf(cSize, "%i", Size);

        fclose(File);
        send(socket, cSize, MAX_PATH, 0); // File size

        int Offset = 0;
        while (Size > Offset) {
            int Amount = send(Sub, Buffer + Offset, Size - Offset, 0);

            if (Amount <= 0) {
                printf("Error: " + WSAGetLastError());
                break;
            } else {
                Offset += Amount;
            }
        }
        free(Buffer);
        closesocket(Sub);
        closesocket(Socket);
        WSACleanup();
        printf("File sent!\n");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您有三个问题,一个在客户端,两个在服务器中。

在客户端中,您通过Socket发送文件大小,这是正确的。但是,然后使用未连接和未初始化的套接字Sub来发送文件的数据。

在服务器中,您遇到了几乎相反的问题,即尝试通过被动侦听socket而不是接受的连接套接字Sub来接收大小和数据。

同样在服务器中,如果在读取报告数据时出现错误,则无论如何都要将数据写入文件。在这种情况下,它将是一些未知长度的未初始化数据。

使用正确的套接字,如果出现错误,则不要做不应该做的事情。

实际上做了一些错误检查。在服务器中收到大小时,您不会检查错误,只是连接的另一端没有关闭连接。