如何让objective-c客户机与python服务器对话?

如何让objective-c客户机与python服务器对话?,python,ios,objective-c,sockets,Python,Ios,Objective C,Sockets,因此,我正在处理一些iOS开发人员的东西,想尝试从Xcode模拟器向我用python运行的服务器发送一些字符串。我从来没有用过两种不同的编程语言,所以我觉得那会很酷 我在网上学习了一些objective-c套接字教程,这就是我想到的 CFReadStreamRef readStream; CFWriteStreamRef writeStream; CFStreamCreatePairWithSocketToHost(NULL,

因此,我正在处理一些iOS开发人员的东西,想尝试从Xcode模拟器向我用python运行的服务器发送一些字符串。我从来没有用过两种不同的编程语言,所以我觉得那会很酷

我在网上学习了一些objective-c套接字教程,这就是我想到的

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()命令,然后python中的connection.recv()方法将读取[outputStream write:]函数。但情况似乎并非如此,因为我使用了调试器,直到[outputStream write:data]命令才触发socket.accept()命令

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


提前谢谢大家

您的代码运行正常,因此我认为您可能犯了一个简单的错误。确保检查以下各项:

  • 首先启动python脚本。从终端:
    pythonYourFileName.py
  • 在python服务器侦听之后运行objective c代码,并确保实际执行了所显示的代码(即,您在项目中的何处放置了此代码片段?)
  • 确保包含代码的objective c类符合
    协议
为了测试您的代码,我刚刚将您的目标c代码粘贴到启动新Xcode项目时获得的标准ViewController类中的方法中。代码如下:

在ViewController.h中:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<NSStreamDelegate>

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


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

在服务器表示正在侦听之前,我没有启动objective c代码。目标c代码只在主函数中运行。我99.99%确定我用NSStreamDelegate声明了这个类。我现在还在工作,要到今晚才能检查。在服务器端,我得到一条有连接的消息,然后等待传入的命令。这是由尝试发送字符串“hello from obj c”的行触发的,但是实际字符串似乎从未发送到服务器。运行时是否会得到不同的结果?是的。我得到了一个不同的结果。请看更新的答案。谢谢你的回答。当我到家的时候我会经历的。我很兴奋能玩它,这不是工作中最糟糕的事情,而且无法编码吗?哈哈,我在工作中也做过一些编码,但这没有修补个人项目那么有趣
#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