iPhone losing wifi after AsyncSocket writeData

199 views
Skip to first unread message

Shukuyen

unread,
Oct 13, 2010, 5:25:33 PM10/13/10
to CocoaAsyncSocket
Hi all,

I am using CocoaAsyncSocket to connect an iPhone app via tcp over wifi
to a c# server. Everything worked fine and still works fine in the
simulator, but on the device (iPhone 4, iOS 4.1) the app is losing the
wifi connection after the first writeData.

This is my writeData call:

[theSocket writeData:[[[commandDictionary JSONRepresentation]
stringByAppendingString:@"\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]
withTimeout:kMessageTimeout
tag:0];

where [commandDictionary JSONRepresentation] is equal to
{"Type":"command","Command":"right"}.

I can reconnect the wifi by opening up the settings app.


Any hints what I could be doing wrong?

Thanks a lot,
Shukuyen.

Aleksei Kac

unread,
Oct 13, 2010, 8:31:18 PM10/13/10
to cocoaasy...@googlegroups.com
Turn off Bluetooth.

--
Sent from my mobile phone
(please excuse any mistakes or short response I might have made)

> --
> You received this message because you are subscribed to the Google Groups "CocoaAsyncSocket" group.
> To post to this group, send email to cocoaasy...@googlegroups.com.
> To unsubscribe from this group, send email to cocoaasyncsock...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cocoaasyncsocket?hl=en.
>

Shukuyen

unread,
Oct 14, 2010, 1:02:27 PM10/14/10
to CocoaAsyncSocket
Wow, I didn't expect that. Turning off bluetooth solved the
problem ... Do you know why this happens and maybe even how to avoid
it? I can't imagine users being very happy when they have to turn off
bluetooth every time they want to use the app ...


On 14 Okt., 02:31, Aleksei Kac <alex...@gmail.com> wrote:
> Turn off Bluetooth.
>
> --
> Sent from my mobile phone
> (please excuse any mistakes or short response I might have made)
>

Alex Kac

unread,
Oct 14, 2010, 2:08:17 PM10/14/10
to cocoaasy...@googlegroups.com
Yup....

--
WebIS, Inc.
CEO/Founder

http://www.pocketinformant.com

Faraaz Sareshwala

unread,
Oct 14, 2010, 5:59:02 PM10/14/10
to cocoaasy...@googlegroups.com, cocoaasy...@googlegroups.com
Is this an iOS problem or cocoa async socket problem?

Aleksei Kac

unread,
Oct 14, 2010, 9:08:57 PM10/14/10
to cocoaasy...@googlegroups.com, cocoaasy...@googlegroups.com
It's an iOS problem but it's not a bug per se.

--
Sent from my mobile phone
(please excuse any mistakes or short response I might have made)

Aleksei Kac

unread,
Oct 14, 2010, 9:11:27 PM10/14/10
to Shukuyen, cocoaasy...@googlegroups.com
The problem is that Bluetooth now supports tcp/ip and so when you do work on the network that is not specifically excluding bluetooth the radio goes full power and it then overpowers the wifi radio.

Or something like that. That's the gist of what the engineers on the Dev Forums said.

--
Sent from my mobile phone
(please excuse any mistakes or short response I might have made)

On Oct 14, 2010, at 5:44 PM, Shukuyen <shuk...@googlemail.com> wrote:

> Yup you know what causes the problem and how to fix it? Care to
> share? :-)
>
> Thanks!

>>> For more options, visit this group athttp://groups.google.com/group/cocoaasyncsocket?hl=en.

Faraaz Sareshwala

unread,
Oct 14, 2010, 10:59:11 PM10/14/10
to cocoaasy...@googlegroups.com, Shukuyen, cocoaasy...@googlegroups.com
Ah I see. Does that mean if I have only Bluetooth on, cocoa async socket will send packets over the Bluetooth connection rather than wifi?

Somewhat related: I am under the impression that apple doesn't expose an API to interact with Bluetooth on iOS. I could be wrong though...

Aleksei Kac

unread,
Oct 14, 2010, 11:24:16 PM10/14/10
to cocoaasy...@googlegroups.com, cocoaasy...@googlegroups.com, Shukuyen
They do actually. But you can also use tcp over BT. It's not discussed much if at all.

--
Sent from my mobile phone
(please excuse any mistakes or short response I might have made)

Shukuyen

unread,
Oct 15, 2010, 2:22:34 AM10/15/10
to CocoaAsyncSocket
Thanks a lot, Aleksei. That brings some light into this.

On 15 Okt., 05:24, Aleksei Kac <alex...@gmail.com> wrote:
> They do actually. But you can also use tcp over BT. It's not discussed much if at all.
>
> --
> Sent from my mobile phone
> (please excuse any mistakes or short response I might have made)
>
> On Oct 14, 2010, at 9:59 PM, Faraaz Sareshwala <fara...@gmail.com> wrote:
>
>
>
> > Ah I see. Does that mean if I have only Bluetooth on, cocoa async socket will send packets over the Bluetooth connection rather than wifi?
>
> > Somewhat related: I am under the impression that apple doesn't expose an API to interact with Bluetooth on iOS. I could be wrong though...
>
> > On Oct 14, 2010, at 6:11 PM, Aleksei Kac <alex...@gmail.com> wrote:
>
> >> The problem is that Bluetooth now supports tcp/ip and so when you do work on the network that is not specifically excluding bluetooth the radio goes full power and it then overpowers the wifi radio.
>
> >> Or something like that. That's the gist of what the engineers on the Dev Forums said.
>
> >> --
> >> Sent from my mobile phone
> >> (please excuse any mistakes or short response I might have made)
>

Robbie Hanson

unread,
Oct 15, 2010, 12:02:42 PM10/15/10
to cocoaasy...@googlegroups.com
Would it help if I added a method which would allow you to specify which interface to connect using (wifi, Ethernet, cell, etc)?

-Robbie Hanson

Richard Fink

unread,
Oct 14, 2010, 3:05:39 PM10/14/10
to cocoaasy...@googlegroups.com
I am intending to use CocoaAsyncSocket too but having the user turning off
Bluetooth is not an acceptable route to making it work. I appreciate Alex
stating so simply the workaround solution. But can he or someone give some
logic as to why Bluetooth is a culprit here ?

I think we need to find a way to use CocoaAsyncSocket without turning
Bluetooth off.

Richard Fink
1160 Mission St, #2108
San Francisco, CA 94103
Tel:   415.409.6396
Cell:  415.302.7317
Skype: richardfink
Twitter: @ricfink

Jim Geldermann

unread,
Oct 15, 2010, 12:26:10 PM10/15/10
to cocoaasy...@googlegroups.com
On the Mac side is there a command to grab a specific network interface?

Jim Geldermann

Robbie Hanson

unread,
Oct 15, 2010, 12:46:55 PM10/15/10
to cocoaasy...@googlegroups.com
Yes, there is a way to query the existing interfaces, as well as their properties. A programmatic way to get info similar to the command line ifconfig tool. I'd post a code example if I was at my computer...

-Robbie Hanson

Aleksei Kac

unread,
Oct 15, 2010, 1:25:35 PM10/15/10
to cocoaasy...@googlegroups.com, cocoaasy...@googlegroups.com
Yes that solves the problem too. For bonjour it only does if you use bsd bonjour API.

--
Sent from my mobile phone
(please excuse any mistakes or short response I might have made)

Richard Fink

unread,
Oct 16, 2010, 4:16:17 AM10/16/10
to cocoaasy...@googlegroups.com
Robbie, how do you do that ? May I ask you to post some code to make it
clearer ?

Thanks.

Richard Fink
1160 Mission St, #2108
San Francisco, CA 94103
Tel:   415.409.6396
Cell:  415.302.7317
Skype: richardfink
Twitter: @ricfink

-----Original Message-----
From: cocoaasy...@googlegroups.com
[mailto:cocoaasy...@googlegroups.com] On Behalf Of Robbie Hanson
Sent: Friday, October 15, 2010 9:03 AM
To: cocoaasy...@googlegroups.com
Subject: Re: iPhone losing wifi after AsyncSocket writeData

Robbie Hanson

unread,
Oct 18, 2010, 3:03:36 PM10/18/10
to cocoaasy...@googlegroups.com
> Robbie, how do you do that ?

You can achieve this by binding the socket (invoking bind()) before starting the connect process.

The bind method is generally considered to be for server use. You bind a socket to a specific port (e.g. port 80 for HTTP servers). But bind takes both an interface and a port. If the interface is left "blank" then the socket will use any interface. And likewise, if the port is left "blank" then the socket will use any available port.

So for outgoing client connections, one could bind the socket the a specific interface, while still allowing the socket to pick any available port upon connection (as typically happens).

> May I ask you to post some code to make it clearer ?


I've committed the new method in revision 101. I also added a sample iPhone xcode project named "InterfaceTest".

It demonstrates the new client method that allows you to specify which interface to use.
The project gives sample code demoing how one might specify the WiFi or 3G/cell interface.

Let me know if you have any questions.
I'll try to put up more general information concerning interfaces and the AsyncSocket interface options on the wiki later this week.

-Robbie Hanson

Raymond Choi

unread,
Oct 19, 2010, 3:58:03 AM10/19/10
to cocoaasy...@googlegroups.com
i am using specific protocol to achieve my project. 

I should communicate on TCP/IP. 

But socket using ip header checker. 
and it drop my stx header information. 
it use the my stx packet as specific protocol. 
So, It discard non ip header packet. 

Not send data. if i send my specific packet to server. 
it die after sending, 
Or reading, when reading, 
the error message: ...Errordomain.....

Thank you for your reply. 

I solved it as using bsd simple socket.....

Best regards. 


2010/10/19 Robbie Hanson <robbie...@deusty.com>

Shukuyen

unread,
Oct 19, 2010, 6:06:49 AM10/19/10
to CocoaAsyncSocket
Robbie, thanks for taking the time to help sort this out! But wow,
your example is way beyond me :-)
You say this is "considered to be for server use", what to do on a
client? My hope is on the upcoming documentation to help me integrate
this low level stuff in my iPhone client app ...

