3

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(); } 
7
  • Just to be clear, you've checked that no firewall on the server is interfering? Commented Jan 30, 2011 at 22:03
  • Also, are you testing this on a simulator or a real iPhone? Commented Jan 30, 2011 at 22:09
  • Yes. The Server is enabled in windows firewall settings. I'm trying to test both on iPhone and a simulator Commented Jan 30, 2011 at 22:11
  • How isn't the server reacting? What's supposed to happen when a client connects to the server? Commented Jan 30, 2011 at 22:34
  • The breakpoint should trigger. Nothing else now Commented Jan 30, 2011 at 23:06

2 Answers 2

1

The connection is established after something was sent to the server

Sign up to request clarification or add additional context in comments.

Comments

0

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.

6 Comments

Can you explain your answer please? And also the now time problem is server is not responding on connection
It's not NULL. Both of them. It's not the indicator the connection is cussessfull: developer.apple.com/library/ios/#documentation/CoreFoundation/…. Here is written: The streams do not open a connection to the specified host until one of the streams is opened.
Sockets don't need to connect to a host to fail.
I've tried to do as you suggested. Write stream is created. But the server on PC does not get a new connection
I know i should keep streams. I want now to establish connection. That's the problem
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.