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

Q&A's, tips, howto's
Locked
statik
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

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

Post by statik »

I'm confused as to why the following occurs:

netcat (nc):

Code: Select all

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

Code: Select all

# 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.

Code: Select all

(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: Select all

# ./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: Select all

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

Code: Select all

# ./code
Connected successfully!
Recieved: BͲ

Code: Select all

# ./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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Post by statik »

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Post by statik »


-statik

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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':

Code: Select all

(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:

Code: Select all

(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

Locked