Now in our last video, we have actually created our center app. And we have created these classes inside that, up to this point, our center app is a Windows Forms application. And we do not have anything in our windows form. So let's design a proper UI so that we can send those commands into our receiver actually. So we are going to just go to our views and then I'm going to enable my toolbox section in that and from toolbox, we are going to drag and drop some basic controls into this. I want one label on the top and this label is going to have one button with that with the textbox.
So we're going to have one button and somewhere in between this we are going to put one text box. So it's going to be the it's something like this that this label is actually going to give me a volume from my TV. So we are going to send the volume with this. So it's going to be volume, whatever we type in the textbox, I want to pass that thing on this particular button. So we are specifying the text set volume. And once the volume is set with that, now let's say that we are going to have one more control on the screen which is going to be maybe my group box.
And this group box is going to have another two controls inside that which are allowing me to maybe turn on and turn off my TV. So we have button two and button three in the button to via setting that we are going to make our television on and then maybe on this we are going to make our television off. So we are going to use this thing with those enum which we have actually created. So we have a group box, and maybe the name of the text of the group box we edifying is going to be my power options. And in this power options, we have two buttons On and off. So we are good to go.
We have three buttons, one text box. Now let me find the click event handlers for all three, so that we can go to our form. And we can write the logical code inside this. So we have button one, button two and button three, these three click event handlers are there in our code behind of our form. And now let me just collapse all this fine. So inside this particular form, now we have to write a code.
The first thing which I'm going to do is I want to create one global object on the top for the television. So I have a private television reference variable on the top. And we are going to initialize this exactly below initialize component inside my phone while initializing this television amplifying that this is going to have this IP and this particular port number, which are actually going to call my constructor and he's going to be passed inside that. After this, we have to read the logic inside this three by And clicks but before that I am creating a simple method with the name execute command and you can see that this execute command is actually going to take one parameter of type my commands. And using this method now I can directly execute my commands whichever I am going to pass into this and different different commands which we are going to create we are going to execute with the help of this because internally This is going to connect with a particular instance of the television and on that particular television we are going to execute that command using this execute command method.
Now we are going to pass on different different commands into our button click event handlers. So my button one is actually having a value of the textbox one into the integer format. So it's going to typecast that thing into integer using convert dot n 32. And then the same value of the volume which we are having, we are going to pass on to the volume command and We are actually passing this command into the execute command, which in turn is internally going to assign this thing to our television. Same way, inside button two, we are going to have execute command, which is actually going to make this setting for power on. And the same way.
The second one is going to do for power settings dot power off. Once you're done with this, we actually send three different commands on three different events. Now it's the time to write the logic to receive this command so that we can see whether the communication is happening properly or not. To check this thing, I'm going to my solution explorer, and now it's the time to deal with the receiver app. Inside the receiver app, I'm going to my program.cs file where I have a main method. And inside this main method, we have to make a connection with our center app.
The first thing inside this main method which I'm going to do is I'm going to create one variable With the name IP and then this one is actually going to have the IP addresses with that. So, we are going to add the namespaces which are required for IP address as well as for the TCP listener. And you can see that we need to add system dotnet and system.net dot sockets, which we have already used in the forms application also. So, using these two classes, we declared that we are going to have this IP and this listener. So, you know that this is actually the same thing what we have specified in our vendor application. And this is actually that particular listener property which helps me to connect this receiver app with the sender.
We are going to write some basic logic to connect and to send the command and my logic looks like this. If you check my code, I have a while loop which first time is going to have a true and inside this we are just specifying listener object dot start. The start is a prediction method, which helps me to do the socket communication between those. And then once the start is just going to show me a console dot write line that waiting for the connection. It's something like this, that when the receiver app is going to be on is going to print this line. And that's the time when we have to wait for the connection to the center app.
After this, we are going to create an object of the socket, which is going to accept the socket based on the listener object in which we have the specific IP and port number. We are specifying the client is connected when we are going to have a connection. And with the center app. Then I'm having a variant data which is going to pass some data from this. And using the socket dot receive we are going to receive the data from the sender app. And whatever data we are receiving, we are just going to encode that thing in the proper string from it.
And then we are passing that thing into the variable with the name input. The same input is going to be printed in our console. So that we can see that Which kind of request is going to be sent by the client? Whatever the sender object is going to send, we are going to receive it and we are going to print this input on our console screen. Same way, maybe we want to respond something. So if the response which is coming from the receiver end, we are going to encode that also and we are going to print the response and we are sending the response back to the sender.
So whatever is received, we are just sending the response to the sender that we got the response. And that response is simple. Okay. Once we are done with this, we are even printing that response is successfully sent to the client. And what is the response in that we are good to go with the basic logic like this. Let's run the receiver app first.
So I'm going to right click on my receiver app and I'm going to debug start new testers. When I do this debug start new instance. It will take some time. And you can see that my receiver app is running and which is still waiting for the connection. So I'm keeping this receiver out. aside, and then we are going to do right click debug start new instance for our sender app.
So sender is running receiver is also running. And now let's try to send some code. The first thing is I'm going to click on this power on button. So when I click on this, you can see that it's showing me that we have connected with the client, the client has requested power on, and the response sent to the client is okay, so it's something like that command is successfully sent. Now, let me set some volume, I'm setting that the volume is going to be 20. And then I'm going to click on the button set volume.
This time it's sending the other command which is volume, and in that is passing the parameter 20 with that, okay, let me increase the volume to maybe 50. And now if I set the volume, you can see it's sending me the same command which is volume with the parameter 50. Once we are done with this, we know that our communication is happening successfully. And now let's power off This particular television, when I do that thing you can see this is sending power zero, and earlier it was power one. So this is a same enum, which we are triggering with that. We can again fire some of the other commands by clicking on this button.
But I hope you're getting the scenario of this design pattern which is common pattern. Thank you so much for watching this video. And if you're watching this full series, and if you are liking this thing, please do not forget to rate our video on Udemy Thank you