Broken server

Started by statik, April 14, 2005, 11:43:07 AM

Previous topic - Next topic

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.



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

newdep

#1
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.
-- (define? (Cornflakes))

Lutz

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



(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

statik

#3
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!
-statik