Trying to receive quick multiple messages with a non-block TCP socket

Hi guys… I need a little help here. I’m currently developing an MMORPG Prototype, using both TCP and UDP… Because this game requires alot of *important* update messages to be broadcasted to the players, there is a need for non-blocking TCP…

So far I’ve been using the LuaSocket lib … http://w3.impa.br/~diego/software/luasocket/tcp.html. This is my code for the TCPSocket Module

module(...,package.seeall);  
  
local socket = require "socket"  
local tcpSocket = socket.tcp()  
local receiveTimer = nil  
  
socketOpen = false  
  
tcpFail = nil  
  
tcpReceive = nil  
  
local function isEmpty(value)  
 return not not tostring(value):find("^%s\*$")  
end  
  
local function cleanUp()  
 if(recieveTimer) then  
 timer.cancel(receiveTimer)  
 end  
end  
local function listen()  
 local s, status, partial = tcpSocket:receive()  
 if status == "closed" or status == "Socket is not connected" then  
 socketOpen = false  
 if(tcpFail) then  
 tcpFail(status)  
 end  
 cleanUp()  
 else  
 --ignore empty string buffers  
 if(isEmpty(partial) == false) then  
 if(tcpReceive) then  
 tcpReceive(s or partial,status)  
 end  
 else  
 return  
 end  
  
 end  
  
end  
  
function connect(ip,port, interval)  
 tcpSocket:connect(ip, port)  
 tcpSocket:settimeout(0)  
 tcpSocket:setoption("tcp-nodelay",true)  
 tcpSocket:setoption("keepalive",true)  
 socketOpen = true  
  
 receiveTimer = timer.performWithDelay(interval, listen, 0)  
  
end  
  
function send(data)  
  
 tcpSocket:send(data)  
 print("TCP SEND: "..data)  
end  
  
function close()  
 cleanUp()  
 tcpSocket:close()  
end  
  

To test this module, I’ve also written a tcp echo server, which will output any received message *15* times… So I’m suppose to receive the 15 replies from the server after I sent a message, However the problem now is that it only receives and reads 1 of the replies… So now the problem is that it didn’t read the stream fully, if the server sends more than 1 messages at the same time… Is there a known fix to this problem? So far after intensive googling…no luck on any solution…

P.S Sorry for my poor broken english

[import]uid: 182598 topic_id: 34844 reply_id: 334844[/import]

Nvm guys I got it … I just changed the code from tcpSocket:receive() to tcpSocket:receive(512) [import]uid: 182598 topic_id: 34844 reply_id: 138473[/import]

Nvm guys I got it … I just changed the code from tcpSocket:receive() to tcpSocket:receive(512) [import]uid: 182598 topic_id: 34844 reply_id: 138473[/import]

Thanks for this, it helped me.

Thanks for this, it helped me.

for the future, here’s a TCP socket module which might be of benefit. it will dispatch events when new data is available. 

http://code.coronalabs.com/code/dmcsockets-–-buffered-non-blocking-event-or-callback-based-network-library

cheers,

dmc

for the future, here’s a TCP socket module which might be of benefit. it will dispatch events when new data is available. 

http://code.coronalabs.com/code/dmcsockets-–-buffered-non-blocking-event-or-callback-based-network-library

cheers,

dmc