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 7751145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:28:21+00:00 2026-06-01T11:28:21+00:00

In my project client send some text, some images to the server. Server need

  • 0

In my project client send some text, some images to the server. Server need to handle all these things i.e if text is there then it need to display on TestArea which is on frame and if image file is there then server need to store that image file on the computer.
I created one application which handle either string(text) or image file but I don’t know how to store all these things when these are send by the client at the same time.

I know when I add this code:
InputStream in = socket.getInputStream();

then all the data send by the client is in this InputStream
So how I identify up to this much range data is image file so need to store in Byte Array, up to this much is test or string so need to display in TextAreea.
If client send two or more images at a time to the server then how server understand up to this much data this is first image, up to this much data this is second image file.

I tried this code to send Images:

Client Code:

public void sendPhotoToServer(String str){ // str is image location
try {
    InputStream input = new FileInputStream(str);
    byte[] buffer=new byte[1024];
    int readData;
    while((readData=input.read(buffer))!=-1){
    dos.write(buffer,0,readData); // dos is DataOutputStream
    }
} catch (FileNotFoundException e) {

} catch (IOException e) {

}       
}

and this method is in the loop so client send all the images in his folder.

Now Server Code, this is in thread and while loop is there which listen client data every time:

public void run() {
while (true) {
        try {
            byte[] buffer = new byte[8192];
            fis = new FileOutputStream("C:\\"+(s1++)+".jpg"); // fis is FileOutputStream
            while ((count = in.read(buffer)) > 0){ //count is a integer and 'in' is InputStream
            fis.write(buffer, 0, count); 
            fis.flush();    
            }
            } catch (Exception e) {}
}
}

but by this code first image is received by the server after that it not show other images.

and here how server understand if simple string is send by the client.
In server side application it open one port to listen all the data send by the client by

FileOutputStream fis = socket.getInputStream();

now how it distinguish all these files and simple string.

  • 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-01T11:28:23+00:00Added an answer on June 1, 2026 at 11:28 am

    Why don’t you just use ObjectInputStream and ObjectOutputStream, if you know the datatypes in advance.

    Here’s sending:

    import java.io.*;
    
    public class Client {
    
      public static void main(String[] args) throws IOException {
        // write string
        ObjectOutputStream out = new ObjectOutputStream(
          new BufferedOutputStream(
            new FileOutputStream("test-string.data")));
        try {
          out.writeObject("Hello");
        } finally {
          out.close();
        }
        // write byte arrays
        out = new ObjectOutputStream(
          new BufferedOutputStream(
            new FileOutputStream("test-byteArrays.data")));
        try {
          out.writeObject(new byte[] { 'H', 'e', 'l', 'l', 'o' });
          out.writeObject(new byte[] { 'W', 'o', 'r', 'l', 'd' });
        } finally {
          out.close();
        }
      }
    
    }
    

    Here’s receiving:

    import java.io.*;
    import java.util.*;
    
    public class Server {
    
      public static void main(String[] args) throws IOException, ClassNotFoundException {
        // write string
        ObjectInputStream in = new ObjectInputStream(
          new BufferedInputStream(
            new FileInputStream("test-string.data")));
        try {
          Object o = null;
          while ((o = in.readObject()) != null) {
            System.out.printf("Class: %s, toString: %s\n", o.getClass(), o.toString());
          }
        } catch (EOFException e) {
          // finished
        } finally {
          in.close();
        }
        // write byte arrays
        in = new ObjectInputStream(
          new BufferedInputStream(
            new FileInputStream("test-byteArrays.data")));
        try {
          Object o = null;
          while ((o = in.readObject()) != null) {
            System.out.printf("Class: %s, toString: %s\n", o.getClass(), o.toString());
          }
        } catch (EOFException e) {
          // finished
        } finally {
          in.close();
        }
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a project for school I need to send xml data to the client
I have Client-Server environment and developed a project for Client-Server. I need to share
My project has both client and server components in the same solution file. I
I am starting a new client/server project at work and I want to start
I'm working on this project where the client has a virtual server setup. I
I have a client-server project (small project for companies in C#) and the server
I need some advice as to how to approach a project I am getting
I am programming a Client/Server application for my project in C++. The whole application
I'm trying to send data from client-side to server-side (asp.net c#), if you really
I'm currently working on a small project: there's a protocol for sending some strings

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.