예제 #1
0
 static void Main(string[] args)
 {
     Console.ReadLine();
     Console.WriteLine("Hello World!");
     Console.WriteLine("你好!");
     try
     {
         int a = 0;
         int b = 10 / a;
     }
     catch (Exception ex)
     {
         UtilesHelper.Error(ex);
         //throw;
     }
 }
예제 #2
0
파일: ProxyIns.cs 프로젝트: staor/Demo
        public async Task StartTcpProxyAsync(IpPort pIpPort)
        {
            ipPort = pIpPort;
            try
            {
                if (ipPort.ToIps.Count != ipPort.ToPorts.Count || ipPort.ToPorts.Count == 0)
                {
                    return;
                }
                if (tcpListener == null)
                {
                    if (string.IsNullOrEmpty(ipPort.FromIp))
                    {
                        tcpListener = new TcpListener(IPAddress.Any, ipPort.FromPort);
                    }
                    else
                    {
                        IPAddress ipa = null;
                        if (IPAddress.TryParse(ipPort.FromIp, out ipa))
                        {
                            tcpListener = new TcpListener(ipa, ipPort.FromPort);
                        }
                    }
                }
                if (tcpListener == null)
                {
                    return;
                }
                ToClients = new List <TcpClient>();
                for (int i = 0; i < ipPort.ToPorts.Count; i++)
                {
                    TcpClient toClient = new TcpClient();
                    ToClients.Add(toClient);
                    toClient.Connect(ipPort.ToIps[i], ipPort.ToPorts[i]);
                }
                tcpListener.Start();
                UtilesHelper.CWshow("Tcp开始侦听:" + ipPort.FromIp + ":" + ipPort.FromPort);
                var tocken = ctsTcp.Token;
                tcpListener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), tcpListener);


                while (!tocken.IsCancellationRequested)
                {
                    TcpClient client = await tcpListener.AcceptTcpClientAsync();

                    for (int i = 0; i < ToClients.Count; i++)
                    {
                        if (!ToClients[i].Connected)
                        {
                            ToClients[i].ConnectAsync(ipPort.ToIps[i], ipPort.ToPorts[i]);
                        }
                    }
                    ProxyTcp(client);
                }
                tcpListener.Stop();
            }
            catch (Exception ex)
            {
                UtilesHelper.Error(ex);
                UtilesHelper.CWshow("StartTcpProxy-" + ex.Message);
            }
        }