coolerkid9999

unread,
Nov 2, 2010, 5:39:28 PM11/2/10
to CocoaAsyncSocket
Is it possible to only have it work over WIFI or 3G and not over a
connection that was established using Internet Tethering? I don't want
my app to be able to be used to connect to a computer where the iPhone
and computer are connected via internet tethering. Is this possible?
Thanks!

Robbie Hanson

unread,
Nov 6, 2010, 7:01:55 PM11/6/10
to cocoaasy...@googlegroups.com
The latest version allows you to specify the interface to use. This would allow you to accomplish what you want. One caveat: you have to use the lower level connectToAddress methods.

-Robbie Hanson

Jim Geldermann

unread,
Nov 10, 2010, 10:57:45 AM11/10/10
to cocoaasy...@googlegroups.com
I have a java client to a middleware that uses out.flush() to terminate a message. How do I get asyncsocket to send the equivalent? Currently I am using [socket writeData:[AsyncSocket CRLFData] withTimeout:-1 tag:0]; as a flush method but it does not seem to signal an end of message to the server. When I connect the java client to Echo Server I need to add "\n\r' to flush the stream.

Shukuyen

unread,
Nov 15, 2010, 9:12:40 AM11/15/10
to CocoaAsyncSocket
Hi all,

sorry to bring this up again, but can anybody confirm the bluetooth
problem is fixed by connectToAddress?
I changed my app to use connectToAddress via the wifi interface (by
using the code of the testproject). With bluetooth on sending and
receiving data is still slaggy and based on luck, once I turn
bluetooth off it works like a charm.

