可以实现字符串的传输,在向服务器传输图片存盘时,总是无法显示图片。下面代码是服务器和客户端传送图片的部分。求大神指定!
服务器端:
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;