"Source: Text/System/IO.text";
"Line: 2457";
"Date: Thu Sep 25 14:47:43 2025";
"Main: Fri Sep 26 14:29:19 2025";
// original file: Text/System/IO.text, line: 2457
// Example: H3E17 ()
print "Example: H3E17";
ei := GetEchoInput();
SetEchoInput(true);
// no-test
chatserver := procedure(host, port)
   server := Socket(: LocalHost := host, LocalPort := port);
   chatters := {};
   ids := AssociativeArray();
   while true do
      text := "";
      ready := WaitForIO([server] cat Setseq(chatters));
      for I in ready do
         if I eq server then    // new chatter
            C := WaitForConnection(server);
            id := Sprintf("[%o]", r[2]) where _,r := SocketInformation(I);
            text cat:= Sprintf("%o has joined\n", id);
            ids[C] := id;
            Include(~chatters, C);
         else
            ok, msg := ReadCheck(I);
            if not ok or IsEof(msg) then    // error or EOF, assume they left
               text cat:= Sprintf("%o has left\n", ids[I]);
               Remove(~ids, I);
               Exclude(~chatters, I);
            else    // all is well; add message to text to send
               text cat:= Sprintf("%o: %o", ids[I], msg);
            end if;
         end if;
      end for;
      if #text gt 0 then
         for I in chatters do
            Write(I, text);
         end for;
      end if;
   end while;
end procedure;
chatserver("localhost", 7000);
SetEchoInput(ei);
