I would like to transfer image file to C server from android. Sending filename -> filesize -> imagefile when I tried to send file size, it concatenate with imagefile. I am not sure why. I do not see difference between sending filename and filesize. When I send filename, it does not concatenate with filesize.
From java side(android)
DataOutputStream imgBodyOut = new DataOutputStream(client.getOutputStream());
//sending name of the file
buffer=fileName.getBytes();
imgBodyOut.write(buffer);
imgBodyOut.flush();
//sending size of the file
buffer=((Long.toString(filesize))+'\n').getBytes();
imgBodyOut.write(buffer);
imgBodyOut.flush();
//sending body
DataInputStream imgBodyIn = new DataInputStream(new FileInputStream(imgFile));
int len;
while(filesize >0&& (len=imgBodyIn.read(buffer))>0){
imgBodyOut.write(buffer,0,len);
imgBodyOut.flush();
filesize-=len;
}
imgBodyOut.flush();
imgBodyIn.close();
imgBodyOut.close();
C(server side) //receiving file name
read(connfd,filename_str,sizeof(filename_str));
printf("File name : %s.JPG\n",filename_str);
//receiving file size
read(connfd,filesize_str,sizeof(filesize_str));
printf("File size : %sbytes\n",filesize_str);
//Test filesize
int i=0;
while(i<1024){
printf("%c",filesize_str[i]);
i++;
}
Result in C
File name : P1011474.JPG
File size : 714438bytes
714438����8EExifII*
�����(1 �2i�&��2�OLYMPUS DIGITAL CAMERA OLYMPUS CORPORATIONX100,D540Z,C310ZHHv775u-770000:00:00 00:00:00PrintIM0250�
�
����������� '
'�'''^'�'�''!������"�'�d�0220��������
���� �
�|�H��}�0100�������������� �
�
�
Due to concatenation, I have trouble sending image. Thank you for reading.