Question

I have created an asp.net website. On that website the user can create webpages and put buttons. These buttons sends commands via web socket to a tray application I implemented. The tray application then takes the commands and passes them along to various third-party applications using TCP connections. the third-party applications then perform the commands and send back their status to the tray application. The tray application writes the status into an sql database. The website reads the status from the database and processes the status (for example, it highlights the button that send the original command)

Right now it is a mess. For example the third-party applications all handle the communication in different ways. How would I best go about organizing such an application?

I use ab sql database in between because I cannot be sure that the user wont reload the website. And then what happens to the web socket? Also, it can update its buttons on its own pace. Is that a good solution?

Also, my knowledge of TCP and socket programming is sorely lacking. Most resources, tutorials, guides just give really simple examples. Isn't there something better out there? (books, articles)

I am using a TCPListener that accepts the website requests in an infinite loop:

static void TcpListenerWorker_DoWork(object sender, DoWorkEventArgs e)
{
    server = new TcpListener(IPAddress.Parse(Properties.Settings.Default.TrayAppServer), Properties.Settings.Default.TrayAppPort);

    // Start listening for client requests.
    server.Start();

    // Buffer for reading data
    Byte[] bytes = new Byte[256];
    String data = null;

    // Enter the listening loop.
    while (true)
    {
        if (TcpListenerWorker.CancellationPending)
            break;

        // Perform a blocking call to accept requests.
        // You could also user server.AcceptSocket() here.
        TcpClient client = server.AcceptTcpClient();

        ...

Is there a better way to link the tray application to the website?

The tray application also has to check if the third-party application is running. Only if it is running, should the socket be created. If the third-party application crashes to connection is gone. How do I reestablish the connection properly?

The user should now if he can send commands. For that I check if the application is running like so

Process.GetProcessesByName("exApp1");

Is there a better way to check for the applications? Maybe by trying to establish a tcp connection and if it fails, the app is not running. But relying on a failed connection attempt might not be good decision.

A lot of errors occur. Connections disappear, commands do not arrive. Are there APIs I could use to handle these things better (better error and exception handling)?

Sorry, if all that sounds confusing. The entire project is a mess. Please tell me if I should explain more.

Was it helpful?

Solution

One thing I would do is sit back and unravel the spaghetti in your mind first. The fact ech uses a different means of communication should not matter if you have properly abstracted them.

The first question is "Do you need TCP socket level communication?" I see nothing that screams "yes" here. Assuming you do, your first issue is getting the socket communication. Get that up and running first. If this is multiple clients, there are open source projects you can start with to handle the communication bits. As an example, there is a project called Socket Server on CodePlex that can manage the sockets. I am, personally, not fond of the way the project is set up, but follow the documentation and get a server up. Or search for another piece that can do this for you.

Your next problem is "should I set up a socket?". If I am right, this sounds like a client side issue; if so, solve it separate from the socket connection and be done with it. I don't have bandwidth to determine if your method is the best here, but encapsulate that code and you can swap out the method of determining if the app is running later.

Now let's jump down to multiple apps communicate differently. If you have one in to multiple outs, think about a simplified service bus and then write adapters for the applications that need to listen. If it is multiple in to one out, then you need to write an adapter for the application anyway. Perhaps some apps do require socket level communication, and you need a socket to web API adapter (just an example, as I don't have enough detail).

The core of the advice is break the problem down and see what you can separate out and focus on. Many simple problems are generally easier to solve than one big complex problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top