[英]client side on MAC OS X gets empty responses (using BSD sockets and Cocoa's NSFileHandle)
I'm connecting a linux machine (acting as server) and a Mac machine (as the client) using bsd sockets. 我正在使用bsd套接字连接linux机器(充当服务器)和Mac机器(充当客户端)。 Because of Cocoa's improve I'm setting bsd sockets the usual way but then encapsulating it on a NSFileHandle object. 由于Cocoa的改进,我以通常的方式设置bsd套接字,然后将其封装在NSFileHandle对象上。 I'm using this object to send and receive messages from the server. 我正在使用该对象来发送和接收来自服务器的消息。
The protocol goes as follow: Linux gets a message from the Mac, and immediately echoes that message back to Mac. 该协议如下:Linux从Mac收到一条消息,并立即将该消息回显给Mac。
Everything goes well 'till I get the message back. 一切顺利,直到我收到信息为止。 My mac gets the message empty. 我的Mac收到的消息为空。
Here is what I do, when sending the message. 发送邮件时,这就是我的工作。
const char *toSend = [line cStringUsingEncoding:NSASCIIStringEncoding];
printf("message to send %s\n", toSend);
//[self writeData:[line dataUsingEncoding: encoding]];
[self writeData:[NSData dataWithBytes:toSend length:strlen(toSend)]];
[self readInBackgroundAndNotify];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:observer selector:@selector(process:) name:NSFileHandleReadCompletionNotification object:self];
As you can see I set "process" on "observer" to process the server respond. 如您所见,我在“观察者”上设置了“过程”以处理服务器响应。 this method does the following. 此方法执行以下操作。
NSFileHandle *fh = [notification object];
NSData *data = [fh availableData];
NSString *dataString = [[NSString alloc]
initWithData:data
encoding:NSASCIIStringEncoding];
printf("server says %s\n", [dataString cStringUsingEncoding:NSASCIIStringEncoding]);
[dataString release];
On console I get: "server says " and that's all 在控制台上,我得到:“服务器说”,仅此而已
On the Linux side I'm printing what I get, and before sending it back I check I there's something. 在Linux方面,我正在打印得到的内容,然后将其发送回去之前,请检查是否有东西。
I know I can use bsd regular sockets on Mac but I really want to pull this of. 我知道我可以在Mac上使用bsd常规套接字,但我真的很想这么做。
Shouldn't you add the observer before calling readInBackgroundAndNotify? 在调用readInBackgroundAndNotify之前不应该添加观察者吗? Your message could be read and gone by the time your process gets around to doing that. 您的消息可以在过程完成时被读取和消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.