gpt4 book ai didi

c++ - 通过 C++ 套接字发送图像 (Linux)

转载 作者:行者123 更新时间:2023-12-03 21:05:02 24 4
gpt4 key购买 nike

我正在尝试通过套接字发送文件。我创建了一个适用于文件类型的程序,例如 .cpp , .txt , 和其他文本文件。但是二进制文件、图像( .jpg.png )和压缩文件,例如 .zip.rar没有正确发送。我知道这与文件的大小无关,因为我用大 .txt 进行了测试。文件。我没有看到问题。我收到所有发送的字节,但无法打开文件。大多数情况下,文件已损坏且无法查看。我通过谷歌搜索了答案,发现其他人也有同样的问题,但没有解决方案。因此,通过帮助我,您也在帮助其他需要解决方案的人。

  • 服务器代码

  • #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>

    int main ( int agrc, char *argv[] )
    {
    /******** Program Variable Define & Initialize **********/
    int Main_Socket; // Main Socket For Server
    int Communication_Socket; // Socket For Special Clients
    int Status; // Status Of Function
    struct sockaddr_in Server_Address; // Address Of Server
    struct sockaddr_in Client_Address;// Address Of Client That Communicate with Server
    int Port;
    char Buff[100] = "";
    Port = atoi(argv[2]);
    printf ("Server Communicating By Using Port %d\n", Port);
    /******** Create A Socket To Communicate With Server **********/
    Main_Socket = socket ( AF_INET, SOCK_STREAM, 0 );
    if ( Main_Socket == -1 )
    {
    printf ("Sorry System Can Not Create Socket!\n");
    }
    /******** Create A Address For Server To Communicate **********/
    Server_Address.sin_family = AF_INET;
    Server_Address.sin_port = htons(Port);
    Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
    /******** Bind Address To Socket **********/
    Status = bind ( Main_Socket, (struct sockaddr*)&Server_Address, sizeof(Server_Address) );
    if ( Status == -1 )
    {
    printf ("Sorry System Can Not Bind Address to The Socket!\n");
    }
    /******** Listen To The Port to Any Connection **********/
    listen (Main_Socket,12);
    socklen_t Lenght = sizeof (Client_Address);
    while (1)
    {
    Communication_Socket = accept ( Main_Socket, (struct sockaddr*)&Client_Address, &Lenght );

    if (!fork())
    {
    FILE *fp=fopen("recv.jpeg","w");
    while(1)
    {
    char Buffer[2]="";
    if (recv(Communication_Socket, Buffer, sizeof(Buffer), 0))
    {
    if ( strcmp (Buffer,"Hi") == 0 )
    {
    break;
    }
    else
    {
    fwrite(Buffer,sizeof(Buffer),1, fp);
    }
    }
    }
    fclose(fp);
    send(Communication_Socket, "ACK" ,3,0);
    printf("ACK Send");
    exit(0);
    }
    }
    return 0;
    }
  • 客户代码

  • #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>

    int main ( int agrc, char *argv[] )
    {
    int Socket;

    struct sockaddr_in Server_Address;
    Socket = socket ( AF_INET, SOCK_STREAM, 0 );
    if ( Socket == -1 )
    {
    printf ("Can Not Create A Socket!");
    }
    int Port ;
    Port = atoi(argv[2]);
    Server_Address.sin_family = AF_INET;
    Server_Address.sin_port = htons ( Port );
    Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
    if ( Server_Address.sin_addr.s_addr == INADDR_NONE )
    {
    printf ( "Bad Address!" );
    }
    connect ( Socket, (struct sockaddr *)&Server_Address, sizeof (Server_Address) );


    FILE *in = fopen("background.jpeg","r");
    char Buffer[2] = "";
    int len;
    while ((len = fread(Buffer,sizeof(Buffer),1, in)) > 0)
    {
    send(Socket,Buffer,sizeof(Buffer),0);
    }
    send(Socket,"Hi",sizeof(Buffer),0);

    char Buf[BUFSIZ];
    recv(Socket, Buf, BUFSIZ, 0);
    if ( strcmp (Buf,"ACK") == 0 )
    {
    printf("Recive ACK\n");
    }
    close (Socket);
    fclose(in);
    return 0;
    }

    最佳答案

    首先,您应该尝试将缓冲区增加到更大的范围。缓冲区越大,传输效率越高(只是不要夸大和消耗太多本地内存)。

    其次,您应该检查并使用读取和写入函数返回的长度。在您的所有读取和写入操作中,您只检查是否读取/写入了某些内容,但您应该在下一次写入/读取操作中使用此字节计数。例如,如果客户端报告它已读取 1 个字节,则您应该只将 1 个字节写入磁盘。此外,如果您从服务器读取了 1024 字节,您应该尝试将 1024 字节写入磁盘,这在该调用中可能不会发生,您可能需要再次调用写入来完成操作。

    我知道这听起来像很多工作,但这就是保证 I/O 操作的方式。所有读取和写入基本上都必须在它们自己的循环中完成。

    关于c++ - 通过 C++ 套接字发送图像 (Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3125080/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com