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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:38:30+00:00 2026-05-10T19:38:30+00:00

I have an abstract base class with a TcpClient field: public abstract class ControllerBase

  • 0

I have an abstract base class with a TcpClient field:

public abstract class ControllerBase {     internal protected TcpClient tcpClient; 

It has a method to setup a connection:

private void setupConnection(IPAddress EthernetAddress, ushort TcpPort)     {         if (this.tcpClient == null || !this.tcpClient.Connected)         {             this.tcpClient = new TcpClient();              try             {                 this.tcpClient.Connect(EthernetAddress, TcpPort);             }             catch(Exception ex)             {                 throw new TimeoutException('The device did not respond.\n' + ex.Message);             }         }     } 

And than methods to request data:

 internal protected virtual byte[] requestData(IPAddress EthernetAddress, ushort TcpPort, byte[] data, bool IgnoreResponse)     {         setupConnection(EthernetAddress, TcpPort);          //The rest of the code uses this.tcpClient  

There are a few others, such as requestRawData, etc… they are required for very specific hardware communication protocols, but that’s not part of this question in any way.

I then have classes that derive from this class and they override the base class methods:

public class Controller : ControllerBase {   internal virtual byte[] requestData(byte[] data, bool IgnoreResponse)   {         return base.requestData(this.eth0.EthernetAddress, this.eth0.TcpPort, data, IgnoreResponse);   } 

The code works without any exceptions, but everytime the setupConnection method is called, the TcpClient instance (tcpClient) seems to be disposed, so a new one is created and the connect method is called again, really slowing down the communication process.

Note: Public methods of the child class call the requestData method, abstracting many details from the developer using this library.

Such as SetDevicePower(byte PowerLevel), QueryDeviceName() etc…

Code such as this:

Controller controller = new Controller('172.17.0.3',34000); string name = controller.QueryDeviceName(); controller.SetDevicePower(200); 

causes the connect method to be called twice… why is it being disposed between calls?

  • 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. 2026-05-10T19:38:30+00:00Added an answer on May 10, 2026 at 7:38 pm

    There are some inefficiencies in the ‘setupConnection’ method that you may want to look into. The first concern is that you are instantiating the TcpClient when it’s closed. This is not necessary. I would split the null check and connect logic into 2 methods, or at least two code blocks within the method:

      if (this.tcpClient == null)   {     this.tcpClient = new TcpClient();   }    try   {     if (!this.tcpClient.Connected)     {       this.tcpClient.Connect(EthernetAddress, TcpPort);     }   }   catch(Exception ex)   {     throw new TimeoutException('The device did not respond.\n' + ex.Message);   } 

    Secondly, the catch(Exception) is also a bad idea, You cannot assume the exception is a timeout, as there are numerous other exceptions that should be caught here.

    As for your answer: you may have to provide further implementation details within your requestData method, as there could be a clue in there. For example, are you closing the connection? If so, you’d end up creating a new TcpClient object on the next call to setupConnection, which might be what’s happening here.

    Hope this sheds some light.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer From the Javadocs for the URLClassLoader(URL[]) constructor: Constructs a new… May 11, 2026 at 3:13 pm
  • added an answer You must use a dynamic cast. the_iterator = the_vector.begin(); while(… May 11, 2026 at 3:13 pm
  • added an answer Try this out Response.AddHeader('content-disposition', fileName); Response.ContentType = 'application/pdf'; Response.BinaryWrite(byteArray); And… May 11, 2026 at 3:13 pm

Related Questions

In a C# program, I have an abstract base class with a static Create
I have an abstract base class and I want to declare a field or
I have an abstract class defining a pure virtual method in c++: class Base
I have a base class that has an abstract getType() method. I want subclasses

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.