在 iPhone 上,syncsocket 上的 writeData 总是阻塞
我使用 asyncsocket 示例作为起点来了解有关 iPhone 上的 wlan 通信的更多信息。
在 Mac 上,我启动了一个打开端口 0 的示例服务器。这很有效,因为我可以使用在 Mac 上运行的测试客户端写入数据。
在 iPhone 上,我想我成功连接了,因为“流已连接”返回 YES。
然后我想用同步套接字发送数据:(带有完整代码的编辑版本)
导入“InterfaceTestAppDelegate.h”
导入“InterfaceTestViewController.h”
导入“AsyncSocket.h”
导入“SyncSocket.h”@implementation
InterfaceTestAppDelegate
@synthesize window;
@synthesize viewController;
-
(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)remoteHost 端口:(UInt16)remotePort { NSLog(@"套接字已连接!");
NSLog(@"远程地址:%@:%hu",remoteHost,remotePort);
NSString *localHost = [sock localHost]; UInt16 localPort = [sock localPort];
NSLog(@"本地地址:%@:%hu", localHost, localPort); (void)onSocket:(AsyncSocket *)sock didConnectToHost:( NSString
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSLog(@"application:didFinishLaunchingWithOptions:");
/* asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
NSError *err = nil; if (![asyncSocket connectToHost: @"192.168.0.30" onPort: 1234 错误: &err]) { NSLog(@"连接错误:%@", err); } NSData *data = [@"testxyz" dataUsingEncoding:NSUTF8StringEncoding]; NSLog(@"轨迹 1"); [asyncSocket writeData:data withTimeout:10 标签:0]; NSLog(@"轨迹 2");
*/ syncSocket = [[SyncSocket alloc] initWithTimeout: 10]; syncSocket.nsLog = YES; if (![syncSocket connectToHost: @"192.168.0.30" onPort: 12345]) { NSLog(@"连接syncSocket时出错:"); } NSData *data = [@"testxyz" dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"syncSocket 跟踪 1"); [syncSocket writeData:数据]; NSLog(@"syncSocket 跟踪 2");
[窗口addSubview:viewController.view]; [窗口 makeKeyAndVisible]; 返回是; 它
永远不会继续发送数据,writeData 总是阻塞。 IP 192.168.0.30 是我的 Mac 的 IP。我现在按照您上面的建议使用了任何端口 12345。
但我真的不知道我必须在 Mac 上做什么才能接收?
正如你所看到的,我实际上使用了syncsocket,然后它就阻塞了。
我也尝试了asyncSocket,然后我在asyncsocket类中收到消息: writeStream Can NOT Accept Bytes
也许是我没有正确设置Mac,即我需要在Mac上运行什么应用程序来测试? 非常感谢!
I use the asyncsocket sample as a starting point to learn more about wlan communication on iPhone.
On the Mac I start a sample server opening port 0. This works, since I can write data with a test client running on the mac.
On the iPhone I think I managed to connect since "streams connected" returns YES.
Then I would like to send data with a syncsocket: (EDITED VERSION WITH COMPLETE CODE)
import "InterfaceTestAppDelegate.h" import "InterfaceTestViewController.h" import "AsyncSocket.h" import "SyncSocket.h"
@implementation InterfaceTestAppDelegate
@synthesize window; @synthesize viewController;
(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)remoteHost port:(UInt16)remotePort { NSLog(@"Socket is connected!");
NSLog(@"Remote Address: %@:%hu", remoteHost, remotePort);
NSString *localHost = [sock localHost]; UInt16 localPort = [sock localPort];
NSLog(@"Local Address: %@:%hu", localHost, localPort); }
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSLog(@"application:didFinishLaunchingWithOptions:");
/* asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
NSError *err = nil; if (![asyncSocket connectToHost: @"192.168.0.30" onPort: 1234 error: &err]) { NSLog(@"Error connecting: %@", err); } NSData *data = [@"testxyz" dataUsingEncoding:NSUTF8StringEncoding]; NSLog(@"trace 1"); [asyncSocket writeData:data withTimeout:10 tag:0]; NSLog(@"trace 2");
*/
syncSocket = [[SyncSocket alloc] initWithTimeout: 10];
syncSocket.nsLog = YES;
if (![syncSocket connectToHost: @"192.168.0.30" onPort: 12345])
{
NSLog(@"Error connecting syncSocket:");
}
NSData *data = [@"testxyz" dataUsingEncoding:NSUTF8StringEncoding];NSLog(@"syncSocket trace 1");
[syncSocket writeData:data];
NSLog(@"syncSocket trace 2");[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
It never continues to send the data, the writeData always blocks.
The IP 192.168.0.30 is my Mac's IP. I just used any port 12345 now as you suggested above.
But I don't really know what I have to do on the Mac to receive??
As you can see I actually use syncsocket, then it blocks.
I also tried asyncSocket, then I get the message in the asyncsocket class: writeStream Can NOT Accept Bytes
Maybe its that I don't setup the Mac correctly,ie what app do I need to run on the Mac to test?
Many thank!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就其价值而言,这就是您通常使用 AsyncSocket 读取某些数据的具体方式:
当然,您之前会滚动第一个“入门”读取命令:
您在连接到主机时执行此操作,因此:
不要忘记你必须在两个地方滚动下一次阅读,(a) 当你第一次连接时,(b) 当然,在每次阅读之后!
在示例中,您的通信协议将如下所示...
示例中的“getMe”之类的变量将简单地如下所示:
如果您正在努力理解这种类型的通信协议,也请尝试这个长答案:
平板电脑(iPad/Android)-服务器通信协议
AsyncSocket非常漂亮,它是由神秘的贾斯汀·沃斯 (Justin Voss) 在将互联网交给世界后似乎就放弃了互联网——它是有史以来最好的图书馆之一,是一部杰作。
希望有帮助。
For what it's worth, this is specifically how you typically read in some data using AsyncSocket:
Of course, you would have previously rolled in the first "primer" read command:
You do that when you connect to a host, hence:
Don't forget you must roll in the next read in two places, (a) when you first connect and (b) of course, after each read!
In the example your communications protocol would look like this ...
Variable like "getMe" in the example would simply look like this:
If you are struggling to understand this type of communications protocol, also try this long answer:
Tablet(iPad/Android)-Server Communication Protocol
AsyncSocket is incredibly beautiful, it was written by the mysterious Justin Voss who seemed to drop off the internet after giving it to the world - it's one of the best libraries ever written, it's a masterpiece.
Hope it helps.