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

**URL:** <https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490>\
**Category:** 🎓 Courses & Articles\
**Created:** [March 4, 2016, 10:46am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490 "2016-03-04T10:46:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![funkyboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/funkyboy/32/109099_2.png) [@funkyboy](https://forums.kodeco.com/u/funkyboy)\
**Post date:** [March 4, 2016, 10:46am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/1 "2016-03-04T10:46:54Z")

</div>

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 [https://www.raywenderlich.com/3057-networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server](https://www.raywenderlich.com/3057-networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server)

---

<div class="post-metadata">

**Author:** ![aisgirish](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aisgirish/32/37024_2.png) [@aisgirish](https://forums.kodeco.com/u/aisgirish)\
**Post date:** [March 16, 2016, 2:05pm UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/2 "2016-03-16T14:05:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![funkyboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/funkyboy/32/109099_2.png) [@funkyboy](https://forums.kodeco.com/u/funkyboy)\
**Post date:** [March 16, 2016, 10:17pm UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/3 "2016-03-16T22:17:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![aisgirish](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aisgirish/32/37024_2.png) [@aisgirish](https://forums.kodeco.com/u/aisgirish)\
**Post date:** [March 17, 2016, 6:04am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/4 "2016-03-17T06:04:42Z")

</div>

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](http://stackoverflow.com/questions/36031532/cfstream-nsoutputstream-not-working?noredirect=1#comment59720716_36031532)

---

<div class="post-metadata">

**Author:** ![funkyboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/funkyboy/32/109099_2.png) [@funkyboy](https://forums.kodeco.com/u/funkyboy)\
**Post date:** [March 17, 2016, 7:35am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/5 "2016-03-17T07:35:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aisgirish](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aisgirish/32/37024_2.png) [@aisgirish](https://forums.kodeco.com/u/aisgirish)\
**Post date:** [March 17, 2016, 7:59am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/6 "2016-03-17T07:59:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![funkyboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/funkyboy/32/109099_2.png) [@funkyboy](https://forums.kodeco.com/u/funkyboy)\
**Post date:** [March 17, 2016, 11:22am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/7 "2016-03-17T11:22:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aisgirish](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aisgirish/32/37024_2.png) [@aisgirish](https://forums.kodeco.com/u/aisgirish)\
**Post date:** [March 17, 2016, 12:00pm UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/8 "2016-03-17T12:00:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![funkyboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/funkyboy/32/109099_2.png) [@funkyboy](https://forums.kodeco.com/u/funkyboy)\
**Post date:** [March 17, 2016, 5:17pm UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/9 "2016-03-17T17:17:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![raushan](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/raushan/32/12043_2.png) [@raushan](https://forums.kodeco.com/u/raushan)\
**Post date:** [March 26, 2016, 3:54am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/10 "2016-03-26T03:54:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![funkyboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/funkyboy/32/109099_2.png) [@funkyboy](https://forums.kodeco.com/u/funkyboy)\
**Post date:** [March 26, 2016, 9:42am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/11 "2016-03-26T09:42:16Z")

</div>

Here is an example which does not use an explicit size for the buffer: [iphone - Stream to Get Data - NSInputStream - Stack Overflow](http://stackoverflow.com/questions/6685785/stream-to-get-data-nsinputstream)

---

<div class="post-metadata">

**Author:** ![raushan](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/raushan/32/12043_2.png) [@raushan](https://forums.kodeco.com/u/raushan)\
**Post date:** [March 26, 2016, 11:55am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/12 "2016-03-26T11:55:26Z")

</div>

Thanks for the quick response .Will try it out .

---

<div class="post-metadata">

**Author:** ![raushan](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/raushan/32/12043_2.png) [@raushan](https://forums.kodeco.com/u/raushan)\
**Post date:** [March 28, 2016, 10:12am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/13 "2016-03-28T10:12:14Z")

</div>

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) {  
NSLog(@“result…%ld”,(long)result);  
[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]  
}  
}

---

<div class="post-metadata">

**Author:** ![kishykumar](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kishykumar/32/12432_2.png) [@kishykumar](https://forums.kodeco.com/u/kishykumar)\
**Post date:** [March 30, 2016, 7:59am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/14 "2016-03-30T07:59:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![funkyboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/funkyboy/32/109099_2.png) [@funkyboy](https://forums.kodeco.com/u/funkyboy)\
**Post date:** [March 30, 2016, 10:57am UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/15 "2016-03-30T10:57:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![joseph\_ray](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/joseph_ray/32/32714_2.png) [@joseph\_ray](https://forums.kodeco.com/u/joseph_ray)\
**Post date:** [May 18, 2016, 7:00pm UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/16 "2016-05-18T19:00:45Z")

</div>

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

Thanks in advance.

Joseph

---

<div class="post-metadata">

**Author:** ![funkyboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/funkyboy/32/109099_2.png) [@funkyboy](https://forums.kodeco.com/u/funkyboy)\
**Post date:** [May 18, 2016, 7:31pm UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/17 "2016-05-18T19:31:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pahanius](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pahanius/32/102137_2.png) [@pahanius](https://forums.kodeco.com/u/pahanius)\
**Post date:** [May 24, 2016, 2:38pm UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/18 "2016-05-24T14:38:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![funkyboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/funkyboy/32/109099_2.png) [@funkyboy](https://forums.kodeco.com/u/funkyboy)\
**Post date:** [May 24, 2016, 2:58pm UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/19 "2016-05-24T14:58:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aortiz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aortiz/32/33095_2.png) [@aortiz](https://forums.kodeco.com/u/aortiz)\
**Post date:** [May 25, 2016, 8:53pm UTC](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490/20 "2016-05-25T20:53:12Z")

</div>

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.

[Next page](https://forums.kodeco.com/t/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server/6490.md?page=2)
