Example #1
0
 static void Main(string[] args)
 {
     //Console.WriteLine(MyTime.getTime());
     try
     {
         server = new Server();
         t      = new Thread(new ThreadStart(server.Listen));
         t.Start();
     }
     catch
     {
         server.StopServer();
         Console.WriteLine(MyTime.getTime() + " Server is stopped");
     }
 }
Example #2
0
        public void Process()
        {
            try
            {
                string message;
                stream  = tcpClient.GetStream();
                message = GetMessage();
                string pack        = "You are join the chat.";
                byte[] dataMessage = Encoding.Unicode.GetBytes(pack);
                byte[] data        = Encoding.Unicode.GetBytes(dataMessage.Length + " " + pack);
                stream.Write(data, 0, data.Length);

                user_name = message;
                message   = "~~~~~~[" + user_name + "] computer join the chat.";
                server.AddConnection(this);
                Console.WriteLine(message);
                server.Broadcast(message, this.Id);
                while (isntClose)
                {
                    try
                    {
                        message = GetMessage();
                        if (isFile)
                        {
                            isFile = false;
                            server.BroadcastFile(message, null);
                            continue;
                        }
                        message = String.Format("[{0}][{1}] {2}",
                                                MyTime.getTime(), user_name, message);
                        Console.WriteLine(message);
                        server.Broadcast(message, this.Id);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Close();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Close();
            }
        }
Example #3
0
        protected string ReadFile(byte[] lenData, byte[] buf)
        {
            int    i        = 2;
            int    counter  = 0;
            int    z        = 0;
            string fileType = "";

            byte[] bufFileName = new byte[64];
            for (; i < buf.Length; i++)
            {
                if (buf[i] == 32 && counter == 0)
                {
                    i      += 2;
                    counter = 1;
                }
                if (counter == 0)
                {
                    lenData[i - 2] = buf[i];
                }
                if (counter == 1)
                {
                    if (buf[i] == 32)
                    {
                        i += 2;
                        break;
                    }
                    bufFileName[z] = buf[i];
                    z++;
                }
            }

            int len = int.Parse(Encoding.Unicode.GetString(lenData));

            fileType = Encoding.Unicode.GetString(bufFileName);
            StringBuilder builder = new StringBuilder();



            /*
             * byte[] data = new byte[20 - i  < len ? 20 - i : len];
             * int m = 0;
             * for (; m < lenData.Length; m++)
             * {
             *  if (m < i) continue;
             *  if (m - i > len - 1) break;
             *  data[m - i] = buf[m];
             * }
             * builder.Append(Encoding.Unicode.GetString(data, 0, data.Length));
             */

            int pos = 0;

            i += 2;
            byte[] data2 = new byte[len]; //len + i - m
            while (pos < len)             //len + i - m
            {
                pos += stream.Read(data2, 0, data2.Length);
            }
            //byte[] data3 = new byte[data.Length + data2.Length];

            /*for(int j = 0; j<data3.Length; j++)
             * {
             *  if(j < data.Length)
             *  {
             *      data3[j] = data[j];
             *  }
             *  else
             *  {
             *      data3[j] = data2[j - data.Length];
             *  }
             * }*/
            string fileName = String.Format("[{0}][{1}]", MyTime.getTimeForFileName(), user_name);

            fileType = fileType.Split("\0")[0];
            server.BroadcastFile(data2, Id, fileName, fileType);
            return(fileName);
        }