简体   繁体   中英

Concurrency Issue Using Multithreading in Java

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM