timeout question about net-receive

Q&A's, tips, howto's
Locked
csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

timeout question about net-receive

Post by csfreebird »

net-receive has not one timeout argument. When will it return if no data is sent by remote peer?
And another question, is it possible to set the timeout now?

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: timeout question about net-receive

Post by csfreebird »

I write some codes for solving this problem:

Code: Select all

;; socket.lsp

(context 'socket)

;; return the number of bytes which are ready to read
;; throw error if connection is closed
(define (net-peek-ex s)
  (unless (net-peek s)
	  (throw-error "connection is closed")))

;; read specified size of data from connection in specified milliseconds
;; s means socket
;; timeout means waiting for milliseconds
;; return the receivied data
;; return () if timeout
(define (read-data s timeout number)
  (let ((t (time-of-day)) (r 0))
    (catch
     (while true
	    (begin 
	      (sleep 1)
	      (set 'r (net-peek-ex s))
	      (if (> r 0) (throw r))
	      (if (> (- (time-of-day) t) timeout) (throw -1))
	      )) 'q)
    (if (!= -1 q)
	(begin (net-receive s buffer (min q number)) buffer)
	'()
	)))


(define (test)
  ;;(set 's (net-connect "localhost" 7777))
  (set 's (net-connect "www.baidu.com" 80))
  (let (x (read-data s 50 10 buff))
    (if x (println x) (println "nil:" x))
  ))

winger
Posts: 46
Joined: Wed Mar 14, 2012 7:31 am

Re: timeout question about net-receive

Post by winger »

Code: Select all

(= -1 q)
to
(> (int q) -1)
because

Code: Select all

(throw-error "connection is closed")
Welcome to a newlisper home:)
http://www.cngrayhat.org

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: timeout question about net-receive

Post by csfreebird »

What's your point?
my codes is

Code: Select all

    (if (!= -1 q)

winger
Posts: 46
Joined: Wed Mar 14, 2012 7:31 am

Re: timeout question about net-receive

Post by winger »

r is a string "'connection is closed" "when socket been closed by romote.
Then expr "(min q number)" will broke .
Welcome to a newlisper home:)
http://www.cngrayhat.org

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: timeout question about net-receive

Post by csfreebird »

you are right. Thank you.

Locked