C#将两个tcp套接字连接在一起

时间:2012-09-05 18:25:53

标签: c# sockets telnet tcpclient

我不确定标题是否具有丰富的信息。

我正在尝试查找/编写一个套接字服务器,它将接受来自客户端(telnet)的连接,然后代表连接的客户端,连接到网络内的四个telnet服务器之一。

一旦连接,我会保留一个计数器,显示有多少连接,然后如果有4个连接,则禁止任何新连接,直到其中一个连接可用。

我写了这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
    static int nodeCount = 4;
    static int currentNode = 1;

    static void Main(string[] args)
    {
        ServerProgram server = new ServerProgram();
    }

    class ServerProgram
    {
        private TcpListener tcpPrimaryListener;
        private Thread listenThread;

        public ServerProgram()
        {
            this.tcpPrimaryListener = new TcpListener(IPAddress.Any, 23);
            Console.WriteLine("Telnet BBS Port Concentrator Server Started.");
            Console.WriteLine("--------------------------------------------");

            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
        }

        private void ListenForClients()
        {
            this.tcpPrimaryListener.Start();

            while (true)
            {
                TcpClient client = this.tcpPrimaryListener.AcceptTcpClient();
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }

        private void HandleClientComm(object client)
        {
            if (currentNode <= nodeCount)
            {
                Console.WriteLine("Connection thread created.");

                StreamWriter swStream;
                StreamWriter swStream2;
                StreamReader srStream;
                StreamReader srStream2;

                TcpClient tcpClient = (TcpClient)client;
                NetworkStream tcpClientStream = tcpClient.GetStream();

                TcpClient telnet = new TcpClient("192.168.100.5" + currentNode, 23);
                NetworkStream telnetStream = telnet.GetStream();

                currentNode++;

                while (true)
                {
                    srStream = new StreamReader(tcpClient.GetStream());
                    swStream2 = new StreamWriter(tcpClient.GetStream());

                    srStream2 = new StreamReader(telnet.GetStream());
                    swStream = new StreamWriter(telnet.GetStream());

                    swStream.Write(srStream.ReadToEnd());
                    swStream2.Write(srStream2.ReadToEnd());

                }
            }
        }
    }
}
}

我已经多次改变了这个例子,所以我真的不知道我拥有什么并且没有尝试过。我愿意尝试任何事情。

目的实际上是为了允许一个telnet端口通过防火墙打开,并允许连接到运行telnet fossil驱动程序BBS软件的DOS机器的小型网络。我只想使用一个端口将telnet流量重定向到可用系统。

问题在于我无法弄清楚如何将两个套接字实际连接在一起并在它们之间传递数据。传入的套接字和我代表服务器创建的套接字到服务器。

感谢。

更新:

这对我有用,我仍然在寻找bug,但它到目前为止还在工作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static int nodeCount = 2;
    static int currentNode = 1;

    static void Main(string[] args)
    {
        ServerProgram server = new ServerProgram();
    }

    class ServerProgram
    {
        private TcpListener tcpPrimaryListener;
        private Thread listenThread;

        public ServerProgram()
        {
            this.tcpPrimaryListener = new TcpListener(IPAddress.Any, 23);
            Console.WriteLine("Telnet BBS Port Concentrator Server Started.");
            Console.WriteLine("--------------------------------------------");

            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
        }

        private void ListenForClients()
        {
            this.tcpPrimaryListener.Start();

            while (true)
            {
                TcpClient client = this.tcpPrimaryListener.AcceptTcpClient();
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }

        private void HandleClientComm(object client)
        {
            string noNodes = "Sorry all nodes are occupied.";

            if (currentNode <= nodeCount)
            {
                Console.WriteLine("Client connected.");

                TcpClient tcpClient = (TcpClient)client;
                NetworkStream tcpClientStream = tcpClient.GetStream();

                TcpClient telnet = new TcpClient("10.24.9.11", 23);
                //TcpClient telnet = new TcpClient("192.168.100.5" + currentNode, 23);
                NetworkStream telnetStream = telnet.GetStream();

                currentNode++;

                ByPass linkedSockets = new ByPass(tcpClientStream, telnetStream);
            }
            else
            {
                TcpClient tcpClient = (TcpClient)client;
                NetworkStream tcpClientStream = tcpClient.GetStream();

                ASCIIEncoding encoder = new ASCIIEncoding();
                tcpClientStream.Write(Encoding.ASCII.GetBytes(noNodes), 0, noNodes.Length);
            }
        }
    }

    public class ByPass
    {
        public ByPass(Stream s1, Stream s2)
        {
            var cTokenSource = new CancellationTokenSource();
            var cToken = cTokenSource.Token;
            Task.Factory.StartNew(() => Process(s1, s2, cToken, cTokenSource), cToken);
            Task.Factory.StartNew(() => Process(s2, s1, cToken, cTokenSource), cToken);
            cToken.Register(() => cancelNotification());
        }

        public void Process(Stream s1, Stream s2, CancellationToken ct, CancellationTokenSource cTokenSource)
        {
            byte[] buf = new byte[0x10000];

            while (true)
            {
                if (ct.IsCancellationRequested)
                {
                    break;
                }

                try
                {
                    int len = s1.Read(buf, 0, buf.Length);
                    s2.Write(buf, 0, len);
                }
                catch
                {
                    s1.Close(); s2.Close();
                    cTokenSource.Cancel();
                    break;
                }
            }
        }
    }

    static void cancelNotification()
    {
        Console.WriteLine("Client disconnected.");
        currentNode--;
    }
}
}

2 个答案:

答案 0 :(得分:5)

我认为,你可以创建一个类似于下面的类来在两个流之间传递数据

public class ByPass
{
    public ByPass(Stream s1, Stream s2)
    {

        Task.Factory.StartNew(() => Process(s1, s2));
        Task.Factory.StartNew(() => Process(s2, s1));
    }

    public void Process(Stream sIn, Stream sOut)
    {
        byte[] buf = new byte[0x10000];
        while (true)
        {
            int len = sIn.Read(buf, 0, buf.Length);
            sOut.Write(buf, 0, len);
        }
    }
}

答案 1 :(得分:0)

我做了一点改变,它在我身边很完美

    public class StreamTransmitter
    {
        static TaskCompletionSource<bool> ts;

        public static async Task Start(Stream s1, Stream s2, CancellationToken token)
        {
            ts = new TaskCompletionSource<bool>();

            Process(s1, s2, token);
            Process(s2, s1, token);

            await ts.Task;
        }

        private static async Task Process(Stream sIn, Stream sOut, CancellationToken token)
        {
            byte[] buf = new byte[0x10000];
            int len = 0;
            do
            {
                len = await sIn.ReadAsync(buf, 0, buf.Length, token);
                await sOut.WriteAsync(buf, 0, len, token);
            }
            while (len > 0 && !token.IsCancellationRequested);

            ts.SetResult(true);
        }
    }