tcp - lua socket communication from c# receiving nil -
i'm trying use lua script extract data program, , send data c# script processed , returns simple messages lua script pass along program.
the lua script follows
-- socket decls local socket = require("socket") local host, port = "127.0.0.1", 3000 local tcp = assert(socket.tcp()) tcp:connect(host,port) tcp:settimeout(0) tcp:send("stream starting...") function sendeventinfotoserver(string) tcp:send(string) end function processeventinfo() local received, status = tcp:receive() if received ~= nil print(received, status); end end while true -- unrelated logic processeventinfo(); end
and on c# end
public class simpletcplistener { private tcplistener tcplistener; private tcpclient _tcpclient; private thread listenthread; private networkstream clientstream; list<string> eventswaiting; public list<string> eventswaiting { { list<string> templist = new list<string>(); (int = 0; < eventswaiting.count; i++) templist.add(eventswaiting[i]); eventswaiting = new list<string>(); return templist; } private set { eventswaiting = value; } } public simpletcplistener() { eventswaiting = new list<string>(); this.tcplistener = new tcplistener(ipaddress.any, 3000); this.listenthread = new thread(new threadstart(listenforclients)); this.listenthread.start(); } private void listenforclients() { this.tcplistener.start(); while(true) { // blocks until client has connected server tcpclient client = this.tcplistener.accepttcpclient(); // create thread handle communication // connected client thread clientthread = new thread(new parameterizedthreadstart(handleclientcom)); clientthread.start(client); clientstream = client.getstream(); } } public void sendsavestateloadrequest() { if(clientstream != null) { asciiencoding encoder = new asciiencoding(); byte[] buffer = encoder.getbytes("hellolua!\n"); console.writeline("sending data lua"); if (clientstream.canwrite) clientstream.write(buffer, 0, buffer.length); else console.writeline("connection close!"); //clientstream.flush(); } } private void handleclientcom(object client) { tcpclient tcpclient = (tcpclient)client; networkstream clientstream = tcpclient.getstream(); byte[] message = new byte[4096]; int bytesread; while(true) { bytesread = 0; try { // blocks until client sends message bytesread = clientstream.read(message, 0, 4096); } catch { // socket error has occured break; } if(bytesread == 0) { // client disconnected break; } // message has been recieved asciiencoding encoder = new asciiencoding(); system.diagnostics.debug.writeline(encoder.getstring(message, 0, bytesread)); string s = encoder.getstring(message, 0, bytesread); console.writeline(s); if (!string.isnullorempty(s)) eventswaiting.add(s); } tcpclient.close(); } }
now, lua talks c# thread no problem, , c# doesn't report errors when sending data lua, on lua side it's receiving nil, if remove nil check receives , prints string, nil check never seems find 'received' not nil. i'm new lua script it's simple i'm not understanding syntax can't seem find lot of documentation.
any suggestions hugely appreciated.
Comments
Post a Comment