TcpListener包转发

时间:2015-02-04 13:31:45

标签: c# .net tcpclient

我正在编写HTTP(S)+邮件代理服务器。我正在使用TCPListener来捕获数据包。以下是我捕获数据包的代码:

private async Task startAcceptClientsAsync()
{
        while (this.hasServerStarted)
        {
            var acceptedClient = await this.tcpListener.AcceptTcpClientAsync();

            this.tcpClients.Add(acceptedClient);
            IPEndPoint ipEndPoint = (IPEndPoint)acceptedClient.Client.RemoteEndPoint;

            var requestSteram = acceptedClient.GetStream();
            var isRequestAllowed = await this.accessControlManager.IsRequestAllowed(ipEndPoint, requestSteram);

            if (isRequestAllowed)
            {
                // Does not work!!!
                await acceptedClient.ConnectAsync(ipEndPoint.Address.ToString(), ipEndPoint.Port);
            }
            else
            {
                throw new Exception(); // TODO: Handle unauthorized.
            }
        }
}

我正在捕获数据包并根据发送方IP和请求内容是否接受请求做出决定。问题是如果请求被接受我如何发送到默认网关,得到答案并返回?

0 个答案:

没有答案