2

我需要一个 C# 层来帮助在两个正在侦听的 TCP 端口之间交换数据。

例如,有一个监听端口@192.168.1.2::5555 和另一个监听端口@192.168.1.4::6666。

我能够使用 socket.connect 建立与两个侦听器的连接我在创建 2 个线程期间感到困惑 1> Sock1.read()->convert to bytes ->sock2 .write() 2> Sock2.read() -> 转换为字节 -> Sock1.write()

我认为这是进入无限循环。有没有更好的方法通过建立到两个端口的连接在两个监听端口之间交换数据包?

我必须实现一个方法

Private void ExchangePackets(IpEndpoint ipe1,IpEndpoint ipe2)
{
//code here
}
4

3 回答 3

2

它可以是这样的:(未测试)

void ExchangePackets(IPEndPoint ipe1, IPEndPoint ipe2)
{
    TcpClient tcp1 = new TcpClient();
    tcp1.Connect(ipe1);

    TcpClient tcp2 = new TcpClient();
    tcp2.Connect(ipe2);

    Task.Factory.StartNew(() => ByPass(tcp1, tcp2), TaskCreationOptions.LongRunning);
    Task.Factory.StartNew(() => ByPass(tcp2, tcp1), TaskCreationOptions.LongRunning);
}

void ByPass(TcpClient tcp1, TcpClient tcp2)
{
    using (tcp1)
    using (tcp2)
    {
        Stream s1 = tcp1.GetStream();
        Stream s2 = tcp2.GetStream();
        byte[] buf = new byte[0x10000];

        int len = s1.Read(buf, 0, buf.Length);
        while (len > 0)
        {
            s2.Write(buf, 0, len);
            len = s1.Read(buf, 0, buf.Length);
        }
    }
}
于 2012-10-10T08:39:34.577 回答
1

$终于实现了:-) $

     private void Form1_Load(object sender, EventArgs e)
    {
        RoutingClient=new TcpClient("127.0.0.1",5765);
        ServerClient = new TcpClient("127.0.0.1", 5766);
        rcStream = RoutingClient.GetStream();
        ScStream = ServerClient.GetStream();
        //start 2 threads
        Thread t1 = new Thread(()=>ExchangePackets(rcStream,ScStream));
        t1.Start();
        Thread t2 = new Thread(() => ExchangePackets(ScStream, rcStream));
        t2.Start();
    }


    private  static void ExchangePackets(NetworkStream FirstStream, NetworkStream SecondStream)
    {
        try
        {
            while (true)
            {
                if (FirstStream.CanRead)
                {
                    byte[] myReadBuffer = new byte[1024];
                    StringBuilder myCompleteMessage = new StringBuilder();
                    MemoryStream ms = new MemoryStream();
                    int numberOfBytesRead = 0;
                    int TotalBytesRead = 0;
                    // Incoming message may be larger than the buffer size. 
                    do
                    {
                        numberOfBytesRead = FirstStream.Read(myReadBuffer, 0, myReadBuffer.Length);
                        ms.Write(myReadBuffer, TotalBytesRead, numberOfBytesRead);
                        myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
                        TotalBytesRead = TotalBytesRead + numberOfBytesRead;
                    }
                    while (FirstStream.DataAvailable);
                    MessageBox.Show("You received the following message : " +
                                                 myCompleteMessage);
                    if (SecondStream.CanWrite)
                    {

                        byte[] myWriteBuffer = ms.ToArray();
                        SecondStream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
                    }
                    else
                    {
                        MessageBox.Show("Sorry.  You cannot write to this NetworkStream.");
                    }
                }
                else
                {
                    MessageBox.Show("Sorry.  You cannot read from this NetworkStream.");
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Routing to Server:" + ex.Message);
        }
    }
于 2012-10-10T10:00:02.793 回答
0

编辑:仔细检查后,我认为我的回答不适合您的情况。您的问题描述了许多可能阻止您以这种方式实现它的固定约束。不过,我会将我的答案留给其他人,因为我确实认为这通常是正确的方法。

有没有更好的方法通过建立到两个端口的连接在两个监听端口之间交换数据包?

你不需要重新发明轮子:)

您可以使用WCF使用TCP 传输创建全双工服务吗?

于 2012-10-09T13:13:42.233 回答