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 8024373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:51:13+00:00 2026-06-04T22:51:13+00:00

I am writing a TCP connection based DLL. The basic premise is that it

  • 0

I am writing a TCP connection based DLL.
The basic premise is that it can be used by any client and all the client needs to do is supply an ip address and a port number, the dll then takes care of connecting, transmitting messages, detecting disconnections etc.
The dll exposes 3 events Connected, Disconnected and MessageReceived the client simply wires up to these to use it.
The DLL also defines a base Message-class which clients can inherit from and then pass their own classes/objects into the DLL for sending/receiving.

In the dll I have defined a Packet-class and supporting types:

[Serializable]
internal class Packet
{
    private MessageType _type;
    private MessageItem _item;

    public MessageType MYtpe
    {
        get
        {
             return _type;
        }
        set
        {
            _type = value;
        }
    }

     //similar for MessageItem
     // ...
}

the enum:

public enum MessageType
{
    None,
    PollItem,
    Binary1,
    Binary2
}

and base class for the object/class sending:

public abstract class MessageItem
{
}

My low level, sending the class, code, hidden in the DLL, then is this(without error handling)

internal bool SendPacket(Packet p)
{
    bool sentOk = false;
    BinaryFormatter bin = new BinaryFormatter();
    try
    {
         bin.Serialize(theNetworkStream, p);
    }
    catch etc..
}

ReadPacket is basically the reverse of that.

The dll exposes a function for the client to use that constructs the packet and calls above sending function

public void SendMessage(MessageType type, MessageItem message)

Now for the client.
Because Ive defined 2 binary enum types in the DLL I can use 2 separate classes in my client code.

e.g.

public class Employee : MessageItem
{
     string name;
     //etc
}

and, say:

public class Car : MessageItem
{
    string Model;
    //etc
}

This all works and I can receive either of the two types by doing :

if(myConnection.NextMessageType() == MessageType.Binary1)
{
     Employee e = (Employee)myConnection.ReadMessage();
}
if(myConnection.NextMessageType() == MessageType.Binary2)
{
    Car c = (Car)myConnection.ReadMessage();
}

So long as the client always sends Employee types as binary1 and Car as binary2.

If I want to send a 3rd type of object/class, at the moment I have to go into the dll, add an enum; binary3, rebuild the dll, then I can derive again in my client and use a 3rd if-clause in the above receive code.

if(myConnection.NextMessageType() == MessageType.Binary3)
{
    Animal a = (Animal)myConnection.ReadMessage();
}

So finally onto my question!

is there a way I can avoid having to rebuild the DLL and yet the client can send as many different class types via the DLL as it likes and the send/recieve mechanisms in the DLL (hidden from the client) still work?
I also think the long list of if messagetype == 1, 2 3 etc is highlighting a bad design but I cant figure what a better design would be.

If you got this far and understand what I’m asking, thanks! Would really appreciate a solution.

  • 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-04T22:51:15+00:00Added an answer on June 4, 2026 at 10:51 pm

    Simplest solution would be to change the MessageType enum into a string and get rid of the enum completely. Change:-

    public void SendMessage(MessageType type, MessageItem message)
    

    into just:-

    public void SendMessage(MessageItem message)
    

    and then change the SendPacket to:

    internal bool SendPacket(Packet p)
    {
      bool sentOk = false;
      BinaryFormatter bin = new BinaryFormatter();
      try
      {
        bin.write (p.MessageItem.GetType ().Name); // or whatever the write/serialise function is called
        bin.Serialize(theNetworkStream, p);
      }
      catch etc..
    }
    

    Then, the receiver first reads the string and then uses reflection to create an object of that type, then deserialises into the object, in pseudocode:

    string message_type = ethernet.readstring ();
    Type item_type = convert_name_to_object_type;
    ConstructorInfo constructor = item_type.GetConstructor ();
    object item = constructor.Invoke ();
    ethernet.Deserialise (item);
    invoke MessageReceived event (item);
    

    That last part could use a SortedDicationary <message type name, message handler delegate> to dispatch the message to the right handler (but this forces the handlers to typecast the MessageItem parameter into the correct message type). Alternatively, use reflection again to find a method called “HandleEthernetMessage” that takes one parameter of the type received over the ethernet. SortedDicationary <message type name, method info> would be quicker at runtime and overcome the first SortedDictionary issues.

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

Sidebar

Related Questions

I'm writing a small (C#) client application that sends data using a TCP/IP connection
I'm writing a tcp client in Delphi for a server that has a series
I am writing client for TCP connection and conversion from IP to socket_addr makes
I am writing a TCP server application using Winsock. The client that connects to
I'm writing a custom TCP server and client and on doing a ton of
I am writing some java TCP/IP networking code ( client - server ) in
I am writing a simple C# tcp client and server program. The server will
I am writing a VB.NET app that connects to a legacy application using TCP.
I’m trying to write a custom TCP based long polling server that will serve
I'm writing a Python program that will use Twisted to connect to a TCP

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.