如何让我的objective-c客户端与我的python服务器通信?

时间:2016-07-07 00:14:25

标签: python ios objective-c sockets

所以我正在搞乱一些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()命令,然后[outputStream write:]函数将被connection.recv()方法读取在python中。这似乎并非如此,因为我逐步使用调试器并且直到[outputStream write:data]命令才触发socket.accept()命令。

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

先谢谢大家!

1 个答案:

答案 0 :(得分:1)

您的代码工作正常,所以我认为您可能会犯一个简单的错误。请务必检查以下内容:

  • 首先启动你的python脚本。来自终端:python yourfilename.py
  • 在python服务器监听之后运行您的目标c代码并确保您显示的代码实际执行(即您的项目在哪里放置了此代码片段?)< / LI>
  • 确保使包含代码的目标c类符合<NSStreamDelegate>协议

为了测试您的代码,我只是将您的目标c代码粘贴到您在启动新Xcode项目时获得的标准ViewController类中的方法中。这是代码:

在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项目:

enter image description here

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