简体   繁体   中英

Java send to C via TCP

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.

TCP is stream oriented; it does not maintain write boundaries so the file name can be received together with the file size and some of the data, or you may have to call read twice to receive the entire file name.

You need to establish a protocol of some kind to take this into account: every variable-length field has to be preceded with its length, or has to be delimited by a character that's guaranteed to not be a part of the content. For example, first send the length of the file name, then the length of the file, and then the file content. When sending lengths, send a fixed number of characters so that the receiver can know how long the lengths are. Or send the file name, then a NUL character, then the file length, another NUL character, and then the file data.

The size of file size string will not be fixed, if you send a 1 byte file, you'll send only one byte. But on the C server side you are reading some fixed buffer for filesize_str and after recieving file size str. you efectively get some data that should belong to picture data.

To repair this you should rework your data recieving to variable length both for file name and file size. (Or at least for the filename, that could be long.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM