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

3] We shall add some code to the Server form shortly! The windows code for ServerApp.Designer.cs is:
namespace SocketApp
{
partial class ServerApp
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.cmdListen = new System.Windows.Forms.Button();
this.txtPortNo = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.cmdSendData = new System.Windows.Forms.Button();
this.txtSendData = new System.Windows.Forms.TextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.cmdClearData = new System.Windows.Forms.Button();
this.txtDataReceived = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.txtPortNo);
this.groupBox1.Controls.Add(this.cmdListen);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(400, 139);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Configuration + Settings";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(22, 58);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(43, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Port #";
//
// cmdListen
//
this.cmdListen.BackColor = System.Drawing.Color.Maroon;
this.cmdListen.Location = new System.Drawing.Point(14, 94);
this.cmdListen.Name = "cmdListen";
this.cmdListen.Size = new System.Drawing.Size(373, 30);
this.cmdListen.TabIndex = 1;
this.cmdListen.Text = "Start Server Listening On This Port #";
this.cmdListen.UseVisualStyleBackColor = false;
this.cmdListen.Click += new System.EventHandler(this.cmdListen_Click);
//
// txtPortNo
//
this.txtPortNo.Location = new System.Drawing.Point(77, 53);
this.txtPortNo.Name = "txtPortNo";
this.txtPortNo.Size = new System.Drawing.Size(100, 22);
this.txtPortNo.TabIndex = 2;
this.txtPortNo.Text = "9999";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.txtSendData);
this.groupBox2.Controls.Add(this.cmdSendData);
this.groupBox2.Location = new System.Drawing.Point(10, 162);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(400, 183);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Data To Send To Client";
//
// cmdSendData
//
this.cmdSendData.BackColor = System.Drawing.Color.Maroon;
this.cmdSendData.Enabled = false;
this.cmdSendData.Location = new System.Drawing.Point(13, 140);
this.cmdSendData.Name = "cmdSendData";
this.cmdSendData.Size = new System.Drawing.Size(372, 30);
this.cmdSendData.TabIndex = 0;
this.cmdSendData.Text = "Send This Data To The Client";
this.cmdSendData.UseVisualStyleBackColor = false;
this.cmdSendData.Click += new System.EventHandler(this.cmdSendData_Click);
//
// txtSendData
//
this.txtSendData.BackColor = System.Drawing.Color.Black;
this.txtSendData.Enabled = false;
this.txtSendData.Location = new System.Drawing.Point(13, 28);
this.txtSendData.Multiline = true;
this.txtSendData.Name = "txtSendData";
this.txtSendData.Size = new System.Drawing.Size(372, 100);
this.txtSendData.TabIndex = 1;
this.txtSendData.TextChanged += new System.EventHandler(this.txtSendData_TextChanged);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.txtDataReceived);
this.groupBox3.Controls.Add(this.cmdClearData);
this.groupBox3.Location = new System.Drawing.Point(8, 359);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(400, 185);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Data Continuously Received From Client";
//
// cmdClearData
//
this.cmdClearData.BackColor = System.Drawing.Color.Maroon;
this.cmdClearData.Enabled = false;
this.cmdClearData.Location = new System.Drawing.Point(14, 143);
this.cmdClearData.Name = "cmdClearData";
this.cmdClearData.Size = new System.Drawing.Size(372, 30);
this.cmdClearData.TabIndex = 0;
this.cmdClearData.Text = "Clear Server Data";
this.cmdClearData.UseVisualStyleBackColor = false;
this.cmdClearData.Click += new System.EventHandler(this.cmdClearData_Click);
//
// txtDataReceived
//
this.txtDataReceived.BackColor = System.Drawing.Color.Black;
this.txtDataReceived.Location = new System.Drawing.Point(13, 29);
this.txtDataReceived.Multiline = true;
this.txtDataReceived.Name = "txtDataReceived";
this.txtDataReceived.Size = new System.Drawing.Size(372, 100);
this.txtDataReceived.TabIndex = 1;
this.txtDataReceived.TextChanged += new System.EventHandler(this.txtDataReceived_TextChanged);
//
// ServerApp
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Navy;
this.ClientSize = new System.Drawing.Size(420, 573);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.ForeColor = System.Drawing.Color.White;
this.Location = new System.Drawing.Point(13, 28);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "ServerApp";
this.Text = "Socket Server Application";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtPortNo;
private System.Windows.Forms.Button cmdListen;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.TextBox txtSendData;
private System.Windows.Forms.Button cmdSendData;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.TextBox txtDataReceived;
private System.Windows.Forms.Button cmdClearData;
}
}
|
4] The windows code for ServerApp.cs is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SocketApp
{
public partial class ServerApp : Form
{
public ServerApp()
{
InitializeComponent();
}
private void txtDataReceived_TextChanged(object sender, EventArgs e)
{
}
private void cmdSendData_Click(object sender, EventArgs e)
{
}
private void cmdClearData_Click(object sender, EventArgs e)
{
}
private void cmdListen_Click(object sender, EventArgs e)
{
}
private void txtSendData_TextChanged(object sender, EventArgs e)
{
}
}
}
|
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

