1

C# で単純な TCP ネットワーク トンネルを作成しようとしています (VS ではなく MonoDevelop を使用)。現在、接続するところまで動作しています。に接続した後netcat localhost <portnum>、ヌルで満たされたパケットが絶えず送信されてきます。私のコードは次のとおりです。

Tunnel.cs

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class Tunnel {
    public TcpListener listener;
    public NetworkStream local, remote;
    private Thread threadControl, threadLocalSide, threadRemoteSide;
    public Tunnel(int localPort, String remoteServer, int remotePort) {
        this.listener = new TcpListener(new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), localPort));
        this.remote = new TcpClient(remoteServer, remotePort).GetStream();
        this.threadControl = new Thread(new ThreadStart(this.thread));
        this.threadControl.Start();
    }
    public void thread() {
        this.listener.Start();
        Console.WriteLine("Awaiting connection...");
        this.local = this.listener.AcceptTcpClient().GetStream();
        Console.WriteLine("Tunnel connected!");
        Console.WriteLine("Starting threads...");
        this.threadLocalSide = new Thread(new ThreadStart(this.localSide));
        this.threadLocalSide.Start();
        this.threadRemoteSide = new Thread(new ThreadStart(this.remoteSide));
        this.threadRemoteSide.Start();
    }
    public void localSide() {
        byte[] buffer = new byte[2048];
        try {
            while(true) {
                while(!this.local.DataAvailable);
                this.local.Read(buffer, 0, buffer.Length);
                this.remote.Write(buffer, 0, buffer.Length);
            }
        } catch {
            this.threadRemoteSide.Abort();
        }
    }
    public void remoteSide() {
        byte[] buffer = new byte[2048];
        try {
            while(true) {
                while(!this.remote.DataAvailable);
                this.remote.Read(buffer, 0, buffer.Length);
                this.local.Write(buffer, 0, buffer.Length);
            }
        } catch {
            this.threadLocalSide.Abort();
        }
    }
}
4

1 に答える 1