简体   繁体   English

MAC OS X上的客户端获得空响应(使用BSD套接字和Cocoa的NSFileHandle)

[英]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.

相关问题 使用Cocoa / Objective-C,获取Mac OS X中当前连接的网络的安全类型 - Using Cocoa / Objective-C, get currently connected network's security type in Mac OS X 如何在Mac OS X中使用Cocoa和CGDisplayCreateImageForRect截取区域截图? - How to take region screenshot in Mac OS X using Cocoa and CGDisplayCreateImageForRect? 在Cocoa Mac OS X中查看范围和坐标 - View bounds and coordinates in Cocoa Mac OS X 这可以使用Cocoa在OS X上获得用户的高亮颜色吗? - Is this possible to get the User's highlight colour on OS X using Cocoa? 使用cocoa获取Mac OS X当前连接的wifi网络的安全类型 - Get security type of currently connected wifi network for Mac OS X using cocoa 在cocoa mac OS X应用程序中分享facebook和twitter中的文本? - Share text in facebook and twitter in cocoa mac OS X application? 在Mac OS X上将命令行应用程序转换为Cocoa GUI应用程序? - Turning a command line app into a Cocoa GUI app on Mac OS X? Aaron Hillegass _适用于Mac OS X的编程_第9章问题 - Aaron Hillegass _Cocoa Programming for Mac OS X_ Chapter 9 Question Cocoa contentsOfDirectoryAtPath:某些用户的方法因错误而失败 - Mac OS X - Cocoa contentsOfDirectoryAtPath: method failing with error for certain users - Mac OS X Mac OS X上的简单可可模态窗口对话框 - Simple Cocoa modal window dialog on Mac OS X
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM