Can't receive messages on client but can send to server

Hello I have a c# socket server I made that seems to handle messages from lua fine. but when I send back from the server defold is not processing the message.
this is my client code

socket = require("socket");
local gettime = socket.gettime
tcp = assert(socket.tcp());
ip, port = "127.0.0.1", 5392;
tcp:settimeout(0);
tcp:connect(ip, port);
local data, errormsg = tcp:receive()
while data do
	print("message received:"..data)
end
function init(self)
	msg.post(".", "acquire_input_focus") 
end
function on_input(self, action_id, action)
	if action_id == hash("touch") and action.pressed then 
		local loginButton = gui.get_node("loginBtn") 		
		if gui.pick_node(loginButton, action.x, action.y) then
			tcp:send("1,user,pass, 1")			
		end
	end
end
function timer(seconds)
	local exp_time = gettime() + seconds
	return function()
		if gettime() < exp_time then return false end
		exp_time = exp_time + seconds
		return true
	end
end
function mysplit (inputstr, sep)
	if sep == nil then
		sep = "%s"
	end
	local t={}
	for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
		table.insert(t, str)
	end
	return t
end

I am currently just sending in a static user and pass to the server to check a db for that user and pass. then I am trying to send a message back but sofar not working. I tried arguments in my receive of “*l” and “*a” but not working either any help would be awesome thanx.

PS. Quick question… is it possible to access the sprite rigging system at runtime?

Receive defaults to “*l”. Are you sending data that ends with a line break? Receive will not return anything until you either close the socket or send a line break. You’re not checking for an error when you receive either. It could return “timeout”.

I would not recommend that you do the socket operations outside the scope of the life cycle functions. Plus you have everything as global functions and variables in the script you shared. Move socket operations to init and use the local keyword.

3 Likes

well you were completely right about the timeout. I checked for the error and thats what its doing. but how can I keep it from doing that?

Also I cant get the message to be received even after the socket is closed I am currently using *a for testing.

You need to continuously check if you can read and write to the socket. In your example you try to read once.

Take a look at this tcp_client:

It checks if it can read/write on the socket here:

2 Likes