Tutorial: Creating 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] Rename Form1.cs to
ServerApp.cs.

2] Create a 428, 640 form, called ServerApp. Make the title Socket Server Application. Append your name to the end of the title. Socket Server Application - Jane Doe

3] Select a font style and color scheme for the ServerForm.

4] Drag a GroupBox, from the Toolbox, to your form. Let the text be Configuration + Settings.

5] Drag a Label, from the Toolbox, into GroupBox1. Let the text be Port #

6] 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.

7] Drag a Button, from the Toolbox, into GroupBox1. Let the text be Start Server Listening On This Port #. Let the name be cmdListen.

8] Your form should work. Check it out! CTRL+F5



 

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 Send This Data To The Client. Let the name be cmdSendData. Let enable be false. Let multiline be true.

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.  Let multiline be true.

3] Drag a Button, from the Toolbox into GroupBox3 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


The ServerForm - Exit Button

1] Drag a Button, from the Toolbox to the bottom of the form. Let the text be Exit. Let the name be cmdExit. Let enable be false.

2] Your form should work. Check it out! CTRL+F5



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] Rename Form1.cs to ClientApp.cs.

2] Create a 428, 640 form, called ClientForm. Make the title Socket Client Application. Append your name to the end of the title. Socket Client Application - by Jane Doe

3] Select a font style and color scheme for the ClientApp. I would encourage you to select a color different from that of the ServerApp

4] Drag a GroupBox, from the Toolbox, to your form. Let the text be Configuration + Settings.

5] Drag a Label, from the Toolbox, into GroupBox3. Let the text be Host I.P. Address

6] Drag a Textbox, from the Toolbox, into GroupBox3. Let the name be txtIPAddress. Let the text be 127.0.0.1. This default IP address is reserved for the current computer. During the development and testing stage, we can actually test both the ClientApp and the ServerApp on your local computer; final testing should be done using separate computers.

7] Drag a Label, from the Toolbox, into GroupBox3. Let the text be Port #.

8] Drag a Textbox, from the Toolbox, into GroupBox3. Let the text be 9999. Let the name be txtPortNo. This will enable us to connect on port # 9999 as a default.

9] Drag a Button, from the Toolbox, into GroupBox3. Let the text be Connect To Server (Host) Let the name be cmdConnect.

9] Drag a Button, from the Toolbox, into GroupBox3. Let the text be Close The Connection Let the name be cmdCloseConnection.

10] 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 Server.

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.  Let multiline be true.

3] Drag a Button, from the Toolbox into GroupBox2. Let the text be Send This Data To Server (Host). 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 Received From Server Through Buffer

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.  Let multiline be true.

3] Drag a Button, from the Toolbox into GroupBox2 Let the text be Click To Receive Data From Server Let the name be cmdReceiveData. Let enable be false.

4]] Your form should work. Check it out! CTRL+F5


 


The ServerForm - Exit Button

1] Drag a Button, from the Toolbox to the bottom of the form. Let the text be Exit. Let the name be cmdExit. Let enable be false.

2] Your form should work. Check it out! CTRL+F5


ServerApp - Code

1] Add the following classes to your application in ServerApp.cs

using System.Net;
using System.Net.Sockets;

2] Add the declarations for asynClientCallback, socServerListener, and socClient. Add class SocketPacket.

3] Add the code to cmdListen_Click.

4] Add the code to OnClickConnect.

5] Add the code to ReadClientDataLoop.

6] Add the code to OnDataReceived.

7] Add the code to cmdSendData_Click

8] Add the code to cmdListen_Click.


ClientApp - Code

1] Add the following classes to your application in ClientApp.cs

using System.Net;
using System.Net.Sockets;

2] Write the declaration for socClient.

2] Write the declaration for cmdConnect_Click.

3] Write the declaration for cmdSendData_Click.

4] Write the declaration for cmdReceiveData_Click.

5] Write the declaration for cmdCloseConnection_Click.
6] Write the declaration for cmdExit_Click.

The Application In Action

Client-Server-Socket.zip


Special Thanks

Special thanks to Ashish Dhar for his contributions to a similar socket application.