2011-01-30 177 views
3

我想建立iphone和PC之間的tcp連接。在PC上QTspServer正在運行並正在運行(已通過其他客戶端應用程序測試)。iphone tcp連接

這裏是連接方法我使用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服務器沒有反應。任何幫助都是合適的

順便說一句:服務器只不過是一個QTcpServer與補充incomingConnection方法。這裏是服務器端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(); 
} 
+0

只是要清楚,你已檢查服務器上沒有防火牆干擾? – outis 2011-01-30 22:03:31

+0

另外,你在模擬器或真正的iPhone上測試這個嗎? – outis 2011-01-30 22:09:19

回答

1

連接建立後發送到服務器

0

檢查write不是調用CFStreamCreatePairWithSocketToHost後NULL。如果是這樣,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); 
    } 
} 

請注意,我將輸出流存儲在self的屬性中。在你的示例代碼中,流不會保存在任何地方。你不釋放它,所以當方法退出時它仍然存在,但是沒有辦法訪問它。如果示例-connectToServer:具有代表性,那麼該錯誤將阻止對象向服務器發送任何內容。