iPhone connection doesn't work

54 views
Skip to first unread message

bsdev

unread,
Oct 27, 2009, 9:40:20 AM10/27/09
to CocoaAsyncSocket
Hello!

I'm using the controller "AsyncSocketController.m" which can be
downloaded here.
I have a TCP server on the one hand and the iPhone implementation
acting as a client on the other hand.

My TCP server (Ruby) is very simple. It listens on port 11111 and
every connection will be threaded. An incoming connection puts a "Got
a message" to the console, send back "Hello!" and close the
connection. Finally it puts to the console of the server "Socket
closed". The server works fine if I use a client script written in
Ruby or in PHP. I don't think that my problem is located server-side.
It must be the client-side, the iPhone.

require 'thread'
require 'socket'
Port = 11111
server = TCPServer.open(Port)
loop {
Thread.start(server.accept) do |socket|
puts "Got a message"
socket.puts "Hello!"
socket.close
puts "Socket closed"
end
}

I create a new iPhone project, add a single button and an action
method "buttonPressed" with the following content:

AsyncSocketController *asyncSock = [[AsyncSocketController alloc]
initWithParms:@"192.168.100.55" portNum:11111];
[asyncSock openSocket];
NSString* str= @"testString";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
[asyncSock writeSocket:data tag:1];
[asyncSock readSocket:2];
[asyncSock closeSocket];

I think that must be fine. Starting the application on the iPhone
pressing the button, there is no output on the server-side.
The same application running on the iPhone simulator, returns once
"Got a message" on the server side. Pressing the button x more times
nothing happened, only once. No "Socket closed" messages, nothing.

What's wrong? Does anyone know?

Thanks & Regards

bsdev

unread,
Oct 28, 2009, 5:44:02 AM10/28/09
to CocoaAsyncSocket
Hello!

I think I have to start much more simple. I'm trying to build up a
connection between the given EchoServer and a Cocoa-Application (not
an iPhone App).
I start the EchoServer on Mac OS 10.6 with portnumber 55555. Then I
wrote a Cocoa App with one button and the following content:

AsyncSocket *asyncSocket = [[AsyncSocket alloc]
initWithDelegate:self];
NSError *err;
[asyncSocket connectToHost:@"172.16.100.10" onPort:55555 error:&err];
[asyncSocket disconnect];

That works. The EchoServer puts on every button-pressed action
Echo server started on port 55555

Accepted client 172.16.100.10:58337
Client Disconnected: 172.16.100.10:58337
Accepted client 172.16.100.10:58338
Client Disconnected: 172.16.100.10:58338
Accepted client 172.16.100.10:58339
Client Disconnected: 172.16.100.10:58339
...

That's great, the connection is established.

This method is called when a client connects to the server:
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host
port:(UInt16)port {
[self logInfo:FORMAT(@"Accepted client %@:%hu", host, port)];

NSString *welcomeMsg = @"Welcome to the AsyncSocket Echo Server\r\n";
NSData *welcomeData = [welcomeMsg
dataUsingEncoding:NSUTF8StringEncoding];

[sock writeData:welcomeData withTimeout:-1 tag:WELCOME_MSG];

[sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT
tag:0];
}

I see, there will be a welcome message which should be send to the
client.
How could I receive this message on the client side? And how could I
send something to the server which will be displayed there?

Thanks & Regards

bsdev

unread,
Oct 28, 2009, 5:55:32 AM10/28/09
to CocoaAsyncSocket
Update:

These lines work well in a Cocoa-App:
AsyncSocket *asyncSocket = [[AsyncSocket alloc]
initWithDelegate:self];
NSError *err;
[asyncSocket connectToHost:@"172.16.100.10" onPort:55555 error:&err];
[asyncSocket disconnect];

I got the accepted and disconnected messages on the EchoServer.

The same lines in a iPhone-Application doesn't work.
On the iPhone Simulator only once (one message pair accepted/
disconnected), all further button clicks -> no message.
On the iPhone I never see a message.

What's the problem? I'm using XCode 3.2.1 64Bit, Mac OS X 10.6.1 on a
MacBook Pro.

Thanks & Regards

bsdev

unread,
Oct 28, 2009, 6:10:44 AM10/28/09
to CocoaAsyncSocket
All messages starting the iPhone simulator. Clicking the button no
more messages appear. Perhaps you could see an error which causes this
problem.

