(net-receive) or (net-only-partially-receive)?

Started by statik, January 28, 2006, 08:01:25 AM

Previous topic - Next topic

statik

I'm confused as to why the following occurs:



netcat (nc):



# nc server 5842
%œŽ00ü›nßæ"ûO-^[[?1;2c


telnet:



# telnet server 5842
Connected to server.
Escape character is '^]'.
.m'Ww¢½MºÏŸjù©3


Now as you can see here, the content that is received is always different upon every connection.  I could use netcat to connect over and over, and I would never receive the same results. This is expected behavior of the server. What confuses me, however, is the fact that the following code only receives what seems like a small portion of the stuff sent from the server.



(context 'MAIN)

(setq host "server")
(setq port 5842)

(setq socket (net-connect host port))

(if socket
        (begin
                (println "Connected successfully!")
                (net-receive socket 'buffer 5128)
                (if buffer
                        (println "Recieved: " buffer)
                )
        )
)

(net-close socket)

(exit)


And when I run that code:



# ./code
Connected successfully!
Recieved: ¹«Õ/


I can run the program multiple times and still, I only appear to get a few results (as compared to that returned by a nc or telnet connection).



# ./code
Connected successfully!
Recieved: Žž»2



# ./code
Connected successfully!
Recieved: BͲ



# ./code
Connected successfully!
Recieved: {<


Can anyone tell me why this is the case? I should be seeing a little more than I am, so what if anything, am I doing incorrectly?
-statik

Lutz

#1
Tcp/IP doesn't guarantee that all transmission will arrive in one packet. 'net-receive' in its simplest form returns after receiving one packet.



There are two ways around this:



(1) use 'net-select' with a timeout and 'net-peek' to check if there are still characters pending



(2) use 'net-receive' with the wait-string option, if you know that the transmission of the sender ends with a certain string. In this case 'net-receive' will continue filling the buffer until the wait-string is received.



Lutz

statik

#2
Is there no way to force net-receive to wait until the entire space of  'buffer has been filled?
-statik

Lutz

#3
Then you would have to know exactly how much bytes you are going to receive, or it would sit there waiting. 'net-receive' mimics the behaviour 'C' recv() socket API.



Lutz

statik

#4

-statik

Lutz

#5
probably the 'net-peek' comes to quick after the last read so there are no characters yet.



Here is code to combine 'net-select' and 'net-peek':



(while (and (net-select sock "r" 1000000) (> (net-peek sock) 0))
     (net-receive sock 'buff 1024)
     (println (net-peek sock))
     (print buff))


Of course if you know exactly how many data to receive to could do this:



(net-receive sock 'buff 16 "xyz") ; suppose 'xyz' never happens


'net-receive' will wait until 16 characters are received or "xyz" has arrived, but you know that "xyz" will never part of the string, so net-receive will return after 16 characters.



Lutz