简体   繁体   English

从Java服务器向C客户端发送文本文件

[英]Sending a text file from Java server to C client

I am trying to send a text file from a Java server to a C client. 我正在尝试将文本文件从Java服务器发送到C客户端。 After I ran my code, the text file was received successfully but when i opened it, i found out that some random data had been inserted in the text file. 运行我的代码后,文本文件成功收到但是当我打开它时,我发现文本文件中插入了一些随机数据。

This is the server code for sending the file. 这是用于发送文件的服务器代码。

public void sendFile(Socket socket, String file) throws IOException 
{
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    FileInputStream fis = new FileInputStream(file);
    byte[] buffer = new byte[256];

    while (fis.read(buffer) > 0) {
        dos.write(buffer);
    }

    fis.close();
    dos.close();    
}

And this is the client code for receiving the file. 这是用于接收文件的客户端代码。

int recv_file(int sock, char* file_name)
{
     char send_str [MAX_SEND_BUF]; 
     int fp; 
     int sent_bytes, rcvd_bytes, rcvd_file_size;
     int recv_count; 
     unsigned int recv_str[MAX_RECV_BUF];
     size_t send_strlen; 
     send_strlen = strlen(send_str); 
     if ( (fp = open(file_name, O_WRONLY|O_CREAT, 0644)) < 0 )
     {
           perror("error creating file");
           return -1;
     }
     recv_count = 0;
     rcvd_file_size = 0;
     while ( (rcvd_bytes = recv(sock, recv_str, MAX_RECV_BUF/*256*/, 0)) > 0 )
     {
           recv_count++;
           rcvd_file_size += rcvd_bytes;
           if (write(fp, recv_str, rcvd_bytes) < 0 )
           {
                  perror("error writing to file");
                  return -1;
           }
           printf("%dThe data received is %u\n", ++count, recv_str);
     }
     close(fp);
     printf("Client Received: %d bytes in %d recv(s)\n", rcvd_file_size,    recv_count);
     return rcvd_file_size;
}

And this is the text file received at the client side. 这是客户端收到的文本文件。 Received text file 收到的文本文件

This gibberish is added to the text file, how do i resolve this issue? 这个乱码被添加到文本文件中,我该如何解决这个问题?

while (fis.read(buffer) > 0) {
    dos.write(buffer);
}

Your copy loop is incorrect. 您的副本循环不正确。 You are writing junk at the end of the file, if not before. 如果不是之前,你正在文件的末尾写垃圾。 It should be: 它应该是:

int count;
while ((count = fis.read(buffer)) > 0) {
    dos.write(buffer, 0, count);
}

You should not use a DataOutputStream because it provides no benefit here. 您不应该使用DataOutputStream因为它在这里没有任何好处。 Just use the plain OutputStream from the socket. 只需使用socket中的普通OutputStream

Then, you must make sure you only write as much data as there is in the file. 然后,您必须确保只写入文件中的数据。

So instead of 而不是

while (fis.read(buffer) > 0) {
    dos.write(buffer);
}

use 采用

OutputStream os = socket.getOutputStream();
int len;
while ( (len = fis.read(buffer)) > 0) {
    os.write(buffer,0,len);
}

to make sure you only write as many bytes as there are in the file. 确保只写入文件中的字节数。

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

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