Page 1 of 1

Broken server

Posted: Thu Apr 14, 2005 6:43 pm
by statik
I got a server that will accept one client but exits right after the client closes the connection. I want to keep the server listening and ready to accept so that other clients (or the same one) can communicate with the server more than once.

Code: Select all

(define (server)
        (set 'addr "localhost")
        (set 'port 100)
        (set 'listen (net-listen port addr))
        (println "Waiting for connection on: " port)
        (set 'socket (net-accept listen))
        (println "Accepting... ")
        (if socket
                (while (net-receive socket 'received 1024)
                        (println "I got something: " (string received))
                        (net-send socket (string received)
                )
        )
)
Anyone got an idea why this would be?

-statik

Posted: Thu Apr 14, 2005 7:28 pm
by newdep
Hello Statik,

actualy your code is oke.. the server keeps listening..
BUT the net-accept returns a socket ID of that specific connection
that is connected at that time...

So if you client disconnect the server is till there but in your code it
is listening to a socket-id which is gone...

So you have to reinit the net-accept again!

See the exmaple in the manual that uses 'net-select, thats a little
easier to use for multiple client access..

Regards, Norman.

Posted: Thu Apr 14, 2005 7:35 pm
by Lutz
Yes, its exactly what Norman is saying. Just put a loop around it. You can stay with the same listen socket, but have to do a new 'net-accept':

Code: Select all

(define (server)
        (set 'addr "localhost")
        (set 'port 100)
        (set 'listen (net-listen port addr))
        (while true
            (println "Waiting for connection on: " port)
            (set 'socket (net-accept listen))
            (println "Accepting... ")
            (if socket
                (while (net-receive socket 'received 1024)
                        (println "I got something: " (string received))
                        (net-send socket (string received)))
		)

          )
)
Lutz

Posted: Thu Apr 14, 2005 7:43 pm
by statik
Thanks you, both of ya. I had been messing around with the the (while true) loop for a while, but couldnt get it in the right spot. I guess I just couldnt grasp everything that was going on. That worked like a charm :) Thanks again!