How to use sockets to send data from a C# application to a server/webpage

Continuing the current them of working with NodeJS and sockets before I go on holiday for a couple of weeks, this post is going to look at how we can send data from a C# application to a server/webpage using Javascript and NodeJS. Previously in this theme we've looked at using NodeJS and Socket.IO to create a simple chat application before upgrading and looking at how we can pass data between Javascript and a C# application.

This time, we're going to look at how we can read data in from the terminal in C#, and display it on a webpage on our local host. For this project, you're going to need NodeJS installed, along with Socket.IO.

Creating the Javascript file

First of, let's create our Javascript file, as we've done previously. Make a new Javascript file (I've called mine "webServer.js") and paste the following code (comments hopefully provide explanations of what's going on - if you have any questions feel free to leave a comment).

Creating the HTML file

Now that we have our Javascript file, we need an HTML file to display our messages on. Make a new HTML file (I've named mine "index.html" - if you name yours something different you will need to change line 10 in the Javascript file accordingly) and populate it with the following mark-up.

Creating the C# application

The C# application is fairly simple. It'll simply read data in from the command line (or, if you chose to implement it, a full UI) and broadcast it to the socket connected to the webpage, and display it on that webpage.

Our main method will handle reading data, while a separate function will handle sending the data to the socket. We'll be using the same includes as we did previously, which gives us a main method like so:

This handles the data input and checks if the user wishes to exit by typing the word 'exit' on its own. If 'exit' is included in a sentence, or with other chars, the if statement will return false and will display that data on the webpage.

Displaying the data is handled by the SendData method, which connects to the socket and sends the data on towards its final destination.

Add this below the main method and viola, providing the connection can be established, you'll now be able to type commands in the C# terminal and have them displayed on the webpage.

Testing the application

To test the application, turn on the Javascript server by getting your command line pointing to the directory of this project and typing
node webServer.js
remembering to replace webServer.js with the name of your Javascript file.

Then navigate to your local host remembering which port you have everything set up on (if you've copied the code here, that'll be port 3000) and start your C# application. If everything has worked correctly, you should see something like the image below between the two terminal windows and a webpage.

Expected output if everything has worked.

Expected output if everything has worked.

That's it, now you have a C# application which is capable of sending data to a webpage using NodeJS and sockets. You can put a GUI on the C# application and you could end up with a very nice piece of kit communicating with the World Wide Web. The possibilities are practically endless...

Comments

  1. By John

  2. By FraserG

Leave a Reply

Your email address will not be published. Required fields are marked *