Simple Example: Networking in iOS with 2016R1.1

I just got handed an iOS deliverable task I have to have done in a very short time, and Xojo seems like a natural for this simple application. I really want to make this a success - as it will lead to more Xojo Projects. (And more Xojo licenses. :slight_smile:

I need to open a network connection (TCPSocket), send some data with a particular protocol, receive some back, display it nicely, and that’s pretty much it.

I can easily do that in 15 mins in a desktop app, but when I try to do the same thing in iOS, I run smack dab into the “new” Framework I didn’t both to learn. (yet)

Help? Anyone has a simple example of how to create and send a MB with an Int32 (big endian of course…) as the first four bytes, followed immediately by a bunch of variable data, and unpack the same kind of memory block on the way back?

From the Connected event handler in my test iOS application…

  Dim Request as Text
  Dim RequestSize as Int32
  
  Request="LGIN  PAULRAULERSON  Test1234       "
  RequestSize = Request.Length + 4
  
  
  
  dim m1 as MemoryBlock
  ' Somehow have to get the Int32 Request Size in here. 
  ' In a desktop app, I would do the following:
  '  dim m1 as MemoryBlock(4)
  ' mb.LittleEndian = False
  ' mb.In32Value(0) = RequestSize
  '
  ' That same method does not work in iOS, as it wants the dojo.Core.MemoryBlock which does not have the same methods.  
  ' I am positive this will work and work well from Xojo, I am just hampered by ignorance! -Paul
  
  me.WriteData(m1)  ' Also frustrating that I cannot find where to tell it how *much* data to write... 
  

try mutablememoryblock

dim m1 as xojo.core.MutableMemoryBlock
’ Somehow have to get the Int32 Request Size in here.
’ In a desktop app, I would do the following:
’ dim m1 as MemoryBlock(4)
’ mb.LittleEndian = False
’ mb.In32Value(0) = RequestSize

m1.In32Value(0) = RequestSize

Thanks Norman - it was quite a “duh” moment there for a bit.

Obviously from the code below, I still do not understand what they heck I am doing there, but at least it works. Now I just have to figure out how to deploy this in the Enterprise, and I can concentrate on getting things done right. :slight_smile:

Does the code below make sense, or did I just totally miss something somewhere?

Yours,
-Paul

  
  
  ' Created two MutableMemoryBlocks, really wanted to just create one, but I sure could not 
  ' find a good way to pack that text data in there, without converting it to a WString or a CString,
  ' neither of which will work in this application. 
  
  dim m1, m2 as  MutableMemoryBlock
  Dim md1 as MemoryBlock
  
  m1 = new MutableMemoryBlock(4)
  m2 = new MutableMemoryBlock(Request.Length)
  
  m1.LittleEndian = False
  m1.Int32Value(0) = RequestSize
  md1= xojo.Core.TextEncoding.ASCII.ConvertTextToData(Request)
  
  'This append does not work?  
  m2.Append(md1)
  
  me.WriteData(m1)
  me.WriteData(md1)  'This does work, though I am not enitrely sure how...
  
  'Also, I need to control the number of bytes written over the socket. 
  '?

EasyTCPSocket sample application for ios.
It can be found under Examples/iOS/Networking/EasyTCPSocket.

It has all your requirements pre-build.
EasyTCPSocket is interger code packet based and can be used to send messages (text) too.

I thought EasyTCPSocket only talked to other EasyTCPSockets? The other end of this is actually running on a remote host machine that does not support Xojo. I will go check them out, just in case I have it wrong. :slight_smile:

Thanks
-Paul

[quote=264109:@Paul Raulerson]I thought EasyTCPSocket only talked to other EasyTCPSockets? The other end of this is actually running on a remote host machine that does not support Xojo. I will go check them out, just in case I have it wrong. :slight_smile:

Thanks
-Paul[/quote]

It does, but you can change te code as it’s source is visible in the ios app rxample.

Easytcpsockets won’t work in iOS as they use the old tcpsockets under the hood.

There is a special made easytcpsocket in the examples for iOS @Greg O’Lone

@Greg O’Lone which example?

Dunno. You’d have to ask @Derk Jochems.

Examples->Communication->Internet->EasyTCPSocket->EasyTCPSocket-IOS.xojo_binary_project

Heh, that should probably be under iOS where I looked… :stuck_out_tongue:

Thanks Steve, will put a feedback request to have it moved to IOS examples!