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;
}