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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:08:35+00:00 2026-06-10T02:08:35+00:00

in an iPhone app I send some data to a server via socket. The

  • 0

in an iPhone app I send some data to a server via socket. The app should receive an answer from the server (the problem is not on the server side). That’s my code:

-(void)sendLocation {

NSLog(@"sendLocation %@, %@", lat, lon);

NSString *imei = @"12345484654";


NSDictionary *sendData = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"0", @"type", 
                          imei, @"imei",
                          lat, @"lat",
                          lon, @"lon", 
                          nil];


NSLog (@"JSON: %@", (NSString*)sendData);
NSString *outJSON = [sendData JSONRepresentation];  




CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
        // CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.0.36", 12390, NULL, &writeStream);
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.0.36", 12390, &readStream, &writeStream);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];

NSData *data = [[NSData alloc] initWithData:[outJSON dataUsingEncoding:NSASCIIStringEncoding]];
NSString *end = @"\n###";
NSData *endData = [[NSData alloc] initWithData:[end dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
[outputStream write:[endData bytes] maxLength:[endData length]];
[outputStream close];

[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop]

                        forMode:NSDefaultRunLoopMode];
[outputStream release]; 



}

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
NSLog(@"got an event");
switch (eventCode) {
    case NSStreamEventHasSpaceAvailable:
        NSLog(@"None!");
        break;
    case NSStreamEventOpenCompleted:
        NSLog(@"Stream opened");
        break;
    case NSStreamEventHasBytesAvailable:
        if (aStream == inputStream) {
           uint8_t buffer[1024];
           int len;
           while ([inputStream hasBytesAvailable]) {
              len = [inputStream read:buffer maxLength:sizeof(buffer)];
              if (len > 0) {
                 NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                     if (nil != output) {
                          NSLog(@"%@",output);
                     }
              }
         }
     }





    [inputStream close];
    [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
    [inputStream release];
        break;  
    case NSStreamEventErrorOccurred:
          NSLog(@"CONNECTION ERROR: Connection to the host  failed!");
          break;
    case NSStreamEventEndEncountered:
          NSLog(@"Stream Closed");
          break;    
    default:
          break;
      } 
}

The log gives me:

2012-08-22 14:58:52.117 Second[1272:11603] got an event
2012-08-22 14:58:52.117 Second[1272:11603] Stream opened
2012-08-22 14:58:52.117 Second[1272:11603] got an event
2012-08-22 14:58:52.117 Second[1272:11603] None!

so something is happening to the Inputstream right? But theres never anygthing received. I’m also uncertain on where to close & release the Inputstream.
A hint on whats wrong with this would be appreciated 🙂

  • 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-10T02:08:36+00:00Added an answer on June 10, 2026 at 2:08 am

    i guess your code is not sending the data to server properly. it first tries to send data to server, because you release the outputstream it stops working. this is the code i wrote before for this event.

           case NSStreamEventHasSpaceAvailable: {
                    if(stream == outputStream) {
    
        // str is your string to send the server
            NSData* data = [str dataUsingEncoding:NSASCIIStringEncoding];
            int byteIndex = 0;
            uint8_t *readBytes = (uint8_t *)[data bytes];
            readBytes += byteIndex; // instance variable to move pointer
            int data_len = [data length];
            unsigned int len = ((data_len - byteIndex >= 1024) ?
                                1024 : (data_len-byteIndex));
            uint8_t buf[len];
            (void)memcpy(buf, readBytes, len);
            len = [outputStream write:(const uint8_t *)buf maxLength:len];
            byteIndex += len;
        }
      // release your outputstream
    break;
    }
    

    i can advice you same place for this ;

    this place is for communicating with tcp server.

    this place is for sending and getting with nsstream

    and my lastly, i advice you to use NSJSONSerialization to play with json objects.

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

Sidebar

Related Questions

i wish to send some data as array from my iphone app to server
I need to send some details to a server from my iphone app. I
I have some image data (jpeg) I want to send from my iPhone app
My iphone app getting json data from php server. 2 basic questions, just to
I am developing an iPhone app in which app needs to send some data
I use BSD socket in my app to send and receive data on iphone4(iOS4.1),there
I have a requirement where I need to send some data from my server
I've got an iPhone App that is supposed to send POST data to my
I'm looking to work on an iOS app that would need to send/receive data
How can I send an SMS message programatically from an iPhone app? I'm using

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.