Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:50:57+00:00 2026-06-10T19:50:57+00:00

I’m not sure if the title is all that informative. I am trying to

  • 0

I’m not sure if the title is all that informative.

I am trying to find/write a socket server that will accept a connection from the client (telnet) and then on behalf of the connected client, connect to one of four telnet servers inside the network.

Once connected I keep a counter of how many connections there are, and then if there are 4 total connections, disallow any new connections until one of the four is available.

I have written this:

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());

                }
            }
        }
    }
}
}

I’ve changed this example multiple times, so I don’t really know anymore what I have and have not tried. I’m willing to try anything.

The purpose is actually running this to allow one telnet port open through the firewall, and allowing connections into a small network of DOS machines running telnet fossil driver BBS software. I would just like to redirect telnet traffic to an available system using only one port.

The problem is that I cannot figure out how to actually connect the two sockets together and pass data between them as it happens. The incoming socket and the socket I created on behalf of the server to the server.

Thanks.

UPDATE:

This is what is working for me, I’m still looking over for bugs but it’s working so far.

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--;
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:50:58+00:00Added an answer on June 10, 2026 at 7:50 pm

    I think, you can create a class similar to below to pass data between two streams

    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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.