0

I am trying to send file from one client 1 to another Client 2 using an intermediate server. Both Client 1 and Client 2 are connected to server through network.For this, I have given IP-address of server to both clients. 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. 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)

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)

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){}
    }
}

2 Answers2

1

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."

dis.read(b2);

The read() method returns a value. You are ignoring it. The value can be -1, or a postive number between 1 and the buffer size. 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:

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

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. To read all of the data that was sent you need to write a loop that reads from the socket until read returns -1.

Joni
  • 108,737
  • 14
  • 143
  • 193