简体   繁体   English

如何在套接字发送的二进制文件中添加2个整数或字符串? TCP协议

[英]How to add 2 integers or Strings to a binary file sent by sockets? TCP

I am sending a binary file like a database or pdf file from client to sever TCP/IP by wifi. 我正在从客户端发送二进制文件,例如数据库或pdf文件,以通过wifi切断服务器TCP / IP。 The client is an Android tablet and the server is a windows 7 PC. 客户端是Android平板电脑,服务器是Windows 7 PC。

When sending the database or pdf, it is sent as a binary file. 发送数据库或pdf时,它将作为二进制文件发送。 How do I attach 2 short integers to this file? 如何将2个短整数附加到此文件? For example a tablet serial number and a file size. 例如平板电脑的序列号和文件大小。

So the 2 integers and the file itself is sent all together. 因此,这2个整数和文件本身一起发送。

The code I am using is shown here. 我正在使用的代码如下所示。

Server side code, for receiving file from client: 服务器端代码,用于从客户端接收文件:

 File file = new File("C:/DBfiles/database.db);
 FileOutputStream fostr = new OutputStream(file);
 BufferedOutputsTream bostr = BufferedOutputStream(fostr);

 InutStream is = socket.getInputstream();

 byte[] buffer = new byte[bufferSize];

 while((( count = is.read())>0)&&(connected));
 bostr.write(buffer, 0, count);

Client side code, for sending a file to the server: 客户端代码,用于将文件发送到服务器:

    File file = new File("/mnt/sdcard/database.db");
    long length = file.length();

    byte[] buffer = new byte[(int) length];

    FileInputStream fistr = new FileInputStream(file2);
    BufferedInputStream bistr = new BufferedInputStream(fis);
    BufferedOutputStream bostr = new BufferedOutputStream(socket.getOutputStream());

                int count;

                while ((count = bistr.read(buffer)) > 0) {
                    bostr.write(buffer, 0, count);
                }

You need to find the byte form of the string (byte for each character) and append them in buffer. 您需要找到字符串的字节形式(每个字符为字节)并将其附加到缓冲区中。 I believe the integer you are saying are just 'string' for of integer. 我相信您所说的整数只是整数的“字符串”。 It is not meant for airthematic sum. 它不是用于航空主题的总和。

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

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