Broken server

For the Compleat Fan
Locked
statik
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Broken server

Post 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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

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

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

Post 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

statik
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Post 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!

Locked