Tutorial: Creating Sockets In C#
Dr. Thomas E. Hicks
Computer Science Department
Trinity University
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
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

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

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

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.


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

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
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 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

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

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.

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.

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