0

I have written a code for Server and Client in Java. Clients are able to download the files from the server and the server should also be able to provide files to clients Concurrently. For this purpose I have used multithreading in Server.

It is working perfectly fine for one client but while using threads for every client it seems to be not working properly in my Code.

As the files are not getting downloaded correctly, are corrupt and are of varying sizes for different clients.

After accepting from a client, I am creating a new Thread for serving it the file Code for server -

public static void send(String pathname) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException
{

    try
    {
    System.out.println("....");

    byte[] buf = new byte[1024];
    OutputStream os = sock.getOutputStream();
    //PrintWriter writer = new PrintWriter(os);
    System.out.println("...11.");

    BufferedOutputStream out = new BufferedOutputStream(os, 1024);
    int i=0;


    System.out.println("hi1");
    File fp = new File(pathname);
    System.out.println("hi2");
    RandomAccessFile ra = new RandomAccessFile(fp,"r");
    System.out.println("hi3");
    long bytecount=1024;
    ////////////////

    while((i=ra.read(buf, 0, 1024)) != -1)
    {

    System.out.println("hi");

        bytecount += 1024;
        System.out.println("hi6");
        out.write(buf, 0, i);
        System.out.println("hi7");
        out.flush();
    }
    System.out.println("bye");
    //os.flush();
    //out.close();
    ra.close();
    //sock.close();
    }
    catch(IOException ex)
    {

    }
    }

And code for client for file receiving is

    public void run() {
        try{
            byte[] b = new byte[1024];
            int len = 0;
            long  bytcount = 1024;

            File fp = new File(path);
            RandomAccessFile ra=new RandomAccessFile(fp,"rw");
            ra.seek(0);
            InputStream is = sock.getInputStream();
            BufferedReader reader=new BufferedReader(new InputStreamReader(is));
            while ((len = is.read(b, 0, 1024)) != -1) {
                  bytcount = bytcount + 1024;

                  //decrypt

                  ra.write(b, 0, len);

            }
            //is.close();
            //ra.close();
            //sock.close();

        }
            catch(IOException ex){
            ex.printStackTrace();
            }

//        throw new UnsupportedOperationException("Not supported yet.");
    }


}

I am not getting out what's wrong here. Please help Many many thanx in advance

5
  • Please let me know why different sockets are interfering with each other so that file size is double for 1 client and 0 for other Commented Nov 26, 2012 at 20:58
  • you're clearly doing something with encryption over the wire as well. are you managing some other data on the stream other than the encrypted data? you aren't sharing any encryption structures among the threads are you? Commented Nov 26, 2012 at 21:05
  • yes i was using DES for encryption, but right now i am not using any encryption neither any other data structure except for receiving the file Commented Nov 26, 2012 at 21:11
  • How sure are you that only one thread writes to each socket? Commented Nov 26, 2012 at 21:16
  • I am saying that it seems that any thread is writing to any socket Commented Nov 26, 2012 at 21:22

2 Answers 2

3

send is static, which suggests to me that you have only one socket, named sock, or the following would not compile.

 public static void send(String pathname) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException
{

    try
    {
    System.out.println("....");

    byte[] buf = new byte[1024];
    OutputStream os = sock.getOutputStream();
    //PrintWriter writer = new PrintWriter(os);
    System.out.println("...11.");

If that is the case, it's hard to see how delivery to >1 client will be reliable. You will surely need one socket per client to perform concurrent deliveries? More code is needed to be sure of this, but it looks like the design of socket handling may need to be revised.

Sign up to request clarification or add additional context in comments.

Comments

2

Your first problem is that you are using an OutputStream on the server and a Reader on the client. the client should be using InputStreams, not Readers.

1 Comment

But what else can I use except OutputStream while sending the file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.