var bufStr = ""
var bufLen = 1024
socket.recv(bufStr, bufLen)
why recv() function will continue receiving data until it reaches the size defined in bufLen. how to stop receiving data without closing the sender socket
var clientSocketFD = clientSocket.getFD()
clientSocketFD.setBlocking(false)
but it work still the same way. can you make it a little clear to me?
@dom96 I'm using asyncnet and have the same trouble.
My packet already contains \c\l combination, so I cannot use recvLine for that.
I get "" only when socket disconnects.
This is my code
proc processClient(client: AsyncSocket) {.async.} =
var request: string = ""
while true:
echo "iter"
let str = await client.recv(1)
if str != "":
request = request & str
else:
echo "ended"
break
echo request
And I get "iter" many times, but never get "" while socket is connected@dom 96, I cannot determine when remote socket stops to send me data.
It's bad when I don't know size of packet which I want to receive.
await client.recv(1)
This line stops this proc. Is it some timing option for this?
I don't understand what did you mean.
I mean that I cannot determine the size of packet I receive and cannot call "recvLine" because I have \c\l in my data. Also, I cannot use "recv" because I can not see some reaction from this function when it can not receive data. I simply stops my thread until some data will come.
This is very simple task, but I still cannot find any info about how to do this.
Go handles it like this:
var n, err = socket.Read(my_buffer)
...where my_buffer is a byte array of whatever size you want.
The err is given any error (including EOF) passed and the n is how many bytes were read into the buffer (which of course will never be greater than the size of the buffer). So any Read may be of size 0 to len(my_buffer), depending on what was sent.
I don't know how one would manage without this if you don't know the full length of the data being received. Maybe there's some way, but I haven't found it.
One way to accomplish the desired non-blocking behaviour in Nim (example edited to make it more useful):
var lenReturned: int
if socket.hasDataBuffered():
# Socket receive buffer has data available, so this call won't block
lenReturned = socket.recv(userBuf, bufLen)
...
else:
# No data available, so don't call socket.recv() here - it will block
...
# At this point (after the if/else), lenReturned bytes of data have been obtained
# from the socket. If lenReturned == 0 then no data was obtained from the socket
The else block may or may not be needed depending on the exact behavior being implemented.