28.10.09 11:07:54 com.apple.launchd.peruser.501[91]
(com.apple.iPhoneSimulator:com.apple.AOSNotification) Conflict with
job: com.apple.AOSNotification over Mach service:
com.apple.AOSNotification
28.10.09 11:07:54 com.apple.launchd.peruser.501[91]
(com.apple.iPhoneSimulator:com.apple.dataaccess.dataaccessd) Bug:
launchd_core_logic.c:4562 (23932):2
28.10.09 11:07:54 com.apple.launchd.peruser.501[91]
(com.apple.iPhoneSimulator:com.apple.dataaccess.dataaccessd) Path
monitoring failed on "/var/mobile/Library/Preferences/
com.apple.dataaccess.launchd": No such file or directory
28.10.09 11:07:54 com.apple.launchd.peruser.501[91]
(com.apple.iPhoneSimulator:com.apple.apsd) Bug: launchd_core_logic.c:
4562 (23932):2
28.10.09 11:07:54 com.apple.launchd.peruser.501[91]
(com.apple.iPhoneSimulator:com.apple.apsd) Path monitoring failed on "/
var/mobile/Library/Preferences/com.apple.apsd.launchd": No such file
or directory
28.10.09 11:07:54 com.apple.launchd.peruser.501[91]
(com.apple.iPhoneSimulator:com.apple.AOSNotification) Bug:
launchd_core_logic.c:4562 (23932):2
28.10.09 11:07:54 com.apple.launchd.peruser.501[91]
(com.apple.iPhoneSimulator:com.apple.AOSNotification) Path monitoring
failed on "/var/mobile/Library/Preferences/
com.apple.AOSNotification.launchd": No such file or directory
28.10.09 11:07:54 vot[15823] ******** AX INSPECTOR MODE ************
28.10.09 11:07:54 vot[15823] VoiceOverTouch exiting because
accessibility is not enabled. To enable do this: 'defaults write
com.apple.Accessibility AccessibilityEnabled true'
28.10.09 11:07:54 mobile_installationd[15825] a0b85500
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:54 mobile_installationd[15825] a0b85500
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:54 mobile_installationd[15825] a0b85500
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:54 mobile_installationd[15825] a0b85500
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:54 mobile_installationd[15825] a0b85500
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:54 mobile_installationd[15825] a0b85500
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:54 mobile_installationd[15825] b0081000
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:54 mobile_installationd[15825] b0081000
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:54 mobile_installationd[15825] b0081000
load_application_info: Could not load signer identity from /Users/
admin/Library/Application Support/iPhone Simulator/User/Applications/
131C89BC-F3BC-4500-86E0-175116DC8C5E/NewSockets.app/NewSockets
28.10.09 11:07:54 mobile_installationd[15825] b0081000
load_application_info: Could not load signer identity from /Users/
admin/Library/Application Support/iPhone Simulator/User/Applications/
267DFE49-3D0A-4D53-B2DD-761CBE80C2ED/Control Fun.app/Control Fun
28.10.09 11:07:54 mobile_installationd[15825] b0081000
load_application_info: Could not load signer identity from /Users/
admin/Library/Application Support/iPhone Simulator/User/Applications/
2A7FF00C-3E11-47EC-B04B-52BDF11D04A3/Sockets.app/Sockets
28.10.09 11:07:54 mobile_installationd[15825] b0081000
load_application_info: Could not load signer identity from /Users/
admin/Library/Application Support/iPhone Simulator/User/Applications/
9CCBAA09-ADAB-41CF-A8D5-8D602C40427F/NewSockets.app/NewSockets
28.10.09 11:07:54 mobile_installationd[15825] b0081000
load_application_info: Could not load signer identity from /Users/
admin/Library/Application Support/iPhone Simulator/User/Applications/
A0EB2EF4-BE44-48CB-BDBA-DEF588B88E60/Hello World.app/Hello World
28.10.09 11:07:54 mobile_installationd[15825] b0081000
load_application_info: Could not load signer identity from /Users/
admin/Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/NewSockets.app/NewSockets
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/Documents to /Users/
admin/Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//Documents
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/Documents to /Users/
admin/Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//Documents
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/Library to /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//Library
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/Library/Caches to /
Users/admin/Library/Application Support/iPhone Simulator/User/
Applications/4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//Library/Caches
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/Library/Caches to /
Users/admin/Library/Application Support/iPhone Simulator/User/
Applications/4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//Library/Caches
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/Library/Preferences to /
Users/admin/Library/Application Support/iPhone Simulator/User/
Applications/4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//Library/Preferences
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/Library/
Preferences/.GlobalPreferences.plist to /Users/admin/Library/
Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//Library/
Preferences/.GlobalPreferences.plist
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/Library/Preferences/
com.apple.PeoplePicker.plist to /Users/admin/Library/Application
Support/iPhone Simulator/User/Applications/4EBD82BA-7FCB-437C-
B7FB-15DAD53004B7//Library/Preferences/com.apple.PeoplePicker.plist
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/Library/Preferences to /
Users/admin/Library/Application Support/iPhone Simulator/User/
Applications/4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//Library/Preferences
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/Library to /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//Library
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/tmp to /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//tmp
28.10.09 11:07:55 mobile_installationd[15825] b0081000
normalize_restored_container2: Attempting to merge /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/Container/tmp to /Users/admin/
Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7//tmp
28.10.09 11:07:55 mobile_installationd[15825] b0081000
load_application_info: Could not load signer identity from /Users/
admin/Library/Application Support/iPhone Simulator/User/Applications/
4EBD82BA-7FCB-437C-B7FB-15DAD53004B7/NewSockets.app/NewSockets
28.10.09 11:07:55 mobile_installationd[15825] b0081000
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:55 mobile_installationd[15825] b0081000
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:55 SpringBoard[15826] Unable to create
CFServerConnection. Telephony state may be incorrect.
28.10.09 11:07:55 mobile_installationd[15825] b0081000
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:55 mobile_installationd[15825] b0081000
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:55 SpringBoard[15826] Not migrating manifest
28.10.09 11:07:55 SpringBoard[15826] ...completed profile migration.
28.10.09 11:07:55 SpringBoard[15826] Unable to create
CFServerConnection. Telephony state may be incorrect.
28.10.09 11:07:55 SpringBoard[15826] Unable to create
CFServerConnection. Telephony state may be incorrect.
28.10.09 11:07:55 SpringBoard[15826] Unable to create
CFServerConnection. Telephony state may be incorrect.
28.10.09 11:07:55 SpringBoard[15826] Can't find the translation
dictionary, loadTranslationDictionaries
28.10.09 11:07:55 mobile_installationd[15825] b0081000
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:55 mobile_installationd[15825] b0081000
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:55 mobile_installationd[15825] b0081000
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:55 mobile_installationd[15825] b0081000
init_simulator_paths: No simulator root specified. Falling back to
environment variable.
28.10.09 11:07:55 SpringBoard[15826] Unable to create
CFServerConnection. Telephony state may be incorrect.
28.10.09 11:07:55 SpringBoard[15826] Unable to create
CFServerConnection. Telephony state may be incorrect.

