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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:14:52+00:00 2026-06-08T12:14:52+00:00

Im trying to send an image as a byte array over a socket to

  • 0

Im trying to send an image as a byte array over a socket to a client so it can view the image, and all goes well except when viewing the image its fragmented, i can view the top 10th of the image but the rest of it is grey and black pixels and i cant figure out why

heres a piece of my server code:

    public synchronized void nextFrame(VideoFrame frame) {
        // This method is called when a new frame is ready.
        // Don't forget to recycle it when done dealing with the frame.

        // draw the new frame onto the JLabel
            go = true;
            pic = frame.getBytes();
            go = false;
            label.getGraphics().drawImage(frame.getBufferedImage(), 0, 0, width, height, null);
            frame.recycle();

    }
}

class server extends Thread{
    int port;
    ServerSocket socket;
    Socket temps = null;
    boolean go = true;
    server(int p){
        port = p;
        start();
    }

    public void run(){      

        while(go == true){
            try {
                socket = new ServerSocket(port, 10);
                socket.setSoTimeout(10000);
                temps = socket.accept();
                new connect(temps);
                port += 1;
            } catch (IOException e) {
                if(e.getMessage().toString().equalsIgnoreCase("Accept timed out")){
                    go = false;
                }else{
                    e.printStackTrace();
                }
                break;
            }

        }
        try {
            socket.close();
            System.out.println("Closing socket server(no more connections will be created)");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class connect extends Thread{
    Socket connection;
    ObjectOutputStream out;
    int port;
    String host;
    GetInput in;
    connect(Socket s){
        try {
            connection = s;
            out = new ObjectOutputStream(connection.getOutputStream());
            host = connection.getInetAddress().getHostName();
            port = connection.getPort();
            System.out.println("Connected to " + host + ":" + port);
            in = new GetInput(connection);
            start();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }

    public void run(){
        try {
            out.writeInt(Main.pic.length);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        while(in.isAlive()){
            if(Main.go){    
                try {
                    out.write(Main.pic);
                } catch (IOException e) {
                        e.getMessage().toString();
                }
            }
        }
        try {
            out.close();
            connection.close();
            System.out.println("Closing " + host + ":" + port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class GetInput extends Thread{
    ObjectInputStream in;
    GetInput(Socket s){
        try {
            in = new ObjectInputStream(s.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        start();
    }
    public void run(){
        try{
            boolean go = in.readBoolean();
            int a = (go?1:0);
            System.out.println(a);
        } catch (IOException e) {
                e.getMessage().toString();
        }
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

i know the frame grabber is grabbing the image right because im also displaying the image on the server and it looks fine, so that means the byte arrays not getting sent over the sockets correctly, but why?

EDIT: heres my client code, its an android app

package org.smiley.doom;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class ClientActivity extends Activity implements View.OnClickListener{

    Socket s;
    InetAddress inet;
    ObjectOutputStream out;
    ObjectInputStream in;
    TextView log;
    ImageView im;
    Button send;
    EditText tip;
    int rport;
    String ip;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tip = (EditText) findViewById(R.id.etIP);
        send = (Button) findViewById(R.id.bSEND);
        im = (ImageView) findViewById(R.id.ivPIC);
        log = (TextView) findViewById(R.id.tvLog);

        s = null;
        in = null;
        out = null;

        send.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        switch(arg0.getId()){
            case R.id.bSEND:
                final int len;
                    try {
                        inet = InetAddress.getByName("192.168.0.2");
                        s = new Socket(inet, 4321);
                        in = new ObjectInputStream(s.getInputStream());
                        out = new ObjectOutputStream(s.getOutputStream());
                        log.setText("Client opened");
                        len = in.readInt();
                        new Thread(new Runnable(){

                            @Override
                            public void run() {
                                    byte[] b = new byte[len];
                                    try {
                                        in.read(b);
                                        log.setText(""+s.getReceiveBufferSize());
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    final byte[] l = b;
                                    im.post(new Runnable(){
                                        @Override
                                        public void run() {
                                            Bitmap bmp = BitmapFactory.decodeByteArray(l, 0, l.length);
                                            im.setImageBitmap(bmp);                             
                                        }
                                    });
                                    try {
                                        out.writeBoolean(true);
                                        out.close();
                                        in.close();
                                        s.close();
                                        //log.setText("Client closed");
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                            }

                        }).start();;
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            break;
        }
    }
}
  • 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-08T12:14:53+00:00Added an answer on June 8, 2026 at 12:14 pm

    TCP only delivers as many bytes to the application as currently available. Since the underlying network layer (IP) is packet based, your stream is getting chunked into parts during transmission. Network stack on the receiving side gets these chunks (packets) off the network card and buffers them until an application reads from given socket, at which point you get min of your buffer size and what’s piled up in the OS per-socket buffer.

    Because TCP connection represents a stream, the OS doesn’t know anything about your application messages, so it’s your responsibility to reassemble everything back together.

    You have to read from the socket in a loop until you know you have all your data. The easiest way to know that is to tell the client upfront how many bytes you are going to send.

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

Sidebar

Related Questions

I am trying to send an image over UDP using JAVA. I have managed
Im trying to send multiple data items over a tcp socket from an android
I am trying to Send the image to the Client which is connected to
I'm trying to send multiple images over a socket using java but I need
I'm trying to send an image as a Base64 encoded string to my PHP
am trying send an array to php file using $_POST ,still always getting an
Trying to send 3rd party scripts over an SSL or SPDY connection is a
im trying to send rest api Users.getLoggedInUser i have all the secret session and
I am trying to send an image from a Java desktop application to a
I am trying to Upload image Capture by Camera into server. server send response

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.