Iphone tcp connection

I want to establish a tcp connection between iphone and pc. On the PC, QTspServer is up and running (has been tested with another client application).

Here is the connection method that I use 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 does not respond to the PC. Any help is suitable

By the way: Server is nothing more than QTcpServer with an advanced method incomingConnection. Here is mainthe server side function :

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();
}
+3
source share
2 answers

The connection is established after something has been sent to the server

+1
source

Make sure it is writenot NULL after the call CFStreamCreatePairWithSocketToHost. If so, the socket connection will fail.

-(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: , - .

0

Source: https://habr.com/ru/post/1788852/


All Articles