天天看點

C# server socket 異步

using System;

using System.Collections.Generic;

using System.Text;

using System.Net;

using System.Net.Sockets;

using System.Threading;

namespace ScoketProxy

{

    class ZServer

    {

        ManualResetEvent clientConnected =    new ManualResetEvent(false);

        TcpListener tl;

        public ZServer(int port)

        {

            tl = new TcpListener(port);

        }

        ~ZServer()

        {

            tl.Stop();

        }

        public void Listen()

        {

            tl.Start(5);

            clientConnected.Reset();

            while (true)

            {

                tl.BeginAcceptTcpClient(new AsyncCallback(OnAccept), tl);

                clientConnected.WaitOne();

            }

        }

        void OnAccept(IAsyncResult ar)

        {

            TcpListener listener = (TcpListener)ar.AsyncState;

            TcpClient client = listener.EndAcceptTcpClient(ar);

            TcpClient tranform = new TcpClient("220.181.5.97",80);

            NetworkStream cns = client.GetStream();

            NetworkStream tns = tranform.GetStream();

            while (client.Connected)

            {

                //byte[] cbuf = ReadBlock(client);

                //tns.Write(cbuf, 0, cbuf.Length);

                //byte[] tbuf = ReadBlock(tranform);

                //cns.Write(tbuf,0,tbuf.Length);

                byte[] cbuf = ReadBlock(client);

                SendBlock(client, cbuf);

                byte[] tbuf = ReadBlock(tranform);

                SendBlock(tranform, tbuf);

            }

            //TcpListener listener = (TcpListener)ar.AsyncState;

            //TcpClient client = listener.EndAcceptTcpClient(ar);

            //clientConnected.Set();

            //while(true)

            //{

            //    string s = ReadLine(client);

            //    if (s == "exit")

            //    {

            //        Console.WriteLine("bye");

            //        break;

            //    }

            //    byte[] buf = GetBytes(s);

            //    client.GetStream().Write(buf,0,buf.Length);

            //}

            //return;

        }

        byte[] GetBytes(string s)

        {

            return System.Text.Encoding.UTF8.GetBytes(s.ToCharArray());

        }

        string GetString(byte[] bs)

        {

            return System.Text.Encoding.UTF8.GetString(bs);

        }

        string ReadLine(TcpClient client)

        {

            StringBuilder sb = new StringBuilder();

            NetworkStream ns = client.GetStream();

            while (true)

            {

                byte[] buf = new byte[client.Available];

                ns.Read(buf, 0, buf.Length);

                string s = GetString(buf);

                if (s == "/r/n")

                {

                    break;

                }

                sb.Append(s);

            }

            return sb.ToString();

        }

        byte[] ReadBlock(TcpClient client)

        {

            System.Collections.Generic.List<byte> r = new List<byte>();

            while (client.GetStream().DataAvailable)

            {

                byte[] buf = new byte[1];

                client.GetStream().Read(buf, 0, buf.Length);

                r.Add(buf[0]);

            }

            return r.ToArray();

        }

        int SendBlock(TcpClient c,byte[] buf)

        {

            c.GetStream().Write(buf, 0, buf.Length);

            return buf.Length;

        }

    }

}