# Questions about sockets and checking for recieved data

**URL:** <https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560>\
**Category:** Questions\
**Created:** [July 1, 2018, 7:58am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560 "2018-07-01T07:58:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 1, 2018, 7:58am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/1 "2018-07-01T07:58:00Z")

</div>

So I am working on a solo project for fun. I essentially made a socket server in c# and can send data to it with the lua client created in defold.  
I was just wondering how I can check for received data in defold from the server.  
So say I send a string to the client from the server how would I check for any data or specific data sent from the server.  
so something like  
if(client:recievedData)then  
print(recievedData)  
end  
also was wondering how I can check if the client got connected in lua.  
I have looked over the lua socket api but can’t find it.  
Is there such a function for this?

any help would be greatly appreciated \<3

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [July 1, 2018, 7:33pm UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/2 "2018-07-01T19:33:55Z")

</div>

It’s non trivial to make non-blocking socket connections. You can take a look at the tcp client in DefNet for inspiration ([defnet/defnet/tcp\_client.lua at master · britzl/defnet · GitHub](https://github.com/britzl/defnet/blob/master/defnet/tcp_client.lua)).

socket.select() takes two lists of sockets to check for ability to read and write from.

The receive() function on a client socket is used to read data (possibly partial data).

---

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 2, 2018, 2:57am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/3 "2018-07-02T02:57:37Z")

</div>

Sorry but I see receive has two arguments how would I go about receiving any data coming in?  
say I send a hi from the server.  
how would I print the hi in the console on defold engine?

so something line

```
if client:receive() then
 print(client:msg)
end

```

Sorry I know this may be a nooby question but I really do need help on this. I just don’t understand it yet.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [July 2, 2018, 5:43am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/4 "2018-07-02T05:43:15Z")

</div>

The documentation ([API reference (LuaSocket)](https://www.defold.com/ref/socket/#client:receive:-pattern---prefix-)) says the two arguments are pattern and prefix.

Pattern is either a number with the number of bytes you wish to read or a string indicating how to read data, “\*l” reads until a newline for instance. Prefix is any already read data to prefix received data with.

---

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 2, 2018, 8:37am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/5 "2018-07-02T08:37:55Z")

</div>

is there a way to set pattern as whatever bytes are received?

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [July 2, 2018, 8:41am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/6 "2018-07-02T08:41:18Z")

</div>

Again, the documentation ([API reference (LuaSocket)](https://www.defold.com/ref/socket/#client:receive:-pattern---prefix-)):

“\*a” reads from the socket until the connection is closed. No end-of-line translation is performed;

---

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 2, 2018, 8:48am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/7 "2018-07-02T08:48:45Z")

</div>

ok thanx Ill give it my best shot 🙂

wow I feel kinda dumb now ;(  
I figured the “\*a” was something I was suppose to specify myself not just write “\*a”  
yup I’m dumb

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [July 2, 2018, 5:56pm UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/8 "2018-07-02T17:56:26Z")

</div>

You will likely not want to use “\*a”. It will read until there are no more bytes. I’m not sure what would happen if there’s like a megabyte of data to receive but it will likely cause a hitch while receiving. It’s better, in a game, to set timeout to 0 and read in chunks. Especially if you don’t know how much data there is.

---

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 2, 2018, 9:59pm UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/9 "2018-07-02T21:59:39Z")

</div>

just a question sorry still having trouble showing the data from the receive  
if I wanted to show the received data could I in theory do

```
local data = client:receive("*a", 0)
if data then
         print(data)
end

```

reason I ask is because I am just trying to make sure I have my client setup right so I can troubleshoot if I am sending the data correctly or not from the server.  
sorry again.

---

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 11, 2018, 12:12pm UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/10 "2018-07-11T12:12:42Z")

</div>

bump

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [July 11, 2018, 3:28pm UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/11 "2018-07-11T15:28:51Z")

</div>

> [@serifaz](#):
>
> still having trouble showing the data from the receive

What kind of problem? Are you not receiving any data?

> [@serifaz](#):
>
> reason I ask is because I am just trying to make sure I have my client setup right so I can troubleshoot if I am sending the data correctly or not from the server.

I think it’s better if you share all of your socket handler code. It’s hard to tell if what you’re doing is good or bad.

Did you look at the [tcp\_client.lua from DefNet](https://github.com/britzl/defnet/blob/master/defnet/tcp_client.lua)? It handles non-blocking TCP socket clients without any problems. Sending and receiving data.

---

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 12, 2018, 6:23am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/12 "2018-07-12T06:23:53Z")

</div>

I did have a look at it but for some reason it will not connect to my server, not sure why. it doesn’t give an error just says connection was denied.  
anyways here is my current code for what I have in my login script . it works completely I just don’t know how I can check for the data I send back.

```
local tcp_client = require "main.scripts.tcp_client"
local IP = "localhost"
local PORT = 5392

function init(self)
	sock = socket.connect(IP, PORT)
	self.user_message = ""
	self.pass_message = ""
	msg.post(".", "acquire_input_focus") 
	self.message = "" 
	status = gui.get_node("Status")
	
	if not sock then
		gui.set_text(status, "server offline")
	end
	if sock then
		gui.set_text(status, "server online")
	end
	
end
function onData(data)
	print(data)
end
function onDisconnect()
	print("disconnected")
end
function on_input(self, action_id, action)
	local userField = gui.get_node("userBg")
	local passField = gui.get_node("passBg")
	local loginBtn = gui.get_node("loginBtn")
	if(gui.pick_node(userField, action.x, action.y))then
		self.field_selected = "user"
		userField = gui.get_node("userField")
		userTxt = gui.get_text(userField)
	end
	if(gui.pick_node(passField, action.x, action.y))then
		self.field_selected = "pass"
		passField = gui.get_node("passField")
		passTxt = gui.get_text(passField)
	end

	if(gui.pick_node(loginBtn, action.x, action.y))then
		sock:send(self.user_message .. "," .. self.pass_message)
		
	end
	if self.field_selected == "user" then
		if action_id == hash("type") then
			self.user_message = self.user_message .. action.text
			gui.set_text(userField, self.user_message) 
		elseif action_id == hash("backspace") and action.repeated then
			local l = string.len(self.user_message)
			self.user_message = string.sub(self.user_message, 0, l-1) 
			gui.set_text(userField, self.user_message)
		end
	end
	if self.field_selected == "pass" then
		if action_id == hash("type") then
			self.pass_message = self.pass_message .. action.text
			gui.set_text(passField, self.pass_message)
		elseif action_id == hash("backspace") and action.repeated then
				local l = string.len(self.pass_message)
				self.pass_message = string.sub(self.pass_message, 0, l-1) 
				gui.set_text(passField, self.pass_message)
			
		end
	end
end

```

would really appreciate any help I can get

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [July 12, 2018, 6:43am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/13 "2018-07-12T06:43:05Z")

</div>

You don’t have any place where you try to receive data on the socket. All I see is the sock:send() call when clicking the login button. Why do you use sockets for that by the way? It would make a lot more sense to do a login using a normal HTTP request.

Anyway, if you want to receive data on the socket you need to poll for it with some interval. To check if you can read from the socket you use socket.select(). A good place to do this would be in an update loop or using a repeating timer:

```
-- check if it's possible to read from the socket
-- 0 = don't block the thread, wait 0 seconds
local receivet,sendt = socket.select({ sock }, nil, 0)
-- if the socket is in receivt then it means it's possible to read from the socket
if receivet[sock] then
    -- read a line from the socket (ie until a linebreak \n)
    -- err can be "closed" or "timeout"
    local data, err = sock:receive("*l")
    print("received", data, err)
end

```

---

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 17, 2018, 4:35am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/14 "2018-07-17T04:35:38Z")

</div>

ok so I tried the above code in my update function but when I send the data from the server the client crashes.  
This is my current update function.

```
function update(self, dt)
	local receivet,sendt = socket.select({ sock }, nil, 0)
	-- if the socket is in receivt then it means it's possible to read from the socket
	if receivet[sock] then
		-- read a line from the socket (ie until a linebreak \n)
		-- err can be "closed" or "timeout"
		local data, err = sock:receive("*l")
		print("receivded", data, err)
	end
end

```

I don’t know if this helps but it is in a gui script. could that cause an issue?  
also when I close the server early without any data being sent or received the client log does send the error with nil data received, but when I do send data it doesn’t say anything. prob cus client crashes not sure tho.

thanx again for helping me by the way.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [July 17, 2018, 7:01am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/15 "2018-07-17T07:01:35Z")

</div>

When you say “crash”, do you mean the entire application shut down?  
Or, was it a Lua error that was displayed?  
Was there any error output in the editor console?

How much data did you send? Is it binary or text you send?

---

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 17, 2018, 11:23am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/16 "2018-07-17T11:23:18Z")

</div>

the entire application crashes no Lua errors are displayed no error in output  
and Im sending back to the client what the client sends to the server which is just a username and password

Just out of curiosity should I change the 0 in the

```lua
receivet, sendt = socket.select({sock}, nil, 0)

```

to be longer or just leave at 0?

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [July 17, 2018, 11:56am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/17 "2018-07-17T11:56:47Z")

</div>

ok. what platform is the client?  
The editor console should have said that the client wrote a “\_crash” file as well?

---

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 17, 2018, 12:00pm UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/18 "2018-07-17T12:00:17Z")

</div>

sorry I think I mis-explained. it actually just stops responding. but the platform the client it on is windows 10.

sorry that was my bad honestly

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [July 17, 2018, 12:28pm UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/19 "2018-07-17T12:28:30Z")

</div>

Did you get past the if-statement? Add a print before the “sock:receive(”\*l")"

Also, when you send the data, do you add a newline? I see the comment here: “read a line from the socket (ie until a linebreak \n)”

---

<div class="post-metadata">

**Author:** ![serifaz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/serifaz/32/8714_2.png) [@serifaz](https://forum.defold.com/u/serifaz)\
**Post date:** [July 18, 2018, 1:49am UTC](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560/20 "2018-07-18T01:49:03Z")

</div>

I’m not necessarily sending lines of data but just data. should I change to “a\*” instead?  
also I set the print before the if statement it fires off consistently in the console until I send the socket data and it freezes the client  
once it freezes the update function doesn’t seem to keep firing but I would figure that’s normal haha.

[Next page](https://forum.defold.com/t/questions-about-sockets-and-checking-for-recieved-data/23560.md?page=2)