bsdev

unread,
Oct 28, 2009, 6:42:30 AM10/28/09
to CocoaAsyncSocket
Last Update:

Running the source code on the iPhone simulator and setting
breakpoints, I get for every button pressed action a "accepted/
disconnected" message on the EchoServer.
So, it works in debug-mode on the simulator. So I don't know how to
debug...

(iPhone 3GS, 32GB)

Best regards.

bsdev

unread,
Oct 28, 2009, 8:34:03 AM10/28/09
to CocoaAsyncSocket
On the iPhone and on the simulator:
If I do not use this line
[asyncSocket disconnect];
it is possible to connect to the EchoServer.
It puts
Accepted client 172.16.100.10:49561
on every button click and disconnects with the message
Client Disconnected: 172.16.100.10:49561
after ~20-25 seconds automatically.

Robert Hanson

unread,
Oct 28, 2009, 10:56:38 AM10/28/09
to cocoaasy...@googlegroups.com
> These lines work well in a Cocoa-App:
> AsyncSocket *asyncSocket = [[AsyncSocket alloc]
> initWithDelegate:self];
> NSError *err;
> [asyncSocket connectToHost:@"172.16.100.10" onPort:55555 error:&err];
> [asyncSocket disconnect];

No, they don't.

AsyncSocket is asynchronous. What this means is that methods that are
normally blocking (and time consuming) do not block - they operate
asynchronously. One example is the connectToHost:onPort:error:
method. This method is asynchronous. So, in other words, if you call
this method it will immediately return. But it is NOT connected yet.
It starts the connection process in the background. And when it's
done, it will invoke the delegate method
(onSocket:didConnectToHost:port:).

