gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:49:53 24 4
gpt4 key购买 nike

我不确定标题是否提供了所有信息。

我正在尝试查找/编写一个套接字服务器,它将接受来自客户端 (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 driver BBS 软件的DOS 机器的小型网络。我只想使用一个端口将 telnet 流量重定向到可用系统。

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

谢谢。

更新:

这就是对我有用的东西,我仍在寻找错误,但到目前为止它还在工作。

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--;
}
}
}

最佳答案

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

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

关于C# 将两个 tcp 套接字连接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12287586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com