Suggestions?

Thanks,
Shukuyen.

On Nov 7, 12:01 am, Robbie Hanson <robbiehan...@deusty.com> wrote:
> The latest version allows you to specify the interface to use. This would allow you to accomplish what you want. One caveat: you have to use the lower level connectToAddress methods.
>
> -Robbie Hanson
>

Robbie Hanson

unread,
Nov 16, 2010, 12:01:52 AM11/16/10
to cocoaasy...@googlegroups.com
I use my iPhone for testing, and I almost always have Bluetooth turned on (although it is usually not connected to anything). How can I duplicate this problem? Do I need my Bluetooth to be connected to something? If so, can it simply be connected to, say, a car? Or do I need to connect to another phone?

-Robbie Hanson

Shukuyen

unread,
Nov 16, 2010, 4:31:32 AM11/16/10
to CocoaAsyncSocket
I am connecting to a c# socket server using DotNetAsyncSocket (http://
code.google.com/p/wifiremote/source/browse/trunk/SocketServer.cs).
The client app finds the hostname via bonjour, resolves the address
with the dnsresolve code from your example and uses the wifi
interface.

Problems occur on an iPhone 3g and an iPhone 4 with bluetooth on, not
connected to anything. Haven't tested it yet with other devices.
I don't think that I am doing anything out of the ordinary with this
connection. Once it is established the server sends a short "welcome"
json message delimited with \r\n (readDataToData on the client). After
this the client can send data and continues t readDataToData.

Thanks for looking into this, if you know of anything else I can do to
help you reproduce the problem please tell me.

Shukuyen.
> ...
>
> read more »

Kendall Trego

unread,
Nov 16, 2010, 9:33:30 AM11/16/10
to cocoaasy...@googlegroups.com
Have you tried resetting the device to default settings? In the past I've
had the WIFI drop and had to either reset wifi settings and/or toggle
Bluetooth. I've been using cocoaasyncsocket on iPhone 3.x/iPad 3.x/iPhone
4.x without noticing anything other than intermittent issues.

Shukuyen

unread,
Nov 16, 2010, 10:13:15 AM11/16/10
to CocoaAsyncSocket
Hi Kendall,

no I didn't try to reset the device. That seems to be a rather drastic
"solution", nothing I would like to say to someone how downloaded your
app ... Also my wifi doesn't drop when the problem occurs. Reception
is good the whole time (at least that's what the indicator on the
status bar says).

Kendall Trego

unread,
Nov 17, 2010, 9:35:19 AM11/17/10
to cocoaasy...@googlegroups.com
I guess I should clarify - I meant "Reset Network Settings" under the
general settings tab which will reset the wifi connection rather than the
entire device.

I too would still see the wifi status bar showing signal, but wifi would not
work through any app on the device, not just mine.

Shukuyen

unread,
Nov 19, 2010, 8:53:18 AM11/19/10
to CocoaAsyncSocket
Thanks for the clarification, Kendall.
I reset the network settings on my device but the problem still
persists.

I will try to find time to do a simple test project and check if it
happens there, too. If it doesn't I at least know that the problem is
somewhere in my source, not in AsyncSocket ...
Reply all
Reply to author
Forward
0 new messages