I'm developing an iOS application which is using socket connection to share it's screen with another device. It's working well and it's sending the screenshots but the memory which the application is allocation keeps on growing and growing until it receives memory warning after several seconds and then it starts sending wrong data to the receiving server so that the server crashes. Here is the code I use right now:
The method in my main ViewController.m:
-(void) activateShareScreen {
newClient *nc = [[newClient alloc] init];
[nc connectToServerUsingCFStream];
dispatch_queue_t backgroundQueue = dispatch_queue_create("No", 0);
dispatch_async(backgroundQueue, ^{
while (true){
#autoreleasepool {
UIGraphicsBeginImageContext(mapView_.frame.size);
[mapView_.layer renderInContext:UIGraphicsGetCurrentContext()];
bitmap = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
[nc writeToServer:UIImageJPEGRepresentation(bitmap, 50)];
}
});
NSLog(#"Share screen button tapped");
}
And it is calling this file:
#import "newClient.h"
#implementation newClient {
NSInputStream *iStream;
NSOutputStream *oStream;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
BOOL boolean;
}
-(void) connectToServerUsingCFStream{
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
(CFStringRef) #"192.168.1.9",
8080,
&readStream,
&writeStream);
if (readStream && writeStream) {
//CFReadStreamSetProperty(readStream,
// kCFStreamPropertyShouldCloseNativeSocket,
// kCFBooleanTrue);
//CFWriteStreamSetProperty(writeStream,
// kCFStreamPropertyShouldCloseNativeSocket,
// kCFBooleanTrue);
iStream = (__bridge_transfer NSInputStream *)readStream;
oStream = (__bridge_transfer NSOutputStream *)writeStream;
// [iStream setDelegate:self];
//[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
// forMode:NSDefaultRunLoopMode];
[iStream open];
// [oStream setDelegate:self];
//[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
// forMode:NSDefaultRunLoopMode];
[oStream open];
// data = UIImageJPEGRepresentation(bitmap, 50);
boolean = YES;
(void) writeToServer:(NSData *) data{
int len;
uint32_t length = (uint32_t)htonl([data length]);
//[oStream write:(const uint8_t *) "/n" maxLength:2];
// Sending out the length of the screenshot image and then the image
len=[oStream write:(const uint8_t *)&length maxLength:4];
NSLog(#"len=%d",len);
//[oStream write:(const uint8_t *) "/n" maxLength:2];
len=[oStream write:(const uint8_t *)[data bytes] maxLength:[data length]];
NSLog(#"len=%d",len);
//[oStream write:(const uint8_t *)"/n" maxLength:0];
}
/*- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
{
switch(eventCode) {
case NSStreamEventHasSpaceAvailable:
if(stream == oStream) {
dispatch_async(dispatch_get_main_queue(), ^{
#autoreleasepool {
int len;
uint32_t length = (uint32_t)htonl([data length]);
// Sending out the length of the screenshot image and then the image
len=[oStream write:(const uint8_t *)&length maxLength:4];
NSLog(#"len=%d",len);
if (len<0) {
[oStream write:(const uint8_t *)[data bytes] maxLength:length];
}
[oStream write:(const uint8_t *)"/n" maxLength:0];
}
});
}
}
Am I the only one who has experienced that kind of problem? Am I using wrong methods? Any suggestions how to solve the problem?
Here's a screenshot of Instruments when I run the app in it:
#autoreleasepool {
while (true){
...
}
}
The autorelease is never done, it should be placed INSIDE the while loop
Related
The input stream works fine if data is sent after a short delay on server. The issue is occurring only when the server makes a response immediately after receiving data. I am handling both input and output streams in the same class.
Creating Streams:
// STREAMS
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
// CREATE READABLE/WRITABLE STREAMS TO HOST/PORT
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)terminalIp, [terminalPort intValue], &readStream, &writeStream);
// SET STREAMS
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
// SETTING DELEGATES
[inputStream setDelegate:self];
[outputStream setDelegate:self];
// SET LOOP TO LISTEN FOR MESSAGES
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
// OPEN STREAMS
[inputStream open];
[outputStream open];
Stream Delegate Event Handler:
////////////////////////////////////
// FUNCTION - STREAM EVENT HANDLER
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{
// LOG
if(aStream == outputStream){
NSLog(#"Status of outputStream: %lu", (unsigned long)[outputStream streamStatus]);
}else{
NSLog(#"Status of inputStream: %lu", (unsigned long)[inputStream streamStatus]);
}
//
// CHECK EVENTS
//
if(eventCode == NSStreamEventOpenCompleted){
//
// STREAM OPEN
//
}else if(eventCode == NSStreamEventHasBytesAvailable){
//
// SERVER SENT BYTES
//
// CHECK STREAM TYPE
if(aStream == inputStream){
// SETTINGS
uint8_t buffer[1024];
// LOOP WHILE THERE ARE BYTES BEING RECIVED
while ([inputStream hasBytesAvailable]){
// MAKE SURE THERE ARE BYTES
if([inputStream read:buffer maxLength:sizeof(buffer)] > 0){
// SET HEX RESPONSE
hexResponse = [[NSData alloc] initWithBytes:buffer length:[inputStream read:buffer maxLength:sizeof(buffer)]];
NSLog(#"hex in stream: %#",hexResponse);
NSString *incomingString;
if (hexResponse.length >= 2){
// TURN HEX INTO STRING
incomingString = [[NSString alloc] initWithData:hexResponse encoding:NSUTF8StringEncoding];
NSLog(#"%#", incomingString);
}
// MAKE SURE THERE IS TEXT THERE
if(incomingString.length > 3){
// SET TEXT
stringResponse = incomingString;
}
}
}
}
}else if(eventCode == NSStreamEventErrorOccurred){
//
// COULDN'T CONNECT
//
NSError *theError = [aStream streamError];
NSLog(#"Stream Error (%ld): %#",(long)[theError code],[theError localizedDescription]);
}else if(eventCode == NSStreamEventEndEncountered){
//
// END
//
// CHECK STREAM TYPE
if(aStream == outputStream){
NSData *newData = [outputStream propertyForKey:
NSStreamDataWrittenToMemoryStreamKey];
if (!newData) {
NSLog(#"No data written to memory from output stream!");
} else {
//[self processData:newData];
// CONVERT
//[self translateResponse];
}
[outputStream close];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
outputStream = nil;
}else if(aStream == inputStream){
NSData *newData = [inputStream propertyForKey:
NSStreamDataWrittenToMemoryStreamKey];
if (!newData) {
NSLog(#"No data written to memory from input stream!");
} else {
NSLog(#"Data found in input stream!");
//[self processData:newData];
// CONVERT
[self translateResponse];
}
[inputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
inputStream = nil;
}
}else if(eventCode == NSStreamEventHasSpaceAvailable){
//
// HAS SPACE TO SEND
//
if(aStream == outputStream){
NSMutableData * dataToSend = [NSMutableData dataWithData:hexRequest];
uint8_t *readBytes = (uint8_t *)[dataToSend mutableBytes];
readBytes += byteIndex; // instance variable to move pointer
NSUInteger data_len = [dataToSend length];
unsigned long 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;
}
}else{
//
// UNKNOWN EVENT
//
}
} // END FUNCTION
The problem was caused by reading the input buffer twice. I was reading it in a conditional statement as well as when I assigned it. The problem was fixed by assigning it first.
New Code:
}else if(eventCode == NSStreamEventHasBytesAvailable){
//
// SERVER SENT BYTES
//
// CHECK STREAM
if(aStream == inputStream){
// SSET BUFFER
uint8_t buffer[1024];
// LOOP WHILE THERE ARE BYTES BEING RECIVED
while ([(NSInputStream *)aStream hasBytesAvailable]){
// READ BUFFER
hexResponse = [[NSData alloc] initWithBytes:buffer length:[(NSInputStream *)aStream read:buffer maxLength:sizeof(buffer)]];
// MAKE SURE THERE IS DATA
if([hexResponse length] > 0){
// INIT INCOMING STRING
NSString *incomingString;
// MAKE SURE RESPONSE IS NOT A HEARTBEAT
if (hexResponse.length >= 2){
// TURN HEX INTO STRING
incomingString = [[NSString alloc] initWithData:hexResponse encoding:NSUTF8StringEncoding];
NSLog(#"%#", incomingString);
}
// MAKE SURE INCOMING STRING IS VALID
if(incomingString.length > 3){
// SET TEXT
stringResponse = incomingString;
}
}
}
}
}
I have this sample code taken from the example of this iPhone chat server.
In my case I need to send larger amounts of data, 100k-200k thus I changed the buffer size to something that can accommodate what I want.
On the iOS simulator (emulating a 6plus) everything works perfectly, as soon as I try to debug on my iPhone 6plus after implementing the suggestions of #tc. I get the incoming message broken into 3-4 parts! Any ideas?
2015-02-03 22:42:34.756 Sock1[7390:3773054] Incoming message: XML part 1
2015-02-03 22:42:34.759 Sock1[7390:3773054] Incoming message: XML part 2
2015-02-03 22:42:34.774 Sock1[7390:3773054] Incoming message: XML part 3
2015-02-03 22:42:34.794 Sock1[7390:3773054] Incoming message: XML part 4
XML part 1-4 make up the entire message that should have been in one as there is no null byte character within.
Just to make things even stranger, when I add a break point on this line:
[currentMessage appendBytes:buffer length:len];
and go through step by step (69-70 pressing Continue) everything works fine! No errors at all! So it is something to do with the speed of parsing or receiving or something I can't figure out?
What I've made sure is that the server doesn't send any null byte characters apart from the terminating one at the end of the message.
The code I use is this:
#implementation ViewController
bool connectionError = true;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)#"192.168.1.1", 6035, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
- (void)closeAll {
NSLog(#"Closing streams.");
[inputStream close];
[outputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream setDelegate:nil];
[outputStream setDelegate:nil];
inputStream = nil;
outputStream = nil;
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(#"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
connectionError = false;
if (theStream == inputStream) {
uint8_t buffer[1024];
long len;
NSMutableData *currentMessage;
currentMessage = [NSMutableData dataWithBytes:"" length:strlen("")];
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
[currentMessage appendBytes:buffer length:len];
}
NSString *newMessage = [[NSString alloc] initWithData:currentMessage encoding:NSUTF8StringEncoding];
NSLog(#"Incoming message: %#",newMessage);
NSData *nullByte = [NSMutableData dataWithLength:1];
len = currentMessage.length;
NSRange searchRange = {0, len};
for (NSRange r; (r = [currentMessage rangeOfData:nullByte options:0 range:searchRange]).length; ) {
NSString *message = [[NSString alloc] initWithBytes:currentMessage.bytes length:r.location encoding:NSUTF8StringEncoding];
searchRange.location = r.location+r.length;
searchRange.length = len - searchRange.location;
[self messageReceived:message];
}
[currentMessage replaceBytesInRange:(NSRange){0, searchRange.location} withBytes:NULL length:0];
}
break;
case NSStreamEventErrorOccurred:
NSLog(#"Can not connect to the host!");
connectionError = true;
[self closeAll];
[self connectionLost];
break;
case NSStreamEventEndEncountered:
break;
default:
NSLog(#"Unknown event");
}
}
- (void) connectionLost {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert!"
message:#"Connection to the server lost!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
- (void) messageReceived:(NSString *)message {
[messages addObject:message];
if (inputStream.streamStatus == NSStreamStatusClosed || inputStream.streamStatus == NSStreamStatusError || inputStream.streamStatus == NSStreamStatusNotOpen) {
[self closeAll];
[self initNetworkCommunication];
}
// do things with the message, XML parsing...
}
- (void) initConnection {
[self initNetworkCommunication];
messages = [[NSMutableArray alloc] init];
}
- (IBAction)joinChat:(id)sender {
[self initConnection];
[self sendSocketMessage: #"iam:" message: _inputNameField.text];
}
- (void) sendSocketMessage:(NSString*) sendCommand message:(NSString*) sendMessage
{
// do something...
if (outputStream.streamStatus == NSStreamStatusClosed || outputStream.streamStatus == NSStreamStatusError || outputStream.streamStatus == NSStreamStatusNotOpen) {
[self closeAll];
[self initNetworkCommunication];
}
NSString *response = [NSString stringWithFormat: #"%#%#", sendCommand, sendMessage];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
NSLog(#"clint sent: %#", response);
}
#end
The short answer is that TCP offers a stream of bytes, not a stream of variable-length messages¹. I don't know why it works in the simulator, unless 192.168.1.1 is the same machine (in which case it isn't subject to the usual flow control, though it's perhaps surprising that the kernel buffers are that big).
To work around this, you need to somehow signal where the end of a message is. There are two common ways:
Length-prefixes. Examples include DJB's netstrings (e.g. 5:abcde for the bytestring "abcde"), HTTP's Content-Length, HTTP 1.1's chunked encoding, and various binary protocols typically with 8-bit, 16-bit, or variable-length-encoded lengths.
Delimiters. Examples include newlines (traditionally CRLF, e.g. in Telnet/SMTP/HTTP/IRC), null bytes, MIME boundaries (ick).
In this case, we can make use of the fact that null bytes aren't permitted within XML. currentMessage is assumed to be a NSMutableData ivar and not nil.
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
[currentMessage appendBytes:buffer length:len];
}
NSData * nullByte = [NSMutableData dataWithLength:1];
len = currentMessage.length;
NSRange searchRange = {0, len};
for (NSRange r; (r = [currentMessage rangeOfData:nullByte options:0 range:searchRange]).length; ) {
NSString * message = [[NSString alloc] initWithBytes:currentMessage.bytes length:r.location encoding:NSUTF8StringEncoding];
searchRange.location = r.location+r.length;
searchRange.length = len - searchRange.location;
[self messageReceived:message];
}
[currentMessage replaceBytesInRange:(NSRange){0, searchRange.location} withBytes:NULL length:0];
Also note that initializing things in -viewDidLoad requires some care if the view can be unloaded (this is less of an issue if you only support iOS 6+, since views are no longer automatically unloaded).
I'm trying to send an image to the twisted server and back to my iPhone. My code works on the simulator but does not work on the iPhone. I have no idea why. All I'm doing is sending the data for the image to the server and then immediately back to my iPhone. Here is the relevant code that I'm using.
SERVER SIDE:
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
class IphoneChat(Protocol):
def connectionMade(self):
#self.transport.write("""connected""")
self.factory.clients.append(self)
print "clients are ", self.factory.clients
def connectionLost(self, reason):
self.factory.clients.remove(self)
def dataReceived(self, data):
#print "data is ", data
self.transport.write(data);
def message(self, message):
self.transport.write(message + '\n')
factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(80, factory)
print "Server Started"
reactor.run()
CLIENT SIDE:
#interface LoginScreen : UIViewController <NSStreamDelegate> {
}
#property (strong, nonatomic) NSMutableData *outputData;
#property (strong, nonatomic) IBOutlet UIImageView *testImage;
#implementation LoginScreen : UIViewController
- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)#"avis-mbp", 80, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
switch (streamEvent) {
case NSStreamEventOpenCompleted:
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream) {
uint8_t buffer[1024];
long len;
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSData *output = [[NSData alloc] initWithBytes:buffer length:len];
if (nil != output) {
[self.appDel.outputData appendBytes:buffer length:len];
}
}
}
}
break;
case NSStreamEventErrorOccurred:
NSLog(#"Can not connect to the host!");
break;
case NSStreamEventEndEncountered:
NSLog(#"Event Ended");
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
theStream = nil;
break;
default:
break;
}
}
-(IBAction)runNetworkingTest:(id)sender {
[self initNetworkCommunication];
NSData *pictureData = UIImagePNGRepresentation([UIImage imageNamed:#"shalin.jpg"]);
NSMutableData *mutedData = [[NSMutableData alloc] initWithData:pictureData];
[outputStream write:[mutedData bytes] maxLength:[mutedData length]];
}
-(IBAction)testPicture:(id)sender {
UIImage *image = [UIImage imageWithData:self.outputData];
self.testImage.image = image
}
I found the solution to my problem. It had to do with the space available when writing to the server. The iPhone can only write a specific amount of bytes at a time, so I had to regulate the amount of data that was written in the delegate via the NSStreamEventHasSpaceAvailable case. Here is the missing piece of code that will allow you to write an image to the server and read it back to the client via a TCP connection to a twisted server:
Missing Code to be put in the NSStream Delegate
case NSStreamEventHasSpaceAvailable:
{
if (self.appDel.willWrite && [self.appDel.inputData length] != 0) {
int bufferSize = 1024;
if ([self.appDel.inputData length] > bufferSize){
NSData *sendData = [self.appDel.inputData subdataWithRange:NSMakeRange(0, bufferSize)];
self.appDel.inputData = [[NSMutableData alloc] initWithData:[self.appDel.inputData subdataWithRange:NSMakeRange(bufferSize, [self.appDel.inputData length] - bufferSize)]];
[outputStream write:[sendData bytes] maxLength:[sendData length]];
} else {
[outputStream write:[self.appDel.inputData bytes] maxLength:[self.appDel.inputData length]];
self.appDel.inputData = [[NSMutableData alloc] init];
}
}
}
Modified version of the run networking test
-(IBAction)runNetworkingTest:(id)sender {
[self initNetworkCommunication];
self.appDel.willWrite = YES;
NSData *pictureData = UIImagePNGRepresentation([UIImage imageNamed:#"shalin.jpg"]);
[self.appDel.inputData appendData:pictureData];
}
Code to display the image
-(IBAction)showNetworkingArray:(id)sender {
UIImage *image = [UIImage imageWithData:self.appDel.outputData];
self.testImage.image = image;
}
Notice: I am writing 1024 bytes at a time. IT WILL NOT WORK IF YOU WRITE TOO MANY BYTES AT A TIME. For example, I tried 1024 * 8 and that did not work with a single image. However, when I set the buffer size to be 1024, I was able to send about ten images over without a problem.
I'm developing an application for iOS which is sharing its screen with another device through constant flow of screenshot images sent through TCP sockets connection.
Here's the code I'm written so far:
#implementation newClient {
NSData *data;
NSInputStream *iStream;
NSOutputStream *oStream;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
BOOL boolean;
}
-(void) connectToServerUsingCFStream:(UIImage *) bitmap {
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
(CFStringRef) #"192.168.1.9",
8080,
&readStream,
&writeStream);
if (readStream && writeStream) {
CFReadStreamSetProperty(readStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue);
iStream = (NSInputStream *)CFBridgingRelease(readStream);
[iStream setDelegate:self];
//[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
// forMode:NSDefaultRunLoopMode];
[iStream open];
oStream = (NSOutputStream *)CFBridgingRelease(writeStream);
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[oStream open];
// data = UIImageJPEGRepresentation(bitmap, 50);
boolean = YES;
[self writeToServer:bitmap];
}
}
- (void) writeToServer:(UIImage *)bitmap {
dispatch_async(dispatch_get_main_queue(), ^{
#autoreleasepool {
NSData *data = UIImageJPEGRepresentation(bitmap, 50);
int len;
uint32_t length = (uint32_t)htonl([data length]);
// Sending out the length of the screenshot image and then the image
len=[oStream write:(const uint8_t *)&length maxLength:4];
NSLog(#"len=%d",len);
len=[oStream write:(const uint8_t *)[data bytes] maxLength:[data length]];
NSLog(#"len=%d",len);
//[oStream write:(const uint8_t *)"/n" maxLength:0];
}
});
}
#end
When I click the button which is activating this object in sends the first screenshot and then crashes giving me "Thread 1: EXC_BAD_ACCESS(code=1, address=0xd000000c) in the main.m file of my project.
The server application is running on Android and it's a thread which is constantly running in a loop.
I don't know what is causing the problem.
Im writing an IOS application for my first time. It is supposed to connect to a static IP device, and send certain "known" commands to it. But for some reason Im unable to establish a connection.
Bellow are the functions I use to establish my connection, and write data to the port.
-(void)connection//:(NSString *)serviceName forIpAddress:(NSString *)ipAddress forPort:(NSString *)portNo
{
if(input && output)
[self close];
NSString *urlString = [NSString stringWithFormat:#"%.%.%.%", "192.168.3.120"];
NSURL *website = [NSURL URLWithString:urlString];
if (!website) {
NSLog(#"%# is not a valid URL", website);
}
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)[website host], 43, &readStream, &writeStream);
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
NSInputStream *input = (__bridge NSInputStream *)readStream;
NSOutputStream *output= (__bridge NSOutputStream *)writeStream;
}
- (void)open {
[input setDelegate:self];
[output setDelegate:self];
[input scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[output scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode]; [input open];
[output open];
}
-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
NSString *event;
switch (streamEvent)
{
case NSStreamEventNone:
event = #"NSStreamEventNone";
break;
case NSStreamEventOpenCompleted:
event = #"NSStreamEventOpenCompleted";
break;
case NSStreamEventHasBytesAvailable:
event = #"NSStreamEventHasBytesAvailable";
if (theStream == input)
{
uint8_t buffer[1024];
NSInteger len;
while ([input hasBytesAvailable])
{
len = [input read:buffer maxLength:1024];
if (len > 0)
{
NSMutableString *output = [[NSMutableString alloc]initWithBytes:buffer length:len encoding:NSUTF8StringEncoding]; NSLog(#"Received data--------------------%#", output);
}
}
}
break;
case NSStreamEventHasSpaceAvailable:
event = #"NSStreamEventHasSpaceAvailable";
break;
case NSStreamEventErrorOccurred:
event = #"NSStreamEventErrorOccurred";
//[self close];
break;
case NSStreamEventEndEncountered:
break; default:
event = #"NSStreamEventEndEncountered";
//[self close];
event = #"Unknown"; break;
}
NSLog(#"event------%#",event);
}
- (void)close
{
[input close];
[output close];
[input removeFromRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[output removeFromRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[input setDelegate:nil];
[output setDelegate:nil];
input = nil;
output = nil;
}
- (void)dataSending:(NSString*)data
{
if(output)
{
if(![output hasSpaceAvailable])
return;
NSData *_data=[data dataUsingEncoding:NSUTF8StringEncoding];
NSInteger data_len = [_data length];
uint8_t *readBytes = (uint8_t *)[_data bytes];
int byteIndex=0;
unsigned int len=0;
while (TRUE)
{
len = ((data_len - byteIndex >= 40960) ? 40960 : (data_len-byteIndex));
if(len==0)
break;
uint8_t buf[len];
(void)memcpy(buf, readBytes, len);
len = [output write:(const uint8_t *)buf maxLength:len];
byteIndex += len;
readBytes += len;
}
NSLog(#"Sent data----------------------%#",data);
}
}
I do call the mentioned functions through that code as a test, and nothing happens
- (IBAction)pumpchange:(id)sender {
[self connection];
[self open];
if ([self.pump backgroundImageForState:(UIControlStateNormal)]==[UIImage imageNamed:#"PumpOff.png"])
{
[self.pump setBackgroundImage:[UIImage imageNamed:#"PumpOn.png"] forState:(UIControlStateNormal)];
[self dataSending:#"pump_on"];
}
else //if ([self.pump backgroundImageForState:(UIControlStateNormal)]==[UIImage imageNamed:#"PumpOn.png"])
{
[self.pump setBackgroundImage:[UIImage imageNamed:#"PumpOff.png"] forState:(UIControlStateNormal)];
[self dataSending:#"pump_off"];
}
[self close];
}
Thanks in Advance
There seem to be some misunderstandings how format strings work, because
NSString *urlString = [NSString stringWithFormat:#"%.%.%.%", "192.168.3.120"];
just gives you the string #"...". Perhaps you meant
NSString *urlString = [NSString stringWithFormat:#"%d.%d.%d.%d", 192, 168, 3, 120];
or
NSString *urlString = [NSString stringWithFormat:#"%s", "192.168.3.120"];
But you don't need a format string at all:
NSString *urlString = #"192.168.3.120";