【问题标题】:How to get my objective-c client to talk to my python server?如何让我的 Objective-C 客户端与我的 python 服务器通信?
【发布时间】:2016-07-07 00:14:24
【问题描述】:

所以我在搞乱一些 iOS 开发的东西,想尝试从 Xcode 模拟器发送一些字符串到我在 python 中运行的服务器。我从来没有谈过两种不同的编程语言,所以我觉得那会很酷。

我在网上关注了一些objective-c socket教程,这就是我想出的

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL,
                                   (CFStringRef)@"localhost",
                                   5321,
                                   &readStream,
                                   &writeStream);
NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;

[inputStream setDelegate:self];
[outputStream setDelegate:self];

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[inputStream open];
[outputStream open];

NSString *response  = [NSString stringWithFormat:@"hello from obj c"]; //", inputNameField.text];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];

然后,在服务器端,这是一个简单的循环,等待消息并应该打印它。

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = 'localhost'
port = 5321
s.bind((host, port))

s.listen(5)
print("Now Listening")
c, addr = s.accept()
print("Connected, Waiting for message...")
while True:
    data = c.recv(1024)
    print(">Recieved: " + str(data))

所以我以为[inputStream open]/[outputStream open]命令会对应python中的socket.accept()命令,然后connection.recv()方法会读取[outputStream write:]函数在蟒蛇。但情况似乎并非如此,因为我使用调试器逐步完成,直到 [outputStream write:data] 命令才触发 socket.accept() 命令。

那么正确的方法是什么?如何让目标 c 代码连接到 python 端的套接字,然后发送套接字可以接收的字符串。我尝试背靠背发送两个不同的字符串,但这就像 write 方法不会触发 recv() 函数。

提前谢谢大家!

【问题讨论】:

    标签: python ios objective-c sockets


    【解决方案1】:

    您的代码运行良好,因此我认为您可能会犯一个简单的错误。请务必检查以下内容:

    • 首先启动您的 python 脚本。从终端:python yourfilename.py
    • 在 python 服务器侦听后运行您的目标 c 代码确保您显示的代码实际得到执行(即您在项目中的哪个位置放置了此代码 sn-p?)
    • 确保使包含代码的目标 c 类符合 <NSStreamDelegate> 协议

    为了测试您的代码,我刚刚将您的目标 c 代码粘贴到标准 ViewController 类中的一个方法中,您在开始一个新的 Xcode 项目时获得。代码如下:

    在 ViewController.h 中:

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController<NSStreamDelegate>
    
    @end
    

    在 ViewController.m 中:

    #import "ViewController.h"
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
    
    [super viewDidLoad];
    
    [self sendNetworkCommunication];
    
    }
    
    
    -(void)sendNetworkCommunication {
    
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL,
                                       (CFStringRef)@"localhost",
                                       5321,
                                       &readStream,
                                       &writeStream);
    NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
    NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
    
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    
    [inputStream open];
    [outputStream open];
    
    NSString *response  = [NSString stringWithFormat:@"hello from obj c"]; //", inputNameField.text];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
    
    }
    
    @end
    

    然后我从终端运行 python 脚本,然后在模拟器中运行 Xcode 项目:

    您可以看到从 iOS 发送的消息。

    【讨论】:

    • 直到服务器说它正在侦听之后,我才开始目标 c 代码。目标 c 代码仅在 main 函数中运行。我有 99.99% 的把握将这个类声明为 NSStreamDelegate。我现在还在工作,要到今晚才能检查。在服务器端,我收到了有连接的消息,然后等待传入的命令。这是由尝试发送字符串“hello from obj c”的行触发的,但是实际的字符串似乎永远不会到达服务器。运行时是否得到不同的结果?
    • 是的。我得到了不同的结果。请查看更新的答案。
    • 感谢您的回答。当我回家时,我会经历它。我很高兴能玩弄它
    • 工作不写代码不就是最糟糕的吗?
    • 哈哈,我在工作中写了一些代码,但它没有修补个人项目那么有趣
    猜你喜欢
    • 2016-02-12
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    相关资源
    最近更新 更多