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

  • Home
  • SEARCH
  • 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 4614668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:43:57+00:00 2026-05-22T01:43:57+00:00

I am currently trying to design a client-server application, something like this: the user

  • 0

I am currently trying to design a client-server application, something like this: the user connects to the server and when the authentication is OK the server send to the user some files. The problem is that those files are written in a single file (with my method).

Here is some code:

The function which transfers the file

public void processFile(int id, DataOutputStream oStream, Socket socket, int tip){
        String fileName;
        if(tip==0){
            fileName="File"+Integer.toString(Intrebari[id])+".txt";
        }else{
            fileName="Image"+Integer.toString(Intrebari[id])+".jpg";
        }
        byte[] buffer=null;
        int bytesRead = 0;
        FileInputStream file=null;

        try {
            file = new FileInputStream(fileName);
            buffer = new byte[socket.getSendBufferSize()];
            while((bytesRead = file.read(buffer))>0)
                {
                oStream.write(buffer,0,bytesRead);
                }
            file.close();
        } catch (IOException ex) {
           System.out.println(ex);
        }

    }

And the function that choose which file have to be send

private void send(int id) throws IOException {
        os.writeBytes("sendFile" + id+"\n");

        System.out.println("sendFile" + id);
        generatedFiles.processFile(id, os, comunicare, 0);
        if (generatedFiles.Imagini[id] == 1) {
            os.writeBytes("sendImage" + id+"\n");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(clientThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("sendImage" + id);
            generatedFiles.processFile(id, os, comunicare, 1);
        }
    }

I have to mention that os is DataOutputStream and comunicare is Socket type.

I think that the problem is that I combine writeBytes with write. Can anyone help me with this problem? How can I make the server and the client to receive both files and messages?

  • 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-05-22T01:43:58+00:00Added an answer on May 22, 2026 at 1:43 am

    I do something like this:

    Server -send Command
    Server -send File
    Client -confirms file sucessfull

    Server -send Command
    Server -send File
    Client -confirms file sucessfull
    …

    like this…supose you have a socket from a client, than you…

    socket.getOutputStream.write(“FILE: SomeFile.bin, SIZE: 872973493\r\n”.getBytes(“choose an encoding”))
    socket.getOutputStream.flush(); (you will only need to flush if you are expecting a server string response like: OK SEND ME THE FILE, IM READY, otherwise no need too)
    client reads and see that is a file and it has this bytes size, so it starts reading from socket.getInputStream untill it gets the length of file as expected.
    after this clients confirm he recived the file

    then server can send another file, you could do instead of FILE, use IMAGE: or anything you want. You just have to read the message from client side to see if it is a file or image

    Here are some functions that might help you:

    public static void readInputStreamToFile(InputStream is, FileOutputStream fout,
            long size, int bufferSize) throws Exception
    {
        byte[] buffer = new byte[bufferSize];
        long curRead = 0;
        long totalRead = 0;
        long sizeToRead = size;
        while(totalRead < sizeToRead)
        {
            if(totalRead + buffer.length <= sizeToRead)
            {
                curRead = is.read(buffer);
            }
            else
            {
                curRead = is.read(buffer, 0, (int)(sizeToRead - totalRead));
            }
            totalRead = totalRead + curRead;
            fout.write(buffer, 0, (int) curRead);
        }
    }
    
    
    public static void writeFileInputStreamToOutputStream(FileInputStream in, OutputStream out, int bufferSize) throws Exception
    {
        byte[] buffer = new byte[bufferSize];
        int count = 0;
        while((count = in.read(buffer)) != -1)
        {
            out.write(buffer, 0, count);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to read in an XML file, make some minor changes (alter
I'm currently trying out db4o (the java version) and I pretty much like what
I'm currently trying to pass a mono threaded program to multithread. This software do
I'm currently trying to build an application that inherently needs good time synchronization across
I am working on a server client application right now (just for learning purposes),
I'm having some problems with my WCF server and client connections. If I use
I'm currently trying to learn more about object oriented design in C++ (familiar with
I currently trying to find a solution, how to ensure that a test fails
I'm currently trying to build a personal website to create a presence on the
I'm currently trying to port a SIP stack library (pjSIP) to the PSP Console

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.