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

  • Home
  • SEARCH
  • 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 8653165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:28:40+00:00 2026-06-12T14:28:40+00:00

I am in need of a C# layer which will help to exchange the

  • 0

I am in need of a C# layer which will help to exchange the data between two TCP ports which are listening.

For example, There is a listener port @ 192.168.1.2::5555 and another listener port @ 192.168.1.4::6666.

I am able to establish the connection to both the listeners using socket.connect
I am getting confused during creation of 2 threads
1> Sock1.read()->convert to bytes ->sock2 .write()
2> Sock2.read()->Convert to bytes -> Sock1.write()

I think this is entering into the infinite loop. Is there any better way of exchanging packets between 2 Listening ports by establishing connection to both ports?

I have to implement a method

Private void ExchangePackets(IpEndpoint ipe1,IpEndpoint ipe2)
{
//code here
}
  • 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-12T14:28:41+00:00Added an answer on June 12, 2026 at 2:28 pm

    $Finally Achieved it 🙂 $

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

Sidebar

Related Questions

I have an Android MapView in which i need to have a second layer
I need to create some simple effects for the layers which I have created
I need a custom layer (extended by new variable and a method) and did
I need to make a div layer so that when you click on it
Within my logic layer I have the need to check permissions for a wide
What I need is a way to modify a vector layer representation without downloading
I am working on a prototype project and need to put a new layer
I have the standard repository pattern with a service layer and I need to
I need to set up two machines later today for a domo of a
I wonder if you can help me. I'm writing a game (2d) which allows

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.