Tutorial: Using Sockets In C# Dr. Thomas E. Hicks Computer Science Department Trinity University About This Tutorial This tutorial will be a bit confusing in that we shall attempt to work with two different C# applications somewhat simultaneously. A single program, which functions as both a server and a client, could be constructed; the logic tends to be more complex. We shall develop a Server Application and a Client Application. After connecting the Client Application to the Server Application, we shall transfer data back and forth between them. We shall show a technique to have the data, transferred to the server, update and show automatically; we shall shall show a technique to have the data, transferred to the client, buffered and displayed upon request.  In order to reduce confusion, I shall use blue when working with server, I shall use maroon when working with the client. Create The Server Application 1] Create folder C:\Sockets\ServerApp 2] Start Visual Studio 2005. Using the mouse, hold down the File Menu -> Select New -> select Project 3] Select Project Type = Visual C#. Template = Windows Application template. Name = ServerApp. Location = C:\Sockets\ServerApp Unselect Create Directory. Push/Select the OK button. 4] Folder C:\Sockets\ServerApp now contains the c# project and support files for our server application. We Shall Create A Socket Server Application That Looks Something Like This   The ServerForm - GroupBox1 1] Create a 428, 600 form, called ServerForm. Make the title Socket Server Application. Append your name to the end of the title. 2] Select a font style and color scheme for the ServerForm. 3] Drag a GroupBox, from the Toolbox, to your form. Let the text be Configuration Settings. 4] Drag a Label, from the Toolbox, into GroupBox1. Let the text be Port # 5] Drag a Textbox, from the Toolbox, into GroupBox1. Let the text be 9999. Let the name be txtPortNo. This will enable us to connect on port # 9999 as a default. 6] Drag a Button, from the Toolbox, into GroupBox1. Let the text be Send This Data To The Client. Let the name be cmdListen. 7] Your form should work. Check it out! CTRL+F5 The ServerForm - GroupBox1 The ServerForm - GroupBox2 1] Drag a GroupBox, from the Toolbox, to your form. Let the text be Data To Send To Client. 2] Drag a Textbox, from the Toolbox, into GroupBox2. Let the name be txtSendData. Let enable be false. Set the background color to black. Set the font color to black. 3] Drag a Button, from the Toolbox into GroupBox2. Let the text be Start Server Listening On This Port #. Let the name beCmdSendData. Let enable be false. 4] Your form should work. Check it out! CTRL+F5 The ServerForm - GroupBox3 1] Drag a GroupBox, from the Toolbox, to your form. Let the text be Data Continuously Received From Client. 2] Drag a Textbox, from the Toolbox into GroupBox3. Let the name be txtDataReceived. Let enable be false. Set the background color to black. Set the font color to black. 3] Drag a Button, from the Toolbox into GroupBox2 Let the text be Clear Server Data. Let the name be CmdClearData. Let enable be false. 4] Your form should work. Check it out! CTRL+F5 5] We shall add some code to the Server form shortly! Create The Client Application 1] Create folder C:\Sockets\ClientApp 2] Start Visual Studio 2005. Using the mouse, hold down the File Menu -> Select New -> select Project 3] Select Project Type = Visual C#. Template = Windows Application template. Name = ClientApp. Location = C:\Sockets\ClientApp Unselect Create Directory. Push/Select the OK button. 4] Folder C:\Sockets\ServerApp now contains the c# project and support files for our server application. We Shall Create A Socket Client Application That Looks Something Like This \\\ The ClientForm - GroupBox1 1] Create a 428, 600 form, called ClientForm. Make the title Socket Client Application. Append your name to the end of the title. 2] Select a font style and color scheme for the ClientForm. I would encourage you to select a color different from that of the ServerApp. 3] Drag a GroupBox, from the Toolbox, to your form. Let the text be Configuration Settings. 4] Drag a Label, from the Toolbox, into GroupBox1. Let the text be Host I.P. Address 5] Drag a Textbox, from the Toolbox, into GroupBox1. Let the text be 127.0.0.1. Let the name be txtIPAddress. Address 127.0.0.1 is the default for this system; by using it as a default, we can easily develop and test both the server and the client on the same computer and then migrate to multiple computers for final testing. 6] Drag a Button, from the Toolbox, into GroupBox1. Let the text be Send This Data To The Client. Let the name be cmdListen.  7] Your form should work. Check it out! CTRL+F5 The ClientForm - GroupBox2 1] Drag a GroupBox, from the Toolbox, to your form. Let the text be Data To Send To Client. 2] Drag a Textbox, from the Toolbox, into GroupBox2. Let the name be txtSendData. Let enable be false. Set the background color to black. Set the font color to black. 3] Drag a Button, from the Toolbox into GroupBox2. Let the text be Start Client Listening On This Port #. Let the name be CmdSendData. Let enable be false. 4] Your form should work. Check it out! CTRL+F5 The ClientForm - GroupBox3 1] Drag a GroupBox, from the Toolbox, to your form. Let the text be Data Continuously Received From Client. 2] Drag a Textbox, from the Toolbox into GroupBox3. Let the name be txtDataReceived. Let enable be false. Set the background color to black. Set the font color to black. 3] Drag a Button, from the Toolbox into GroupBox2 Let the text be Clear Client Data. Let the name be CmdClearData. Let enable be false. 4]] Your form should work. Check it out! CTRL+F5 5] We shall add some code to the Client form shortly! The Code Behind Buttons