conutd 2022-10-26 09:34 采纳率: 50%
浏览 135
已结题

TCP客户端添加包头进行发送图片,导致图片打不开

TCP客户端添加包头进行发送图片,导致图片打不开,不加包头文件传输灭有问题;看不出来哪里出现问题了

客户端

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <thread>
#include <fstream>
#include <stdlib.h>
#include <fcntl.h>
#include "cs.h"

using namespace std;

#define PORT 8889
#define MSG_SIZE 4096
#define MAX_LINE 5
#define IP "127.0.0.1"


// typedef struct msg
// {
//     int len;
//     char buf[1024];
// }MSG;


// void recv_server(int fd,char *client_MSG)
// {
//     char buf[MSG_SIZE];
//     strcpy(buf,client_MSG);
//     while (1)
//     {
//         // 接收服务器的消息
//         int s_len=recv(fd,buf,sizeof(buf),0);
//         if(s_len <=0 )
//         {
//             perror("client recv err!");
//             exit(-1);
//         }

//         // server_MSG[s_len]='\0';

//         cout<<"recv msg form <--> "<<buf<<endl;
//     }
    
// }



int client()
{
    char client_MSG[MSG_SIZE], server_MSG[MSG_SIZE];

    //初始化TCP结构体
    struct sockaddr_in serverAddr;
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_port = (PORT);
    serverAddr.sin_family = AF_INET;

    if (inet_pton(AF_INET, IP, (void *)&serverAddr.sin_addr) <= 0)
    {
        perror("client inet_pton err!");
        return -1;
    }

    //创建socket套接字
    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_fd < 0)
    {
        perror("server socket err!");
        return -1;
    }

    // connect连接
    if (connect(socket_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
    {
        perror("client connect err!");
        return -1;
    }

    cout << "Connect Success!." << endl;
    cin.ignore(1024,'\n'); // 去除上一个cin残留在缓冲区的\n

    // thread th;
    // th = thread(recv_server,socket_fd , client_MSG);



    //打开照片的文件描述符
    // char yuv_data[3110];
    int fd=open("/home/nvidia/8.jpg",O_RDONLY);
    // int fd1=open("/home/nvidia/10.jpg", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if(fd <0&&fd1<0)
    {
        perror("open err!");
        return -1;
    }


    // MSG *myNode=(MSG*)malloc(sizeof(MSG));
    char buff[1024]={0};
    char buf[1024]={0x90,0x81};
    
    while (1)
    {
        
        //返回值实际读到的大小
        int len=read(fd,buff,sizeof(buff));//1024
        // len = 64;
        buf[2]=len&0xFF;
        buf[3]=(len>>8)&0xFF;
        buf[4]=(len>>16)&0xFF;
        buf[5]=(len>>24)&0xFF;
                             //1024
        strncpy(&buf[6],buff,len);

        char *p=&buf[6];

        // if(buf[0]==0x90&&buf[1]==0x81)
        // {
        //     int a = (buf[2]|(buf[3] << 8)|(buf[4] << 16)|(buf[5] << 24));
        //     if(a==len)
        //     {
        //         write(fd1, p, a);
        //     }
        // }
        

        if (0==len)
        {
            break;
        }
        

                              //1024
        if(send(socket_fd, buf, len, 0)<0)
        {
            perror("client send err!");
            return -1;
        }
        

#if 
#endif
    }
    close(fd);
    // close(socket_fd);
    // free(myNode);
}

服务器

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <thread>
#include <stdlib.h>
#include <fcntl.h>
#include "cs.h"

using namespace std;

#define PORT 8889
#define MSG_SIZE 4096
#define MAX_LINE 5

// typedef struct msg
// {
//     int len;
//     char buf[1024];
// }MSG;

// void send_client(int fd,char *client_MSG)
// {
//     char buf[MSG_SIZE];
//     strcpy(buf,client_MSG);
//     while (1)
//     {
//             //将服务器消息转发给客户端
//             if(send(fd,buf,strlen(buf),0) <= 0)
//             {
//                 perror("server send err!");
//                 exit(-1);
//             }

//             cout<<"send msg to  -->"
//                 <<buf<<endl;
//     }

// }

int server()
{
    char client_MSG[MSG_SIZE], server_MSG[MSG_SIZE];

    //初始化TCP结构体
    struct sockaddr_in serverAddr;
    serverAddr.sin_port = (PORT);
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    //创建socket套接字
    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_fd < 0)
    {
        perror("server socket err!");
        return -1;
    }

    // bind绑定套接字
    if (bind(socket_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
    {
        perror("server bind err!");
        return -1;
    }

    // listen被动监听
    if (listen(socket_fd, MAX_LINE) < 0)
    {
        perror("server listen err!");
        return -1;
    }

    cout << "Listening........\n";
    int client_fd;

    // thread th;
    // th = thread(send_client,socket_fd , client_MSG );

    int fd = open("/home/nvidia/Downloads/1.jpg", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (fd < 0)
    {
        perror("open err!");
        return -1;
    }

    // MSG *myNode=(MSG*)malloc(sizeof(MSG));
    while (1)
    {
        memset(client_MSG, 0, sizeof(client_MSG));
        memset(server_MSG, 0, sizeof(server_MSG));

        struct sockaddr_in clientAddr;
        socklen_t lens = sizeof(clientAddr);

        //返回用于通信的文件描述符
        client_fd = accept(socket_fd, (struct sockaddr *)&clientAddr, &lens);
        if (client_fd < 0)
        {
            perror("server accept err!");
            return -1;
        }

        cout << "server: connection from " << clientAddr.sin_addr.s_addr
             << ", port " << PORT
             << ", socket " << client_fd << endl;

        int a, b;
        while (1)
        {
            // char buff[1024] = {0};
            char buf[1024] = {0};
            int rd = recv(client_fd, buf, sizeof(buf), 0);
       

            

            char *p = &buf[6];

            if (buf[0] == 0x90 && buf[1] == 0x81)
            {
                int a = (buf[2]|(buf[3] << 8)|(buf[4] << 16)|(buf[5] << 24));
                cout << a << endl;
                // int rd = recv(client_fd, buf, sizeof(buf), 0);
                if (a == rd)
                {
                    cout<<"hello\n";
                    write(fd, p, rd);
                }
                
            }
            if (0==rd)
            {
                break;
            }
            

#if 0

#endif
        }

        //关闭通信套接字
        close(client_fd);

        cout << "当前客户端已结束通信,是否继续等待其他客户端?(1 - 是, 0 - 否)" << endl;
        bool isContinue = false;
        cin >> isContinue;
        if (!isContinue)
        {
            break;
        }
    }
    close(fd);
    close(socket_fd);
    return 0;
}


  • 写回答

3条回答 默认 最新

  • X-道至简 2022-10-26 10:06
    关注

    估计你是大小端搞错了吧, 数据在发送的时候是有大小端的,在客户端的len要转换为网络序发送,在接收相应的处理为主机序
    如果没有处理,数据是对不齐的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月3日
  • 已采纳回答 10月26日
  • 赞助了问题酬金15元 10月26日
  • 修改了问题 10月26日
  • 展开全部

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上