简体   繁体   English

Java通过TCP发送到C

[英]Java send to C via TCP

I would like to transfer image file to C server from android. 我想将图像文件从android传输到C服务器。 Sending filename -> filesize -> imagefile when I tried to send file size, it concatenate with imagefile. 当我尝试发送文件大小时,发送文件名-> filesize-> imagefile,它与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) 从Java端(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 C(服务器端)//接收文件名

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 结果为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; TCP是面向流的; 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. 它不保持写边界,因此可以将文件名与文件大小和某些数据一起接收,或者您可能必须调用两次read才能接收整个文件名。

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. 或发送文件名,然后发送一个NUL字符,然后发送文件长度,再发送另一个NUL字符,然后发送文件数据。

The size of file size string will not be fixed, if you send a 1 byte file, you'll send only one byte. 文件大小字符串的大小不会固定,如果您发送一个1字节的文件,则只会发送一个字节。 But on the C server side you are reading some fixed buffer for filesize_str and after recieving file size str. 但是在C服务器端,您在读取文件大小str之后,正在读取一些用于fileize_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.) (或者至少对于文件名来说可能很长。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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