3] We shall add some code to the Server form shortly! The windows code for ClientServerApp.Designer.cs is:
namespace ClientApp
{
partial class ClientApp
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.txtIPAddress = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtPortNo = new System.Windows.Forms.TextBox();
this.cmdConnect = new System.Windows.Forms.Button();
this.cmdCloseConnection = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.txtSendData = new System.Windows.Forms.TextBox();
this.cmdSendData = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.txtDataReceived = new System.Windows.Forms.TextBox();
this.cmdReceiveData = new System.Windows.Forms.Button();
this.cmdExit = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.cmdCloseConnection);
this.groupBox1.Controls.Add(this.cmdConnect);
this.groupBox1.Controls.Add(this.txtPortNo);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.txtIPAddress);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(400, 139);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Configuration + Settings";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 27);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(111, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Host I.P. Address";
//
// txtIPAddress
//
this.txtIPAddress.Location = new System.Drawing.Point(165, 27);
this.txtIPAddress.Name = "txtIPAddress";
this.txtIPAddress.Size = new System.Drawing.Size(100, 22);
this.txtIPAddress.TabIndex = 1;
this.txtIPAddress.Text = "127.0.0.1";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(96, 60);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(43, 16);
this.label2.TabIndex = 2;
this.label2.Text = "Port #";
//
// txtPortNo
//
this.txtPortNo.Location = new System.Drawing.Point(165, 60);
this.txtPortNo.Name = "txtPortNo";
this.txtPortNo.Size = new System.Drawing.Size(100, 22);
this.txtPortNo.TabIndex = 3;
this.txtPortNo.Text = "9999";
//
// cmdConnect
//
this.cmdConnect.BackColor = System.Drawing.Color.Navy;
this.cmdConnect.Location = new System.Drawing.Point(16, 96);
this.cmdConnect.Name = "cmdConnect";
this.cmdConnect.Size = new System.Drawing.Size(184, 30);
this.cmdConnect.TabIndex = 4;
this.cmdConnect.Text = "Connect To Server (Host)";
this.cmdConnect.UseVisualStyleBackColor = false;
this.cmdConnect.Click += new System.EventHandler(this.cmdConnect_Click);
//
// cmdCloseConnection
//
this.cmdCloseConnection.BackColor = System.Drawing.Color.Navy;
this.cmdCloseConnection.Enabled = false;
this.cmdCloseConnection.Location = new System.Drawing.Point(206, 96);
this.cmdCloseConnection.Name = "cmdCloseConnection";
this.cmdCloseConnection.Size = new System.Drawing.Size(179, 30);
this.cmdCloseConnection.TabIndex = 5;
this.cmdCloseConnection.Text = "Close The Connection";
this.cmdCloseConnection.UseVisualStyleBackColor = false;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.cmdSendData);
this.groupBox2.Controls.Add(this.txtSendData);
this.groupBox2.Location = new System.Drawing.Point(10, 162);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(400, 183);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Data To Send To Server";
//
// txtSendData
//
this.txtSendData.BackColor = System.Drawing.Color.Black;
this.txtSendData.Location = new System.Drawing.Point(14, 21);
this.txtSendData.Multiline = true;
this.txtSendData.Name = "txtSendData";
this.txtSendData.Size = new System.Drawing.Size(372, 100);
this.txtSendData.TabIndex = 0;
//
// cmdSendData
//
this.cmdSendData.BackColor = System.Drawing.Color.Navy;
this.cmdSendData.Enabled = false;
this.cmdSendData.Location = new System.Drawing.Point(17, 136);
this.cmdSendData.Name = "cmdSendData";
this.cmdSendData.Size = new System.Drawing.Size(369, 30);
this.cmdSendData.TabIndex = 1;
this.cmdSendData.Text = "Send This Data To Server (Host).";
this.cmdSendData.UseVisualStyleBackColor = false;
this.cmdSendData.Click += new System.EventHandler(this.button1_Click);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.cmdReceiveData);
this.groupBox3.Controls.Add(this.txtDataReceived);
this.groupBox3.Location = new System.Drawing.Point(8, 359);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(400, 193);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Data Received From Server Through Buffer";
//
// txtDataReceived
//
this.txtDataReceived.BackColor = System.Drawing.Color.Black;
this.txtDataReceived.Location = new System.Drawing.Point(16, 31);
this.txtDataReceived.Multiline = true;
this.txtDataReceived.Name = "txtDataReceived";
this.txtDataReceived.Size = new System.Drawing.Size(372, 100);
this.txtDataReceived.TabIndex = 0;
//
// cmdReceiveData
//
this.cmdReceiveData.BackColor = System.Drawing.Color.Navy;
this.cmdReceiveData.Enabled = false;
this.cmdReceiveData.Location = new System.Drawing.Point(16, 146);
this.cmdReceiveData.Name = "cmdReceiveData";
this.cmdReceiveData.Size = new System.Drawing.Size(369, 30);
this.cmdReceiveData.TabIndex = 1;
this.cmdReceiveData.Text = "Click To Receive Data From Server";
this.cmdReceiveData.UseVisualStyleBackColor = false;
//
// cmdExit
//
this.cmdExit.Location = new System.Drawing.Point(8, 571);
this.cmdExit.Name = "cmdExit";
this.cmdExit.Size = new System.Drawing.Size(400, 30);
this.cmdExit.TabIndex = 7;
this.cmdExit.Text = "Exit";
this.cmdExit.UseVisualStyleBackColor = true;
//
// ClientApp
//
this.BackColor = System.Drawing.Color.Maroon;
this.ClientSize = new System.Drawing.Size(420, 613);
this.Controls.Add(this.cmdExit);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Font = new System.Drawing.Font("Arial", 9.75F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.ForeColor = System.Drawing.Color.White;
this.Name = "ClientApp";
this.Text = "Socket Client Application";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button cmdCloseConnection;
private System.Windows.Forms.Button cmdConnect;
private System.Windows.Forms.TextBox txtPortNo;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtIPAddress;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button cmdSendData;
private System.Windows.Forms.TextBox txtSendData;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Button cmdReceiveData;
private System.Windows.Forms.TextBox txtDataReceived;
private System.Windows.Forms.Button cmdExit;
}
}
|
4] The windows code for ClientApp.cs is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ClientApp
{
public partial class ClientApp : Form
{
public ClientApp()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void cmdConnect_Click(object sender, EventArgs e)
{
}
}
}
|
1] Add the following classes to your application in ServerApp.cs
using System.Data; using System.Net; using System.Net.Sockets;
2] Your form should work. Check it out! CTRL+F5
3] We will need a socket object for the server application. We shall create a socket, associate it with a port number and listen for incoming data on that port. The pointer to our socket shall be socServerListener. Placing it at the top of our form, as shown below, will make it available to all of the functions in this form.
public Socket socServerListener;

4] The start of the code for the listen button may be found below. I creates a socket, This block of code
attempts to create a new Socket object and associate it with the socServerListener pointer
convert the text port number, found in txtPortNo, to an IPEndPoint format that the can be bound with the socket.
initiate the listening on that port
There is a message box display letting you know that we have started the process and convincing you that this block of code has been executed. Enter the code below in your program.

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