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;
        }


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值