Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:58:38+00:00 2026-06-15T01:58:38+00:00

I have written a code for Server and Client in Java. Clients are able

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:58:40+00:00Added an answer on June 15, 2026 at 1:58 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written simple client server socket programming code as follow MyServer.java import java.io.DataInputStream;
I have a very simple client server code written java(server listens on some port
I have written code for server and client sockets. I have used threads using
Here is the code I have written a server and client. But when i
I have a server written to Java to service HTTP requests for various clients.
I have written the following code in the server side for a chat client
I have written a server is java here is the code: public mainClass() {
I have written a java server and here is the code: try { ss
I have java server that I am writing the server reads command from clients,
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.