Esempio n. 1
0
        public void HandleConnection()
        {
            client = newsock.AcceptTcpClient();

            Thread recei = new Thread(new ThreadStart(ReceiveData));

            recei.Start();
            int           recv; byte[] data = new byte[1024];
            NetworkStream ns = new NetworkStream(client);

            connections++;
            //listBoxserver.Items.Add("New client accepted: "+ connections+ "active connections");
            if (listBoxserver.InvokeRequired)
            {
                listBoxserver.Invoke(new MethodInvoker(delegate {
                    listBoxserver.Items.Add("New client accepted: " + connections + "active connections");
                }));
            }
            string welcome = "Welcome to my test server";

            data = Encoding.ASCII.GetBytes(welcome);
            ns.Write(data, 0, data.Length);
            while (true)
            {
                data = new byte[1024];
                recv = ns.Read(data, 0, data.Length);
                if (recv == 0)
                {
                    break;
                }

                ns.Write(data, 0, recv);
            }
            ns.Close();
            client.Close();
            connections--;
            //Console.WriteLine("Client disconnected: {0} active connections", connections);
        }