Chat server

From Rosetta Code
Revision as of 15:34, 11 December 2010 by rosettacode>Dkf (→‎Tcl: Added implementation)
Chat server is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Write a server for a minimal text based chat. People should be able to connect via 'telnet', sign on with a nickname, and type messages which will then be seen by all other connected users. Arrivals and departures of chat members should generate appropriate notification messages.

PicoLisp

<lang PicoLisp>#!/usr/bin/picolisp /usr/lib/picolisp/lib.l

(de chat Lst

  (out *Sock
     (mapc prin Lst)
     (prinl) ) )

(setq *Port (port 4004))

(loop

  (setq *Sock (listen *Port))
  (NIL (fork) (close *Port))
  (close *Sock) )

(out *Sock

  (prin "Please enter your name: ")
  (flush) )

(in *Sock (setq *Name (line T)))

(tell 'chat "+++ " *Name " arrived +++")

(task *Sock

  (in @
     (ifn (eof)
        (tell 'chat *Name "> " (line T))
        (tell 'chat "--- " *Name " left ---")
        (bye) ) ) )

(wait)</lang> After starting the above script, connect to the chat server from two terminals:

           Terminal 1            |           Terminal 2
---------------------------------+---------------------------------
$ telnet localhost 4004          |
Trying ::1...                    |
Trying 127.0.0.1...              |
Connected to localhost.          |
Escape character is '^]'.        |
Please enter your name: Ben      |
                                 | $ telnet localhost 4004
                                 | Trying ::1...
                                 | Trying 127.0.0.1...
                                 | Connected to localhost.
                                 | Escape character is '^]'.
                                 | Please enter your name: Tom
+++ Tom arrived +++              |
Hi Tom                           |
                                 | Ben> Hi Tom
                                 | Hi Ben
Tom> Hi Ben                      |
                                 | How are you?
Tom> How are you?                |
Thanks, fine!                    |
                                 | Ben> Thanks, fine!
                                 | See you!
Tom> See you!                    |
                                 | ^]
                                 | telnet> quit
--- Tom left ---                 |
                                 | Connection closed.
                                 | $

Tcl

Works with: Tcl version 8.6

<lang tcl>package require Tcl 8.6

  1. Write a message to everyone except the sender of the message

proc writeEveryoneElse {sender message} {

   dict for {who ch} $::cmap {

if {$who ne $sender} { puts $ch $message }

   }

}

  1. How to read a line (up to 256 chars long) in a coroutine

proc cgets {ch var} {

   upvar 1 $var v
   while {[gets $ch v] < 0} {

if {[eof $ch] || [chan pending input $ch] > 256} { return false } yield

   }
   return true

}

  1. The chatting, as seen by one user

proc chat {ch addr port} {

   ### CONNECTION CODE ###
   #Log "connection from ${addr}:${port} on channel $ch"
   fconfigure $ch -buffering none -blocking 0 -encoding utf-8
   fileevent $ch readable [info coroutine]
   global cmap
   try {

### GET THE NICKNAME OF THE USER ### puts -nonewline $ch "Please enter your name: " if {![cgets $ch name]} { return } #Log "Mapping ${addr}:${port} to ${name} on channel $ch" dict set cmap $name $ch writeEveryoneElse $name "+++ $name arrived +++"

### MAIN CHAT LOOP ### while {[cgets $ch line]} { writeEveryoneElse $name "$name> $line" }

   } finally {

### DISCONNECTION CODE ### if {[info exists name]} { writeEveryoneElse $name "--- $name left ---" dict unset cmap $name } close $ch #Log "disconnection from ${addr}:${port} on channel $ch"

   }

}

  1. Service the socket by making corouines running [chat]

socket -server {coroutine c[incr count] chat} 4004 set ::cmap {}; # Dictionary mapping nicks to channels vwait forever; # Run event loop</lang>