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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:41:37+00:00 2026-06-10T22:41:37+00:00

i have a small problem sending image over Socket(client-server), i receive only UTF text

  • 0

i have a small problem sending image over Socket(client-server), i receive only “UTF” text but not the image object, is there something wrong with code?,

This code just sends the UTF txt and it’s received on server side, i’m using the UTF text to identify an image(name) on server side and after identified it can send the image Object to client

    /*
     * ServerSide
     * 
     */
    package Interface_class;

    import configuraciones.procesador;
    import java.awt.image.BufferedImage;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketTimeoutException;
    import javax.imageio.ImageIO;

    /**
     *
     * @author TheCoder
     */
    public class img_monitor extends Thread{
            ServerSocket serverSocket;
           Socket server;
           BufferedImage bimg;
            byte[] bytes;
           public img_monitor() 
           {
               try{
//Opening server socket
              serverSocket = new ServerSocket(6066);
              System.out.println("Conectado al Socket!");
    }
               catch(IOException ex){
                   System.out.println("Error en: "+ex);

              }
           }

           public void run()
           {
//This class gets the path from a property file (works well)
            procesador obj = new procesador();
            obj.UBARCHIVO_CONFIG();

               while(true)
              {
                   try
                   {

                      server = serverSocket.accept();
                      System.out.println("Nuevo cliente conectado!");
                      DataInputStream din=new DataInputStream(server.getInputStream());
                      DataOutputStream dout=new DataOutputStream(server.getOutputStream());                 
    //Receiving image "name" from client as txt
                      String nombre = din.readUTF();
                      System.out.println(nombre);
//Using path+name of image to identify and send over socket
                      bimg = ImageIO.read(new File(obj.getRuta_arch_img()+nombre));
                      System.out.println(obj.getRuta_arch_img()+nombre);
                      ImageIO.write(bimg,"JPG",dout);
                      System.out.println("Image sent!!!");
    //                  server.close();

                      //lblimg.setIcon(img);
                  }
                 catch(IOException e)
                 {
                      e.printStackTrace();
                      break;
                 }
                 catch(Exception ex)
                {
                      System.out.println(ex);
                }
              }
           }

    }

Client Side

    /*
     * Client Side 
     * 
     */
    package Interface_class;

    import java.awt.image.BufferedImage;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketTimeoutException;
    import javax.imageio.ImageIO;

    /**
     *
     * @author TheCoder
     */
    public class Obtener_imagen extends Thread {

        public Obtener_imagen() {
            System.out.println("Loading  Socket!");

        }

        public void run() {
//The class below gets info from database
            CLIENTINFO_CLASS obj = new CLIENTINFO_CLASS();
            obj.consulta();
            try {
                Socket socket = new Socket("localhost", 6066);
    //              DataInputStream din = new DataInputStream(socket.getInputStream());
                DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
               //Using this to send image "name" to server, so it can identify and send image to client
 dout.writeUTF(obj.getImg_name());
                BufferedImage img = ImageIO.read(ImageIO.createImageInputStream(socket.getInputStream()));
                System.out.println("Image received!!!!");
    //                socket.close();
            } catch (IOException ex) {
                System.out.println("Error al abrir el socket" + ex);
            }
        }
    }
  • 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-10T22:41:39+00:00Added an answer on June 10, 2026 at 10:41 pm

    You aren’t closing the socket after you send the image, so the client never stops reading. The server should close the socket output stream, and close the socket itself in a finally block.

    The client should also close his socket, in a finally block.

    EDIT: You don’t need to use ImageIO to read and write the image files at the server, unless you are, say, changing the format. Just copy the bytes.

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

Sidebar

Related Questions

Hi have implemented simple file exchange over a client/server connection in c++. Works fine
Okay so I have had a few small problems while sending data over WiFi.
have small problem, and would very much appreciate help :) I should convert byte
I have small problem with Spring MVC. Basically what I'm trying to do is
I have a small problem and I've figured out where when and why it
I have a small problem which i can't seem to figure out. I tried
I have a small problem which i can't seem to solve myself. Look at
I have a small problem with the visibility of an ellipse. In runtime I
I have a small problem with Smarty... I have two different template files in
I have a small problem where I want to find the next active item

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.