Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c TCP套接字通信问题_Objective C_Ios_Sockets_Tcp - Fatal编程技术网

Objective c TCP套接字通信问题

Objective c TCP套接字通信问题,objective-c,ios,sockets,tcp,Objective C,Ios,Sockets,Tcp,我是iOS编程的新手-我需要一些帮助来解决我的问题。。。 我花了很多时间试图找到一个解决这个问题的办法,但没有任何成功 我正在编写一个非常简单的应用程序(在iPad上),它将通过一些TCP命令发送到我的服务器。服务器已配置且工作正常。我可以用iPad上的pTerm连接到它,在通过原始TCP或telnet成功连接后,我可以将请求发送到我的服务器,如下所示: #100输入 而且很有效 但当我尝试在我的应用程序上执行此操作时,它不起作用,似乎是服务器的行尾信息有问题(通常通过按enter键完成)。 服

我是iOS编程的新手-我需要一些帮助来解决我的问题。。。 我花了很多时间试图找到一个解决这个问题的办法,但没有任何成功

我正在编写一个非常简单的应用程序(在iPad上),它将通过一些TCP命令发送到我的服务器。服务器已配置且工作正常。我可以用iPad上的pTerm连接到它,在通过原始TCP或telnet成功连接后,我可以将请求发送到我的服务器,如下所示:

#100
输入

而且很有效

但当我尝试在我的应用程序上执行此操作时,它不起作用,似乎是服务器的行尾信息有问题(通常通过按enter键完成)。 服务器配置在192.168.1.220端口2000上

在点击重置按钮后,它应该向服务器发送命令-但我在服务器端看不到任何东西

我的.h文件看起来:

#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

非常感谢您的帮助。

您正在初始化连接两次,请删除该行
[self initNetworkCommunication]
resetbutonclick
方法中执行。

有一件事我们会立即注意到,您没有任何流事件hadler,这很奇怪。你怎么知道已经建立了联系

-(void)stream:(NSStream*)stream handleEvent:(NSStreamEvent)eventCode


如果您愿意阅读这些文章,中的代码已经过测试并正常工作。

您是否可以通过使用附加库来避免此类问题?(上面的抽象)
我用。它非常简单舒适。

好的,我把它取下来了。我还检查了-发送了#100-但我不知道如何发送行尾字符-chr(13)。什么时候?所以它现在起作用了,但是你想发送另一个角色,对吗?chr(13)?
#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