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