You're not giving the socket time to connect. You're starting the
connect process and then immediately telling it to disconnect.

-Robbie Hanson

bsdev

unread,
Oct 28, 2009, 11:45:00 AM10/28/09
to CocoaAsyncSocket
Oh, okay, your explanation helped me a lot! Now I have found my
problem and I can send a message to the server and there I see it on
the screen.

"it will invoke the delegate method (onSocket:didConnectToHost:port:)"
-> I am a newbie programming in Objective-C. Does this mean if the
connection is established the method (onSocket:didConnectToHost:port:)
will be called in the background?
And how can I know the connection is established?

My server is an EchoServer. How can I receive this echo message?
[asyncSock readDataWithTimeout:0.500 tag:1];
How can I get the message text?

Thanks a lot for your help!

Robert Hanson

unread,
Oct 28, 2009, 11:52:51 AM10/28/09
to cocoaasy...@googlegroups.com
> "it will invoke the delegate method (onSocket:didConnectToHost:port:)"
> -> I am a newbie programming in Objective-C. Does this mean if the
> connection is established the method (onSocket:didConnectToHost:port:)
> will be called in the background?
> And how can I know the connection is established?

When the connection is established, the
onSocket:didConnectToHost:onPort: delegate method will be called.
Once this method is called, you know the socket is connected. The
delegate method will be called in the same thread that you started the
socket in. (So you don't have to worry about all those crazy
threading issues. All delegate methods will be invoked in the proper
thread.)

> My server is an EchoServer. How can I receive this echo message?
> [asyncSock readDataWithTimeout:0.500 tag:1];
> How can I get the message text?

The way you use the AsyncSocket is dependent upon the protocol you're
using it for. E.g HTTP, POP3, IMAP, etc.

If you're working with an EchoServer, these terminate each message
with an ending CRLF (carriage return, line feed). So you can do
something like this:

[asyncSocket readDataToTerm:[AsyncSocket CRLFData] withTimeout:-1.0
tag:0];

-Robbie Hanson

bsdev

unread,
Oct 28, 2009, 12:16:03 PM10/28/09
to CocoaAsyncSocket
> When the connection is established, the  
> onSocket:didConnectToHost:onPort: delegate method will be called.  
> Once this method is called, you know the socket is connected.  The  
> delegate method will be called in the same thread that you started the  
> socket in.  (So you don't have to worry about all those crazy  
> threading issues.  All delegate methods will be invoked in the proper  
> thread.)
Okay, and how can I check this in my source?

Something like: if (connectionEstablished) { do something }?

> > My server is an EchoServer. How can I receive this echo message?
> > [asyncSock readDataWithTimeout:0.500 tag:1];
> > How can I get the message text?
>
> The way you use the AsyncSocket is dependent upon the protocol you're  
> using it for.  E.g HTTP, POP3, IMAP, etc.
I see the different readData methods in the source code.

> If you're working with an EchoServer, these terminate each message  
> with an ending CRLF (carriage return, line feed).  So you can do  
> something like this:
>
> [asyncSocket readDataToTerm:[AsyncSocket CRLFData] withTimeout:-1.0  
> tag:0];
It must be "readDataToData" instead of "readDataToTerm"?

My source code:

NSString* str= @"test\r\n";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
[asyncSocket writeData:data withTimeout:0.300 tag:1];
[asyncSocket readDataToData:[AsyncSocket CRLFData] withTimeout:-1.0
tag:0];

How can I access the response message? I want to print it for example
via NSLog().
But now I have to use the "delegate methods"? Because if I call the
method to write the data to the server, it could be possible if I call
the method readDataToData the message isn't send at this time and
therefore the server can't send me the echo?

Thanks & Regards.

bsdev

unread,
Oct 29, 2009, 6:02:10 AM10/29/09
to CocoaAsyncSocket
I integrate these lines in my Socket-Controller.m file:

-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData*)data withTag:
(long)tag {
NSLog(@"Tag: %i", tag);
NSString* aStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@", aStr);
}

Now the response is printed in my logfile. But how should I handle it?
Should I create a class file and instantiate it if I need a connection
and make a read/write process via this new classs? So I can save the
data in a class property and get the value of the response.
And how do I know, that the read process finished? I have to check it
in a while loop or what's the best solution?

Thank you in advance.
Reply all
Reply to author
Forward
0 new messages