[英]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.
请在我的代码中找到可能的错误。
`
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){}
}
}
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){}
}
}
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: 其中
count
是read(),
返回的长度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.