Wanting to migrate a PHP Socket project to Corona

My apologies in advance for asking so early, but I’m anxious to confirm that I can do this with lua sockets.
Basically what we do is send an XML doc to a listening server and get XML back.

I’ve been digging around the sample code but have not found what I need yet so I thought I’d ask so that I can buy into Corona sooner rather than later.
Perhaps you can suggest a sample piece that would be appropriate for me to review.

Thanks in advance

// open a client connection $fp = fsockopen ($host, $port, $errno, $errstr); if (!$fp) { $result = "Error: could not open socket connection"; } else { // write the user string to the socket fputs ($fp, $xmlsend); fputs ($fp, chr(0)); // get the result while(!feof($fp)) { $result .= fgets($fp,1024); } fclose ($fp); [import]uid: 6547 topic_id: 928 reply_id: 300928[/import]

Some success.

-- loads the socket module   
local socket = require("socket")  
xmlsend = "XML format text string"  
local a = socket.connect("xxx.xxx.xxx.xxx" , 5056)  
a:send(xmlsend .. "\n")  
a:settimeout(5)  
-- following doesn't work  
result, err = a:receive()  
print ("err: " .. err)  
a:close()  
if result then print ( "result: " .. result ) end  

The ‘send’ works and is being received at the server.
I’ve been unable to get the receive working.
err = timeout
reply = nil

Any help greatly appreciated.
So close… and yet so far :slight_smile:

[import]uid: 6547 topic_id: 928 reply_id: 2214[/import]

You’ll want to check your return info from the send call as it will tell you whether or not the socket was closed.

A lot depends on your protocol and server as to whether there’s a handshake, etc.

I’m not entirely certain of the defaults for the socket library, if this is a non-blocking or blocking socket, if it’s open for read-only or write-only, etc.

I’ve not used it before so I’m not sure. The brief look through the documentation looks like this may be where the problem is, in your creation parameters. You’re using the convenience function socket.connect instead of creating the master socket and setting all the parameters… point is, the default might not be doing what you want.

Scott [import]uid: 5659 topic_id: 928 reply_id: 2215[/import]

Thanks for the input Scott.

Been thinking along the same lines, just having a tough time figuring it all out.
Was hoping to find an example, but so-far no luck.

Hopefully I didn’t make a mistake going Corona since I’m not a gamer, but I always think that nothing ventured, nothing gained :slight_smile:

Not giving up yet as once I have the sockets harnessed I’m pretty sure I can handle the rest to build a mobile data maintenance app.

Heinz

[import]uid: 6547 topic_id: 928 reply_id: 2216[/import]

Hello Heinz,

I saw your question, and need to do a similar thing. So I poked around and found this page:

http://www.wellho.net/resources/ex.php4?item=u116/webclient

and tried this code, which in a quick test worked for me:


    
    require "socket"
    
    client = socket.connect("localhost",51000)
    client:send("user message\r\n")
    
    while true do
     s, status, partial = client:receive(1024)
     print (s or partial)
     if status == "closed" then break end
    end
    client:close()

  

Good luck!
.phil
[import]uid: 4366 topic_id: 928 reply_id: 2322[/import]

Thanks for the fresh look.

No luck though. Send is ok, just as my other tries but it hangs on receive.

At the server I see an error 13 which leads me to believe it’s a permission thing, I.e. the server doesn’t have permission to write to the corona simulator.

Your socket connect, what type of server is listening on 51000?

Heinz
[import]uid: 6547 topic_id: 928 reply_id: 2326[/import]

Have you checked your firewall? It may not be allowing incoming connections to your application.

I’ve developed my own server for a specialized application - talking to the Arduino microcontroller and other physical computing devices. It’s here:

http://newecologyofthings.wik.is/NETLab_Toolkit

One thing I’ve noticed is that there is often a NULL character at the end of a transmission, and you have to deal with that in your code. I’ve been working on this for the afternoon, and now have the following code, which checks for a transmission terminated by the NULL. The number I’m interested in is the second element delimited by a space, which is then displayed on the iPhone.


    
    require "socket"
    
    local mainDisplay = display.newGroup()
    local textObject = display.newText( "", 27, 29, "Arial-BoldMT", 40)
    textObject:setTextColor( 255, 255, 255 )
    mainDisplay:insert( textObject, true )
    textObject.x = 100; textObject.y = 100;
    
    local getdata = function(event)
     data = ""
     for i=1,1000,1 do
     s, status, partial = client:receive(1)
     if string.byte(s) == 0 then 
     --print("NULL") 
     break
     else 
     data = data .. s
     --print(s,string.byte(s))
     end
     if status == "closed" then break end
     end
     dataValues = split(data," ")
     --print(dataValues[2])
     textObject.text = dataValues[2]
    end
    
    function split(str, pat)
     local t = {} -- NOTE: use {n = 0} in Lua-5.0
     local fpat = "(.-)" .. pat
     local last\_end = 1
     local s, e, cap = str:find(fpat, 1)
     while s do
     if s ~= 1 or cap ~= "" then
     table.insert(t,cap)
     end
     last\_end = e+1
     s, e, cap = str:find(fpat, last\_end)
     end
     if last\_end \<= #str then
     cap = str:sub(last\_end)
     table.insert(t, cap)
     end
     return t
    end
    
    client = socket.connect("10.0.1.21",51000)
    client:send("/service/osc/polling-reader/nlhubconfig/connect 192.168.0.200:10000\n")
    client:send("/service/osc/polling-reader/nlhubconfig/listen /analogin/0/value\n")
    client:send("/service/osc/polling-reader/nlhubconfig/samplerate 30\n")
    
    timer.performWithDelay( 30, getdata, 0 )
    

  

[import]uid: 4366 topic_id: 928 reply_id: 2331[/import]

Firewall not involved but I’m wondering if the NULL is my problem. Not the receiving but sending.

How do I correctly send a NULL via socket?

In PHP I just append chr(0)

Have been testing with \n as at least the send gets sent. The server is supposed to time out if no NULL received but we may have a bug in that.
SOLVED!

I was being an idiot and tried everything but ‘\0’
I think I’m off to the races now.

Thank you all.
[import]uid: 6547 topic_id: 928 reply_id: 2333[/import]