0

I need some help to translate this code from Python to Objective C (for iOS).

I know how to create the socket in C but perhaps ther is a most elegant way to do it with cocoa classes (NSStream ?)

What is the equivalent of thats chr(0x00) and b64encode ?

if you guys have some knowledge in Python And Objective, thank you for your help !

new = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
new.connect((dst, 55000))

msg = chr(0x64) + chr(0x00) +\
    chr(len(base64.b64encode(src)))    + chr(0x00) + base64.b64encode(src) +\
    chr(len(base64.b64encode(mac)))    + chr(0x00) + base64.b64encode(mac) +\
    chr(len(base64.b64encode(remote))) + chr(0x00) + base64.b64encode(remote)

pkt = chr(0x00) +\
    chr(len(app)) + chr(0x00) + app +\
    chr(len(msg)) + chr(0x00) + msg
new.send(pkt)
4

2 回答 2

1

这是部分答案。它没有解决您关于套接字的问题,因为这是我在 Objective-c 方面没有经验的领域

至于你的另外两个问题:

相当于chr(0x00)什么?

的等价物chr(0x<num>)很简单'0xnum'。所以例如chr(0x00)变成'0x00'. 请注意,在这里使用单引号而不是双引号很重要,因为与 C/objective-c 中的 python 不同,它们具有不同的含义

b64encode 的等价物是什么?

好吧,似乎没有办法像 python 那样开箱即用地做到这一点。但是,在 SO wiki 上有一个指向此文章的条目。文章末尾有一些源代码的链接,您应该可以按原样使用。它通过使用类别来添加NSData允许您对 base64 字符串进行编码/解码的方法来工作。

于 2013-03-05T00:39:04.470 回答
1

此代码是有效的翻译。它当然可以改进,欢迎您提出意见,但它运行良好,并且完全符合 Python 代码的功能。

NSString * src = @"X.X.X.X";
NSString * mac = @"XX-XX-XX-XX-XX-XX";


const unsigned char byte64[] = {0x64};
const unsigned char byte00[] = {0x00};

NSString * srcString = [src base64EncodedString];
int srcDataLength = [srcString length];
char* srcDataLengthAsByte = (char*) &srcDataLength;

NSString * macString = [mac base64EncodedString];
int macDataLength = [macString length];
char* macDataLengthAsByte = (char*) &macDataLength;

NSString * remoteString = [remote base64EncodedString];
int remoteDataLength = [remoteString length];
char* remoteDataLengthAsByte = (char*) &remoteDataLength;

NSString * appString = [app base64EncodedString];
int appDataLength = [appString length];
char* appDataLengthAsByte = (char*) &appDataLength;

NSMutableData * msgData = [NSMutableData data];
[msgData appendBytes:byte64 length:1];
[msgData appendBytes:byte00 length:1];
[msgData appendBytes:srcDataLengthAsByte length:1];
[msgData appendBytes:byte00 length:1];
[msgData appendData:[srcString dataUsingEncoding:NSASCIIStringEncoding]];
[msgData appendBytes:macDataLengthAsByte length:1];
[msgData appendBytes:byte00 length:1];
[msgData appendData:[macString dataUsingEncoding:NSASCIIStringEncoding]];
[msgData appendBytes:remoteDataLengthAsByte length:1];
[msgData appendBytes:byte00 length:1];
[msgData appendData:[remoteString dataUsingEncoding:NSASCIIStringEncoding]];
int msgDataLength = [msgData length];
char* msgDataLengthAsByte = (char*) &msgDataLength;

NSMutableData * packet = [NSMutableData data];
[packet appendBytes:byte00 length:1];
[packet appendBytes:appDataLengthAsByte length:1];
[packet appendBytes:byte00 length:1];
[packet appendData:[appString dataUsingEncoding:NSASCIIStringEncoding]];
[packet appendBytes:msgDataLengthAsByte length:1];
[packet appendBytes:byte00 length:1];
[packet appendData:msgData];
[self send:packet];

和插座部分:

- (BOOL)connect
{
    struct sockaddr_in addr;
    sockfd = socket( AF_INET, SOCK_STREAM, 0 );
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr([TV_IP UTF8String]);
    addr.sin_port = htons(TV_PORT);
    return connect(sockfd, (struct sockaddr*)&addr, sizeof(addr))==0;
}

- (long)send:(NSData*)data
{
    long sent = send(sockfd, [data bytes], [data length], 0);

    VADebugLog(@"sent data:(%ld bytes) = [%@]",sent,[data description]);

    return sent;
}

-(void)close
{
    close(sockfd);
}

并包含来自: https://github.com/nicklockwood/Base64的 base64

#include <netinet/in.h>
#include <arpa/inet.h>
#import "Base64.h"
于 2013-03-05T03:58:31.620 回答