Lmikic 2015-04-28 03:26 采纳率: 58.3%
浏览 1938

linux下用socket,client向server传送图片

可以实现字符串的传输,在向服务器传输图片存盘时,总是无法显示图片。下面代码是服务器和客户端传送图片的部分。求大神指定!
服务器端:
unsigned char recPhotoBuff[100];

unsigned char sendPhotoBuff[100];
char recPhotoPath[50];
char recPhotoName[20];
FILE *recPhotoFp;
FILE *sendPhotoFp;

///*
unsigned char ch;
recv(clientfd,recPhotoName,100,0);
recPhotoName[strlen(recPhotoName)-1]='\0';
printf("You get an image: %s\n",recPhotoName);
printf("change the name you want: ");
scanf("%s",recPhotoName);
//printf("\n");
sprintf(recPhotoPath,"/socketTest/photo/server/receive/%s",recPhotoName);
if((recPhotoFp=fopen(recPhotoPath,"wb+"))==NULL)
{
printf("cannot open image %s\n",recPhotoName);
return 0;
}
while((n=recv(clientfd,recPhotoBuff,100,0))>0)
{
recPhotoBuff[strlen(recPhotoBuff)-1]='\0';
ch=atoi(recPhotoBuff);
fputc(ch,recPhotoFp);

}//while
  fclose(recPhotoFp);
  printf("The image is saved!\n");

//*/

close(clientfd);
close(sock_fd);

return 0;

服务器端:
char sendPhotoName[20];

char sendPhotoPath[50];
char recPhotoName[20];
char recPhotoPath[50];
FILE *sendPhotoFp;
FILE *recPhotoFp;
unsigned char sendPhotoBuff[100];
unsigned char recPhotoBuff[100];
unsigned char ch;
while(1)
{
printf("please input the name of the image you want to send: ");
scanf("%s",sendPhotoName);
send(sock_fd,sendPhotoName,strlen(sendPhotoName),0);
sprintf(sendPhotoPath,"/socketTest/photo/client/original/%s",sendPhotoName);
if((sendPhotoFp=fopen(sendPhotoPath,"rb"))==NULL)
{
printf("cannot open image %s\n",sendPhotoName);
return 0;
}
printf("please input the name of the receive image: ");
scanf("%s",recPhotoName);
sprintf(recPhotoPath,"/socketTest/photo/client/receive/%s",recPhotoName);
if((recPhotoFp=fopen(recPhotoPath,"wb+"))==NULL)
{
printf("cannot open image %s\n",recPhotoName);
return 0;
}
while((ch=fgetc(sendPhotoFp))!=EOF)
{
sprintf(sendPhotoBuff,"%d",ch);
int b=atoi(sendPhotoBuff);
fputc(b,recPhotoFp); //在客服端存盘,此部分可以实现

send(sock_fd,sendPhotoBuff,strlen(sendPhotoBuff),0);
//usleep(1000);

 }//while !

fclose(sendPhotoFp);
fclose(recPhotoFp);
printf("The image is sent!\n");

} //while 1
//*/

close(sock_fd);

return 0;

  • 写回答

2条回答 默认 最新

  • 91program 博客专家认证 2015-04-28 03:33
    关注

    建议你将发送的图片与接收后保存的图片的数据进行对比,看看有什么错误(不能显示肯定是有错误了)。
    知道错误后,再来分析代码。

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