Maintenance - We're currently working on things and you might experience some issues. Should be wrapped up soon!

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

5
Efficient receive() in luasocket
Post Flair (click to view more posts with a particular flair)
Post Body

I'm having trouble understanding how I'm supposed to use luasocket to efficiently receive data. My application is a TCP server that waits for clients to connect and then will handle them all in parallel. I decided to go with non-blocking server socket and polling using select over all sockets: the actuall connected sockets and the listening socket.

Receive takes a parameter which is supposed to be the number of bytes it will read. If I set it to 100 and have the socket in non-blocking mode (timeout 0) then receive will return "timeout" on the second returned value and throw away the 50 bytes. Why not keep those 50 bytes for the next receive I do? Or even better why not return both the 50 bytes AND the "timeout" error?

I can make my application work if I set the receive size to 1 byte, but that means that I'll need to aggregate byte by byte in an array, and the concatenate it when I need the data.

What am I missing here?

EDIT: Here's the critical part of the code: https://pastebin.com/raw/Q9WTQUKH

Author
Account Strength
100%
Account Age
17 years
Verified Email
Yes
Verified Flair
No
Total Karma
13,905
Link Karma
2,072
Comment Karma
11,818
Profile updated: 14 hours ago
Posts updated: 7 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
1 year ago