0

我有 2 个应用程序

一种是作为服务器监听 80 和 4026 端口

第二个应用程序,连接到端口 80 和端口 4026

所以两者都通过端口 4026 进行通信以向我传输数据。

我正在尝试通过端口 80 上的 Firefox 本地连接到第一个应用程序,然后让第二个应用程序连接到 google.com 获取网站上的端口 80 并将其带回给我。

所以输入 localhost:80 应该会给我带来谷歌的网页

这是用于中继数据的第一个应用程序代码片段。Relaydata1 和 Relaydata2 都是一起运行的线程。

public class serv
{

public static Socket socket1;
public static Socket socket2;

public static void Main()
{

        IPAddress ipAd = IPAddress.Parse("127.0.0.1");
        IPAddress ipAd2 = IPAddress.Parse("127.0.0.1");

        TcpListener myList1 = new TcpListener(ipAd, 80);
        TcpListener myList2 = new TcpListener(ipAd, 4026);


        myList2.Start();
        myList1.Start();



        Socket s2 = myList2.AcceptSocket();

        Console.WriteLine("Alright, someone connected from outside. " + s2.RemoteEndPoint + "   now open up the browser and connect to localhost port 80");

        Socket s1 = myList1.AcceptSocket();

        socket1 = s1;
        socket2 = s2;

        Thread t1 = new Thread(new ThreadStart(Relaydata1));
        t1.Start();

        Thread t2 = new Thread(new ThreadStart(Relaydata2));
        t2.Start();
}

private static void Relaydata1()
{
    while (true)
    {
        byte[] b = new byte[3072];
        socket1.Receive(b);
        socket2.Send(b);

    }
}

private static void Relaydata2()
{
    while (true)
    {
        byte[] b = new byte[3072];
        socket2.Receive(b);
        socket1.Send(b);
    }
}
}

这是第二个应用程序的片段。同样,这两个例程都作为线程运行。

public class clnt
{
    static Stream stream1;
   static  Stream stream2;

    public static void Main()
    {
        TcpClient tcpclnt1 = new TcpClient();
        TcpClient tcpclnt2 = new TcpClient();

        tcpclnt1.Connect("127.0.0.1", 4026);  
        tcpclnt2.Connect("173.194.39.39", 80); 



        Stream stm1 = tcpclnt1.GetStream();
        Stream stm2 = tcpclnt1.GetStream();


        stream1 = stm1;
        stream2 = stm2;

        Thread t1 = new Thread(new ThreadStart(Relay1));
        t1.Start();

        Thread t2 = new Thread(new ThreadStart(Relay2));     
        t2.Start();



}

public static void Relay1()
{
    while (true)
    {

        byte[] bb = new byte[3072];
        stream1.Read(bb, 0, 3072);
        stream2.Write(bb, 0, bb.Length);
        stream2.Flush();

    }

}

public static void Relay2()
{

    while (true)
    {
        byte[] bb = new byte[3072];
        stream2.Read(bb, 0, 3072);
        stream1.Write(bb, 0, bb.Length);
        stream1.Flush();
    }


}
}

当我在 Firefox 中键入 localhost:80 时,它会询问我是否要下载八位字节流,并且在接受时会卡在“开始”

我不明白为什么它不起作用我基本上是按原样将数据从谷歌转发到我的服务器?

4

0 回答 0