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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:10:52+00:00 2026-05-27T07:10:52+00:00

I’ve succesfully made the connection using TCP Sockets from C# to Java (Android). I

  • 0

I’ve succesfully made the connection using TCP Sockets from C# to Java (Android). I can send and receive string messages no problem. However when I try to receive a PNG image sent from C# Server, I get only black screen on Android Activity View.

Basically, the Server listens and waits until client sends a message. When the server receives the message, it will respond with sending an image to client.

C# Server:

    private void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)
        {
            bytesRead = 0;

            try
            {
                //blocks until a client sends a message
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                //a socket error has occured
                break;
            }

            if (bytesRead == 0)
            {
                //the client has disconnected from the server
                break;
            }

            //message has successfully been received. Now let's send an image.

            byte[] pic = new byte[5000*1024];
            pic = ImageConverter.imageToByteArray(System.Drawing.Image.FromFile("C:\\ic_launcher.png"));
            clientStream.Write(pic, 0, pic.Length);  
            clientStream.Flush();
        }

Android client:

Socket s = new Socket("192.168.1.154", 8888);

        DataInputStream ins = new DataInputStream(s.getInputStream());

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

        //Let's send output message

        String outMsg = "TCP connecting to " + 8888 + System.getProperty("line.separator"); 

        out.write(outMsg);

        out.flush();


        //Receive the image from server.
        int bytesRead;
        byte[] pic = new byte[5000*1024];
        bytesRead = ins.read(pic, 0, pic.length);


        //Decode the byte array to bitmap and set it on Android ImageView
        Bitmap bitmapimage = BitmapFactory.decodeByteArray(pic, 0, bytesRead);
        ImageView image = (ImageView) findViewById(R.id.test_image);
        image.setImageBitmap(bitmapimage);

        //Show in android TextView how much data in bytes has been received (for debugging)   
        String received;
        received= Integer.toString(bytesRead);
        test.setText(received);

        //close connection

        s.close();

Now received variable shows that ~3000 bytes have been transfered, while the image size is actually 4.147 bytes (shown in Windows Explorer), well that doesn’t sound right.

So anyway why the image does not show up in Android Activity? What am I missing here?

  • 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-27T07:10:53+00:00Added an answer on May 27, 2026 at 7:10 am

    First of all. TCP do not send messages, it sends byte streams. A byte stream that you Receive() can contain anything from a small part of a message to several messages. Do not assume that you’ll receive exactly one message with one Receive().

    You would have noticed that if you had proper error handling in your server. Don’t just ignore exceptions or disconnections. Handle them.

    The easiest way to solve your problem is to send a integer header which tells the length. Read that header and then continue call Read until the entire image have been transferred.

    The reason to why your string worked is that the message is tiny and that you didn’t transfer anything else directly after. Thus giving you the illusion of sending a message. (Try to send two messages directly after each other, Nagle algorithm should pack them together so that you’ll receive them in the same Read()).

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string

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.