Reputation: 11
I'm a newbie in iOS programming - and I need a little help with resolving my problem... I spent many hours trying to find a solution to this problem - but without any success...
I'm writing a very simple application (on iPad) which will send over a few TCP commands to my server. The server is already configured and working properly. I can connect to it with pTerm on my iPad, and after connecting succesfully over RAW TCP or telnet I can send the request to my server looking like that:
#100
enter
and it works..
But when Im trying to do it on my application - it doesn't work, it seems to be a problem with the end of line information to server (normally done by pushing enter key). Server is configured on 192.168.1.220, port 2000.
After clicking reset button it should send command to server - but I can't see anything on the server side...
My .h file looks:
#import <UIKit/UIKit.h>
NSInputStream *inputStream;
NSOutputStream *outputStream;
@interface ViewController : UIViewController <NSStreamDelegate>
@property (weak, nonatomic) IBOutlet UIButton *resetbutton;
@property (strong, nonatomic) IBOutlet UIView *viewController;
- (IBAction)resetbuttonclick:(id)sender;
@end
my .m file :
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize resetbutton;
@synthesize viewController;
- (void) viewDidLoad
{
[self initNetworkCommunication];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) viewDidUnload
{
[self setViewController:nil];
[self setResetbutton:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)initNetworkCommunication
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.220", 2000, &readStream, &writeStream);
inputStream = objc_unretainedObject(readStream);
outputStream = objc_unretainedObject(writeStream);
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
- (void)sendMessage
{
NSString *response = [NSString stringWithFormat:@"#100\n"];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
}
- (IBAction)resetbuttonclick:(id)sender
{
[self initNetworkCommunication];
[self sendMessage];
}
@end
Thank you very much for your help.
Upvotes: 1
Views: 1737
Reputation: 13
Maybe you can avoid such problems by using an additional library? (abstraction on top) I use CocoaAsyncSocket. It is very simple an comfortable.
Upvotes: 0
Reputation: 18865
One thing one immediately notices is you don't have any stream event hadlers - which is strange. How do you know that connection had been established?
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
The code in iPhone Network Programming is tested and working, if you're willing to read trough the articles.
Upvotes: 0
Reputation: 10104
You are initing the connection two times, remove the line [self initNetworkCommunication];
from the resetbutonclick
method.
Upvotes: 2