Created
May 13, 2013 22:10
-
-
Save Raggles/8a7aa18307a0b58240fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Net.Sockets; | |
using System.Threading; | |
using System.Net; | |
namespace HC.SseTcpListenerPassThru | |
{ | |
/// <summary> | |
/// Joins the traffic between two programs that both | |
/// initiate connections (ie two clients trying to connect to each other) | |
/// If one client disconnects the program closes the connection, then | |
/// listens again on the port in case the program tries to reconnect. | |
/// </summary> | |
class Program | |
{ | |
private static List<TcpClient> clients = new List<TcpClient>(); | |
public static TcpListener tcpListener1; | |
public static TcpListener tcpListener2; | |
private delegate void CommsHandler(TcpClient client1, TcpClient client2); | |
/// <summary> | |
/// Arg1: Port 1 to listen on | |
/// Arg2: Port 2 to listen on | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
tcpListener1 = new TcpListener(IPAddress.Any, int.Parse(args[0])); | |
tcpListener2 = new TcpListener(IPAddress.Any, int.Parse(args[1])); | |
ReconnectClient(tcpListener1); | |
ReconnectClient(tcpListener2); | |
} | |
catch (Exception ex) | |
{ | |
Console.Write(ex.ToString()); | |
} | |
} | |
private static void ReconnectClient( TcpListener listener) | |
{ | |
Thread listenThread = new Thread(unused => ListenForClient(listener)); | |
listenThread.Start(); | |
} | |
public static void ListenForClient( TcpListener listener) | |
{ | |
listener.Start(); | |
Console.WriteLine("Listening for client... " + listener.Server.LocalEndPoint.ToString()); | |
//blocks until a client has connected to the server | |
TcpClient client = listener.AcceptTcpClient(); | |
clients.Add(client); | |
listener.Stop(); | |
Console.WriteLine("Client connected, stopped listening on " + listener.LocalEndpoint.ToString()); | |
HandleClientComms(listener, client); | |
} | |
public static void HandleClientComms( TcpListener listener, TcpClient client) | |
{ | |
NetworkStream clientStream = client.GetStream(); | |
byte[] message = new byte[4096]; | |
int bytesRead; | |
while (true) | |
{ | |
bytesRead = 0; | |
try | |
{ | |
//blocks until a client sends a message | |
bytesRead = clientStream.Read(message, 0, 4096); | |
} | |
catch | |
{ | |
//client has disconnected or socket error | |
Console.WriteLine("Client disconnected or socket error on " + client.Client.LocalEndPoint.ToString()); | |
break; | |
} | |
if (bytesRead == 0) | |
{ | |
//the client has disconnected from the server | |
Console.WriteLine("Client disconnected on " + client.Client.LocalEndPoint.ToString()); | |
break; | |
} | |
foreach (var otherClient in clients) | |
{ | |
if (otherClient == client) | |
continue; | |
if (otherClient.Connected) | |
{ | |
NetworkStream stream = otherClient.GetStream(); | |
stream.Write(message, 0, bytesRead); | |
stream.Flush(); | |
} | |
} | |
} | |
client.Close(); | |
clients.Remove(client); | |
ReconnectClient(listener); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment