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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:09:18+00:00 2026-05-11T16:09:18+00:00

I want to send the data of a UIImage to the server through a

  • 0

I want to send the data of a UIImage to the server through a socket, so I:

a) open NSOutputStream


- (IBAction)send:(id)sender {
    NSURL *website = [NSURL URLWithString:str_IP];
    NSHost *host = [NSHost hostWithName:[website host]];
    [NSStream getStreamsToHost:host port:1100 inputStream:nil outputStream:&oStream];
    [oStream retain];
    [oStream setDelegate:self];
    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [oStream open];
}

b) write data to oStream when space is available


- (void) stream: (NSStream *) stream handleEvent: (NSStreamEvent) eventCode
{
switch(eventCode)
    {
    case NSStreamEventHasSpaceAvailable:
    {
            if(stream == oStream)
            {
                    NSData *data = UIImageJPEGRepresentation(drawImage.image, 90);
                    //Convert from host to network endianness
                    uint32_t length = (uint32_t)htonl([data length]);

                    [oStream write:(uint8_t *)&length maxLength:4];
                    [oStream write:[data bytes] maxLength:length];
            }
    }
    break;
}

The problem is that the data keeps writing to the oSream again and again. How to stop it when all the data is sent?

I send the length of the data before the Image data in order to let the server know when it reaches the end. Do I have to change the code in the server side to close the socket when it reaches the end? And where can I add [oStream close] to close the oStream in the client side?

  • 1 1 Answer
  • 1 View
  • 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-11T16:09:18+00:00Added an answer on May 11, 2026 at 4:09 pm

    The problem is that the data keeps writing to the oSream again and again. How to stop it when all the data is sent?

    See, this is the danger of thinking in the passive voice. Reword the question in the active voice:

    The problem is that the data keeps writing to the oSream again and again. How to stop it when I have sent all the data?

    And you have almost answered your own question.

    A large part of the problem is your HasSpaceAvailable handler. Here’s what you’re doing:

    1. Creating a complete JPEG representation of the image (every time)
    2. Sending the length of the representation (every time)
    3. Sending the first portion of the representation (every time)

    These are three problems and you need to solve all three.

    1. Create the JPEG representation once.
    2. Send the length of the representation only once.
    3. Keep track of what you have already sent and only send the first part of what you haven’t.

    To do all three, put the length into an NSMutableData and append the JPEG representation to it. Then, retain this new data object, and have additional instance variables for the bytes pointer and the length remaining. You’ll do all this before you get to the HasSpaceAvailable state.

    Then, when you do get to HasSpaceAvailable, don’t separately send the length, because you’ve put it into the data object. You need only send the bytes from your pointer instance variable with the length remaining as the max length. After you check that the number of bytes sent was not 0 or -1 (see the documentation), add it to the pointer and subtract it from the length remaining.

    When the length remaining reaches zero, you have sent everything. Assuming you don’t want to follow the JPEG data up with something, you can then close the stream.

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

Sidebar

Related Questions

I want to send a UIImage to a server through a socket. a)I open
I want to send data back to my main view when popping -(IBAction)addPartToBuild:(id)sender{ [self.navigationController
I want to send data through URL.I've tried the below code: Intent browserIntent =
Hi I want to send data to the server and then have the php
Hello Sir i want send list of data to php server i use following
I want to send data to server when user fill ups the from in
I want to send data to server from client(Android), below is my format http://101.34.45.45/rawData?data={userId:guest1,timestamp:2010-07-01
I'm trying to send data to a remote PHP website. I want to send
I want to send data from a c# application to a c++ application through
I just want to send data using ajax post request to a remote server.

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.