littleGirl_dev
littleGirl_dev

Reputation: 131

iphone tcp connection

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();
}

Upvotes: 3

Views: 4334

Answers (2)

littleGirl_dev
littleGirl_dev

Reputation: 131

The connection is established after something was sent to the server

Upvotes: 1

outis
outis

Reputation: 77400

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.

Upvotes: 0

Related Questions