iPhone TCP 连接
我想在iPhone和PC之间建立tcp连接。在 PC 上 QTspServer 正在运行并工作(已与其他客户端应用程序进行测试)。
这是我在 iPhone 上使用的连接方法:
- (IBAction)connectToServer:(id)sender {
CFReadStreamRef read = NULL;
CFWriteStreamRef write = NULL;
NSString *host = @"192.168.1.169";
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)host, 1000, &read, &write);
CFWriteStreamOpen(write);
int k = 0;
}
PC 上的服务器没有反应。任何帮助都是适当的顺便
说一句:服务器只不过是一个带有补充incomingConnection
方法的QTcpServer。这是服务器端的 main
函数:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
AbstractServer server;
server.listen(QHostAddress::Any, 1000);
QLabel label("Hello server");
label.setFixedSize(400, 400);
label.show();
return app.exec();
}
I want to establish tcp connection between iphone and PC. On PC QTspServer is running and working (was tested with other client application).
Here is the connection method i'm using on iphone:
- (IBAction)connectToServer:(id)sender {
CFReadStreamRef read = NULL;
CFWriteStreamRef write = NULL;
NSString *host = @"192.168.1.169";
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)host, 1000, &read, &write);
CFWriteStreamOpen(write);
int k = 0;
}
The server on PC is not reacting. Any help is appropriate
By the way: Server is nothing more then a QTcpServer with replemented incomingConnection
method. Here is the main
function on server side:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
AbstractServer server;
server.listen(QHostAddress::Any, 1000);
QLabel label("Hello server");
label.setFixedSize(400, 400);
label.show();
return app.exec();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将某些内容发送到服务器后建立连接
The connection is established after something was sent to the server
在调用
CFStreamCreatePairWithSocketToHost
后检查write
是否不为 NULL。如果是,套接字连接失败< /a>.请注意,我将输出流存储在 self 的属性中。在您的示例代码中,流不会保存在任何地方。您没有释放它,因此当方法退出时它仍然存在,但无法访问它。如果示例
-connectToServer:
具有代表性,则该错误将阻止您的对象向服务器发送任何内容。Check that
write
isn't NULL after the call toCFStreamCreatePairWithSocketToHost
. If it is, the socket connection is failing.Note that I store the output stream in a property of
self
. In your sample code, the stream isn't saved anywhere. You don't release it, so it still exists when the method exits, but there's no way of accessing it. If the sample-connectToServer:
is representative, that error will prevent your object from sending anything to the server.