Reply 60 of 64, by Caligula
Myg wrote on 2025-01-01, 01:48:So how exactly do you imagine this should work? I've been thinking about it a bit more and was curious what I could actually add to the equation per say.
I just asked ChatGPT "How do I write a TCP server in C# that accepts two connections and forward the incoming data from one connection to the other?"
Reply (I just added Task.WaitAll to the code, otherwise it terminates immediately):
using System.Net;
using System.Net.Sockets;
class TcpServer
{
private const int Port = 5000; // Port number to listen on
private static TcpListener _tcpListener;
private static TcpClient _client1 = null;
private static TcpClient _client2 = null;
public static void Main(string[] args)
{
// Start the TCP listener
_tcpListener = new TcpListener(IPAddress.Any, Port);
_tcpListener.Start();
Console.WriteLine($"Server listening on port {Port}...");
// Accept exactly two client connections
AcceptClients().Wait();
}
private static async Task AcceptClients()
{
// Wait for the first client connection
_client1 = await _tcpListener.AcceptTcpClientAsync();
Console.WriteLine("First client connected.");
// Wait for the second client connection
_client2 = await _tcpListener.AcceptTcpClientAsync();
Console.WriteLine("Second client connected.");
// Handle the data forwarding between the two clients
var task1 = Task.Run(() => ForwardData(_client1, _client2)); // Forward from client1 to client2
var task2 = Task.Run(() => ForwardData(_client2, _client1)); // Forward from client2 to client1
Task.WaitAll([task1, task2], -1);
}
private static async Task ForwardData(TcpClient fromClient, TcpClient toClient)
{
using (fromClient)
using (toClient)
{
NetworkStream fromStream = fromClient.GetStream();
NetworkStream toStream = toClient.GetStream();
byte[] buffer = new byte[1024];
int bytesRead;
try
{
// Forward data from 'fromClient' to 'toClient'
while ((bytesRead = await fromStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
Console.WriteLine($"Forwarding {bytesRead} bytes from {fromClient.Client.RemoteEndPoint} to {toClient.Client.RemoteEndPoint}");
// Write the data to the other client's stream
await toStream.WriteAsync(buffer, 0, bytesRead);
await toStream.FlushAsync();
}
Console.WriteLine("Client disconnected.");
}
catch (Exception ex)
{
Console.WriteLine($"Error in data forwarding: {ex.Message}");
}
}
}
}
This works already well with unmodified versions of Dosbox 0.74.3 and Dosbox-Staging (tested with DOOM2):
serial1 nullmodem port:5000 server:localhost
sersetup.exe
Main issues are that this code cannot handle any reconnect right now.