English 中文(简体)
建设C# asnchronous TCP proxy服务器
原标题:building a C# asynchronous TCP proxy server

我试图为我的业务建立一个简单的C# TCP代理人,以便我能够阻止我雇员的某些网站。 除了我难以看到用户试图访问哪一个网站外,所有网站都很好。 我可以看到,用户与我的代理服务器有关系,因此我知道我正在联系,但OnRecieve报警甚至连发射。 我读一下一下从一纸空文中看错了吗?

Here is my code:

internal class AsyncState
{
    public const int BufferSize = 4096;
    public byte[] Buffer = new byte[AsyncState.BufferSize];
    public Socket Socket;
    public StringBuilder Content = new StringBuilder();
}

private void OnLoad(object sender, EventArgs e)
{
    IPAddress[] addressCollection = Dns.GetHostAddresses(Dns.GetHostName());
    foreach (IPAddress ipAddress in addressCollection)
    {
        if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
        {
            localEndPoint = new IPEndPoint(ipAddress, 8080);
            Console.WriteLine("Local IP address found... " + localEndPoint.ToString());
            break;
        }
    }

    isListening = true;

    thread = new Thread(new ThreadStart(
        delegate()
        {
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(localEndPoint);
            serverSocket.Listen(10);

            while (isListening)
            {
                resetEvent.Reset();

                Console.WriteLine("Waiting for clients...");
                serverSocket.BeginAccept(new AsyncCallback(OnAccept), serverSocket);

                resetEvent.WaitOne();
                }
            }));

        thread.Start();
    }
}

private void OnAccept(IAsyncResult result)
{
    resetEvent.Set();

    Socket clientSocket = (result.AsyncState as Socket).EndAccept(result);
    Console.WriteLine("Client has connected... " + clientSocket.RemoteEndPoint.ToString());

    AsyncState state = new AsyncState();
    state.Socket = clientSocket;
    state.Socket.BeginReceive(state.Buffer, 0, AsyncState.BufferSize, SocketFlags.None, new AsyncCallback(OnRecieve), state);
}

private void OnRecieve(IAsyncResult result)
{
    AsyncState state = result.AsyncState as AsyncState;

    int totalRead = state.Socket.EndReceive(result);
    if (totalRead > 0)
    {
        state.Content.Append(Encoding.ASCII.GetString(state.Buffer, 0, totalRead));
        state.Socket.BeginReceive(state.Buffer, 0, AsyncState.BufferSize, SocketFlags.None, new AsyncCallback(OnRecieve), state);
    }
    else
    {
        if (state.Content.Length > 1)
            Console.WriteLine("Message recieved from client... " + state.Content.ToString());

        state.Socket.Close();
    }
}
最佳回答

建立行之有效的代理机构并非简单的任务,因为你必须理解和处理吉大港山区问题等。

我建议要么使用现有的图书馆,作为该图书馆的某种可图的代号。

REMARK:

我不知道,在什么法域里,如果雇员不知情或同意,使用这种技术,在某些地方可能是一个问题。

另一点: 我将不使用这些方法,而是告诉雇员停止滥用公司1至3次的互联网连接,如果这样做,我会向此人开火......这些雇员不仅滥用公司的互联网连接,而且在最坏的情况下将公司置于风险之下(病毒/暴风雨等),而且欺骗公司(如果他在工作时间这样做的话)。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签