Reputation: 22008
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 :)
Upvotes: 1
Views: 8145
Reputation: 1885
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.
Upvotes: 1