zoukankan      html  css  js  c++  java
  • 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;
            }



        }

    }


  • 相关阅读:
    mysql常用命令(2)
    mysql常用命令(1)
    svn使用方法介绍(1)
    java设计模式
    maven常见错误
    Powershell上线MSF
    Alibaba Nacos 认证绕过
    好视通-视频会议存在弱口令&任意文件下载漏洞
    金山 V8 终端安全系统存在默认口令
    (CVE-2021-3297)Zyxel NBG2105身份验证绕过漏洞
  • 原文地址:https://www.cnblogs.com/zyip/p/1450590.html
Copyright © 2011-2022 走看看