/// <summary>
        /// Starts the server going and taking connections
        /// </summary>
        public void start(MessageRecievedListener mrl)
        {
            if (running)
            {
                return;
            }
            myMRL            = mrl;
            chatSenderThread = delegate(object o)
            {
                ChatServer cs = (ChatServer)o;
                while (!(cs.running && cs.clients.Count != 0))
                {
                }
                ClientInfo ci         = cs.clients.Last <ClientInfo>();
                Socket     s          = ci.socket;
                byte[]     name_bytes = new byte[bufferSize];
                s.Receive(name_bytes);
                ci.name    = Encoding.ASCII.GetString(ChatServer.noNulls(name_bytes));
                name_bytes = Encoding.ASCII.GetBytes(ci.name + ": ");
                while (true)
                {
                    try {
                        byte[] bytes = new byte[bufferSize];
                        s.Receive(bytes);
                        bytes = ChatServer.noNulls(bytes);
                        string msg = Encoding.ASCII.GetString(bytes);
                        //Console.WriteLine("Server Recieved bytes:"+msg);
                        cs.myMRL(ChatServer.getTimeStamp() + " " + ci.name + ": " + msg);
                        cs.broadcast(ChatServer.conCat(name_bytes, bytes), s);
                    } catch (SocketException e) {
                        //Console.WriteLine(e.StackTrace);
                    }
                }
            };
            MyServer.Listen(5);
            running = true;
            ParameterizedThreadStart receptions = delegate(object o)
            {
                ChatServer cs = (ChatServer)o;
                while (running)
                {
                    cs.clients.Add(new ClientInfo(MyServer.Accept()));
                    newChatThread();
                }
            };

            addThread(receptions);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Hangs until a message is recieved then returns it in string form
 /// </summary>
 /// <returns>The string sent by the server</returns>
 public string recieve()
 {
     byte[] bytes = new byte[bufferSize];
     mySocket.Receive(bytes);
     return(Encoding.ASCII.GetString(ChatServer.noNulls(bytes)));
 }