尝试将 iPod touch 连接到我的服务器时网络超时
我有一个 iPod touch 程序,它应该从我的 Mac 上的服务器程序接收消息。为了确保 touch 可以接收来自 Mac 以外的计算机的消息,我用 C++ 编写了服务器。如果我在同一台计算机上运行服务器和 iPod 应用程序(应用程序在模拟器上运行),则连接良好并且一切顺利。但是,当我尝试从我的设备连接到服务器时,连接超时。有人能发现问题吗?我不太擅长网络和 iPhone 操作系统。
server.cpp:
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
cout << "ERROR opening socket";
return;
}
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
cout << "ERROR on binding";
return;
}
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, (socklen_t*)&clilen);
if (newsockfd < 0) {
cout << "ERROR on accept.";
return;
}
服务器卡在accept()处,等待应用程序...
client.m:
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)hostName, portNum, &readStream, &writeStream);
if (readStream && writeStream) {
NSLog(@"Starting streams");
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
inputStream = (NSInputStream *)readStream;
[inputStream retain];
[inputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
outputStream = (NSOutputStream *)writeStream;
[outputStream retain];
[outputStream setDelegate:self];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream open];
}
if (readStream)
CFRelease(readStream);
if (writeStream)
CFRelease(writeStream);
据我所知,服务器和客户端都没有报告任何错误(我正在通过errno和NSError检查)其他比超时。
如果有人可以帮助我,非常感谢!
I have an ipod touch program that should receive messages from a server program on my mac. To make sure that the touch can receive messages from a computer other than a mac, I programmed the server in C++. If I run both the server and the ipod app on the same computer (the app running on the simulator), the connection is fine and everything is dandy. However, when I try to connect to the server from my device, the connection times out. Can anyone spot the problem? I'm not too good with networking, and the iPhone OS in general.
server.cpp:
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
cout << "ERROR opening socket";
return;
}
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
cout << "ERROR on binding";
return;
}
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, (socklen_t*)&clilen);
if (newsockfd < 0) {
cout << "ERROR on accept.";
return;
}
The server gets stuck at the accept(), waiting for the app...
client.m:
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)hostName, portNum, &readStream, &writeStream);
if (readStream && writeStream) {
NSLog(@"Starting streams");
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
inputStream = (NSInputStream *)readStream;
[inputStream retain];
[inputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
outputStream = (NSOutputStream *)writeStream;
[outputStream retain];
[outputStream setDelegate:self];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream open];
}
if (readStream)
CFRelease(readStream);
if (writeStream)
CFRelease(writeStream);
As far as I can tell, neither server nor client reports any errors (I'm checking through errno and NSError) other than timing out.
If anyone can help me with this, much thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
iPod 连接到的网络与我的 Mac 不同,这就是它被阻止的原因。当我连接到同一网络时,它工作得很好。
The iPod was connected to a different network than my Mac, and that's why it got blocked. When I connected to the same network, it works perfectly fine.