iPhone TCP 连接

发布于 2024-10-15 01:12:28 字数 816 浏览 1 评论 0原文

我想在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小女人ら 2024-10-22 01:12:28

将某些内容发送到服务器后建立连接

The connection is established after something was sent to the server

清浅ˋ旧时光 2024-10-22 01:12:28

在调用 CFStreamCreatePairWithSocketToHost 后检查 write 是否不为 NULL。如果是,套接字连接失败< /a>.

-(IBAction)connectToServer:(id)sender {
    CFWriteStreamRef write = NULL;

    NSString *host = @"192.168.1.169";
    int port = 1000;
    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)host, port, NULL, &write);
    if (!write) {
        // connection failed.
        NSLog(@"Connection to %@:%d failed.",host,port);
    } else {
        CFWriteStreamOpen(write);
        // keep a reference to the output stream for later use.
        self.output = (NSOutputStream*)write;
        // the function that made the output stream has "Create" in its name, so
        // this method owns the write stream & should release it.
        CFRelease(write);
    }
}

请注意,我将输出流存储在 self 的属性中。在您的示例代码中,流不会保存在任何地方。您没有释放它,因此当方法退出时它仍然存在,但无法访问它。如果示例 -connectToServer: 具有代表性,则该错误将阻止您的对象向服务器发送任何内容。

Check that write isn't NULL after the call to CFStreamCreatePairWithSocketToHost. If it is, the socket connection is failing.

-(IBAction)connectToServer:(id)sender {
    CFWriteStreamRef write = NULL;

    NSString *host = @"192.168.1.169";
    int port = 1000;
    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)host, port, NULL, &write);
    if (!write) {
        // connection failed.
        NSLog(@"Connection to %@:%d failed.",host,port);
    } else {
        CFWriteStreamOpen(write);
        // keep a reference to the output stream for later use.
        self.output = (NSOutputStream*)write;
        // the function that made the output stream has "Create" in its name, so
        // this method owns the write stream & should release it.
        CFRelease(write);
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文