Group Group Group Group Group Group Group Group Group Forums

Networking Tutorial for iOS: How To Create A Socket Based iPhone App and Server

A networking tutorial for iOS in which you will learn how to create a socket based server.

This is a companion discussion topic for the original entry at

I need to connection with socket host but to Android device.
If I send data infinitely then only android host get data. Otherwise, it is not able to get it.

Hi @aisgirish,

can you provide a few more details about your scenario?

Are you running the server code of the article?
Do you need to support both iOS and Android?

Actually, I am developing an iOS app which need to communicate to Android app.

  • Android app :
    it will create wifi hotspot and socket server. our android developer has also create android client app which can send & receive message (Byte) to/from server.

  • iOS app :
    it will just connect to socket server. For this, I need to go to Wifi setting and manually connect to wifi hotspot created by Android server app. I read your article, It is very helpful to make connection to server. I can successfully connect to socket server. But Android server app only able to read stream if I pass long data or small data infinitely. I had asked question at here

Have you tried adding a ‘\n’ at the end of the message sent via the iOS app?

No. I just send string “I am here”. Now, I send string as “I am here \n”. It call - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode;

with eventCode = NSStreamEventHasSpaceAvailable

Ok, that should be a good sign. Is data received by the Android device?

No. I there any sample socket server. So, I can test on it.

Well the article we are discussing includes a server written in Python :slight_smile:

I am using the same code and increases the buffer size from 1024 to 50012 but it is not able to read whole 50012 bytes at a time.We are transferring a 26MB Image from linux server to IPAD and data is divided in to buffer of 50012 bytes but when we use this method after increasing the buffer size most of the bytes are getting missed so we are getting distorted image.Can you please suggest which approach i should follow.

Here is an example which does not use an explicit size for the buffer:

Thanks for the quick response .Will try it out .

i have tried that method but i am able to read only 2000 bytes at a time .Can u please tell how can we read 50012 bytes in one read .
Below is the source code for reference .
NSInteger result;
uint8_t buffer[50012]; // BUFFER_LEN can be any positive integer
while((result = [inputStream read:buffer maxLength:50012]) != 0) {
if(result > 0) {
[self OCRImageSave:buffer];
// buffer contains result bytes of data to be handled
} else {
// The stream had an error. You can get an NSError object using [iStream streamError]

I am trying to implement an iOS chat application using websockets.
The question is how does message delivery work when the iOS app is in the background. Is the message lost because the socket connection is broken when the app is in the background?
If yes, then do I have to implement a state refresh protocol every time the app comes to foreground?

When the connection is down the message won’t be sent.
Yes, you need a way to cache messages somewhere and send them when the app is online again.
Background mode is “ice on the cake”. Many chat apps use it to refresh the list of messages so that when the user opens the app there’s no need to wait for the UI to refresh. But background mode should NOT be used as the means to cache messages.

I was wondering how the iphone send the data using socket method? Is it over the wifi or bluetooth?

Thanks in advance.


In the case of this tutorial data is sent and received via wifi.

Anyone knows how to create the same server in PHP? Could you provide some tutorials?

@pahanius I am sorry, I am clueless about PHP libs that deal with sockets :frowning:

Hi funkyboy,

excellent tutorial, thanks for this.
I want to ask you some thing, I need open a socket for send a string between two apps on the same ipad.
This applied too?
Client and Server on the same iPad, and the server could be in background state.

PD: Sorry by my english, but I am from Chile and my english is not the best.