C# Client Socket program

The C# Socket Programming has two sections.

1. C# Server Socket Program

2. C# Client Socket Program

c# client socket program Client Socket Program

The C# Client Socket Program is the second part of the C# Server Socket Program . The C# Client Socket Program is a Windows based application . The Client is connected to the Port 8888 of the C# Server Socket Program , and the IP Address (Computer Name) here we give as 127.0.0.1 , because the Server and Client running on the same machine .

clientSocket.Connect("127.0.0.1", 8888);

When the C# Client program starts , it will connect to the C# Server Socket Program and start to reads data from NetworkStream , and also write to the NetworkStream . When you start the client program you will get a message from Server "client started". When press the button at the bottom of Client window, it will send a message to the Server and also receive response from the Server.

Full Source C#

How to run this program ?

The C# Socket Programming has two sections.

1. C# Server Socket Program

2. C# Client Socket Program

When you finish coding and build the Server and Client program , First you have to start C# Server Socket Program from DOS prompt , then you will get a message " Server Started " in your DOS screen, where the server program is running .

Next step is to start C# Client Socket Program in the same computer or other computers on the same network . When you start the client program , it will establish a connection to the Server and get a message in client screen " Client Started " , at the same time you can see a message in the Server screen " Accept connection from client " .

Now your C# Server Socket Program and C# Client Socket Program is get connected and communicated . If you want to communicate the Server and Client again , click the button in the client program , then you can see new messages in the Server and Client programs displayed.

Full Source C#
using System; using System.Windows.Forms; using System.Net.Sockets; using System.Text; namespace WindowsApplication1 { public partial class Form1 : Form { System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { msg("Client Started"); clientSocket.Connect("127.0.0.1", 8888); label1.Text = "Client Socket Program - Server Connected ..."; } private void button1_Click(object sender, EventArgs e) { NetworkStream serverStream = clientSocket.GetStream(); byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$"); serverStream.Write(outStream, 0, outStream.Length); serverStream.Flush(); byte[] inStream = new byte[10025]; serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize); string returndata = System.Text.Encoding.ASCII.GetString(inStream); msg(returndata); textBox2.Text = ""; textBox2.Focus(); } public void msg(string mesg) { textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg; } } }