简体   繁体   English

无法使用JAVA中的网络将文件从一台电脑传输到另一台电脑

[英]Unable to Transfer File from one pc to another using networking in JAVA

I am trying to send file from one client 1 to another Client 2 using an intermediate server. 我正在尝试使用中间服务器将文件从一个客户端1发送到另一个客户端2。 Both Client 1 and Client 2 are connected to server through network.For this, I have given IP-address of server to both clients. 客户端1和客户端2都通过网络连接到服务器。为此,我已经将服务器的IP地址提供给了两个客户端。 But I am unable to transfer the file properly due to some mistake in my code. 但是由于代码中的某些错误,我无法正确传输文件。 I am using the following code and its not working and at the Client 2 (receiver's) end, the file which is created is empty. 我正在使用下面的代码,它无法正常工作,并且在客户端2(接收方)端,创建的文件为空。 Kindly find the possible error in my code. 请在我的代码中找到可能的错误。

Server code 服务器代码

`
import java.net.*;
import java.io.*;

public class S1 {


public static void main(String[] args) {

try{

ServerSocket sc1=new ServerSocket(6988);
Socket c1=sc1.accept();
Socket c2=sc1.accept();



DataInputStream dis=new DataInputStream(c1.getInputStream());
int m=c1.getInputStream().available();
byte b2[]=new byte[m];
dis.read(b2);

DataOutputStream dos=new DataOutputStream(c2.getOutputStream());
dos.write(b2);
dos.flush();
dos.close();


}
catch(Exception e){}
}
}

Client 1 (Sender) 客户端1(发送方)

import java.io.*;
import java.net.*;

public class C11 {


public static void main(String[] args) {
    try{
        Socket c2=new Socket("127.0.0.1",6988);
        FileInputStream fis=new FileInputStream("f:/abc.jpg");
        File f1=new File("f:/abc.jpg");
        long l1=f1.length();
        int a=(int)l1;
        byte b1[]=new byte[a];
        DataInputStream dis=new DataInputStream(fis);
        dis.read(b1);
        DataOutputStream dout=new DataOutputStream(c2.getOutputStream());
        dout.write(b1);

    }
    catch(Exception e){}
    }

}

Client 2 (Receiver) 客户端2(接收方)

import java.io.*;
import java.net.*;

public class C22 {


public static void main(String[] args) {
    try{
        Socket c2=new Socket("127.0.0.1",6988);
        DataInputStream dis=new DataInputStream(c2.getInputStream());
        int m=c2.getInputStream().available();
        byte b2[]=new byte[m];
        dis.read(b2);
        FileOutputStream fout=new FileOutputStream("E:\\PRACTICE\\xyz.txt");
        DataOutputStream dos=new DataOutputStream(fout);
        dos.write(b2);
        dos.close();
    }
    catch(Exception e){}
    }
}

Usual problems. 常见问题。

nt m=c1.getInputStream().available();
byte b2[]=new byte[m];

From the Javadoc : "It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream." 来自Javadoc :“使用此方法的返回值分配旨在容纳该流中所有数据的缓冲区永远是不正确的。”

dis.read(b2);

The read() method returns a value. read()方法返回一个值。 You are ignoring it. 您无视它。 The value can be -1, or a postive number between 1 and the buffer size. 该值可以是-1,也可以是1到缓冲区大小之间的正数。 You're assuming the read filled the buffer. 您假设读取已填充缓冲区。 It isn't obliged to do that. 它没有义务这样做。

dout.write(b1);

That should be 那应该是

dout.write(b1, 0, count);

where count was the length returned by read(), and it should be in a loop: 其中countread(),返回的长度read(),应该在循环中:

while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}

Use this at both ends, with any buffer size greater than zero. 在两端使用此值,并且任何缓冲区大小都大于零。 I usually use 8192. 我通常使用8192。

The most obvious mistake is here: 最明显的错误是在这里:

 int m=c2.getInputStream().available();

The available method only tells you how much data can be read without blocking , not how much data could potentially be read from the socket. available方法仅告诉您可以不阻塞地读取多少数据,而不能告诉您可能从套接字读取多少数据。 To read all of the data that was sent you need to write a loop that reads from the socket until read returns -1. 要读取所有已发送的数据,您需要编写一个从套接字读取的循环,直到read返回-1。

暂无
暂无

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

相关问题 无法使用Java代码将文件从一个地方传输到另一个地方 - Cannot transfer a file from one place to another using java codes 使用AS2传输协议将文件从一台计算机传输到另一台计算机 - Transfer file from one machine to another using AS2 transfer protocol 如何将我的Eclipse Maven Selenium Webdriver TestNG Java项目从一台PC转移到另一台PC? - How to transfer my Eclipse Maven Selenium Webdriver TestNG Java project from one PC to another? 如何使用java将一个文件传输到另一个位置 - How to transfer one file to another location using java 如何使用java I / O将文本从一个文件传输到另一个文件 - How to transfer text from one file to another using java I/O 如何使用 Java SFTP 库 JSch 将文件从一个目录传输到另一个目录? - How do I transfer a file from one directory to another using Java SFTP Library JSch? 使用Java中的for循环将元素从一个列表传输到另一个列表 - Transfer elements from one list to another using for loop in Java 如何将文件从一台PC复制到另一台PC - How to copy file from one PC to another 如何在java中将资金从一个账户转移到另一个账户,使用每个所需账户的用户输入,以及转账金额 - How to transfer funds from one account to another in java, using user input for each desired account, and amount to transfer 使用套接字编程将文件从android传输到PC - Transfer file from android to pc using socket programming
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM