<repo_name>HRida/TcpListner_TcpClient_SqlConnection<file_sep>/Client1/Form1.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

namespace Client1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // creat client Socket  to send
            TcpClient Sending = new TcpClient("localhost", 7110);
            byte[] data = Encoding.ASCII.GetBytes(textBox1.Text);
            NetworkStream datastream = Sending.GetStream();
            datastream.Write(data, 0, data.Length);

            data = new byte[256];
            datastream.Read(data, 0, data.Length);
            string ResponseData;
            ResponseData = Encoding.ASCII.GetString(data, 0, data.Length);

            textBox2.Text = textBox2.Text + Environment.NewLine + ResponseData;
            textBox1.Text = "";
            Sending.Close();
        }
    }
}
<file_sep>/Server/Program.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Net;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The Server is Run And Wait .....");

            IPAddress address = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(address, 7110);
            listener.Start();

            while (true)  // to make the server always on
            {
                TcpClient client = listener.AcceptTcpClient();                // to connect with clients

                NetworkStream clientStream = client.GetStream();
                byte[] message = new byte[4096];
                clientStream.Read(message, 0, message.Length);

                string messageFromClient = Encoding.ASCII.GetString(message, 0, message.Length);

                SqlConnection sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
                sqlConnection.Open();
                SqlDataAdapter adb = new SqlDataAdapter("Select EmployeeId From Employees Where LastName='" + messageFromClient.Trim('\0') + "'", sqlConnection);
                System.Data.DataSet dat = new System.Data.DataSet();
                adb.Fill(dat);

                string MessageToClient = dat.Tables[0].Rows[0].ItemArray[0].ToString();

                NetworkStream ServerStream = client.GetStream();
                byte[] buffer = Encoding.ASCII.GetBytes(MessageToClient);
                ServerStream.Write(buffer, 0, buffer.Length);

                ServerStream.Flush();
                client.Close();

            }                
         //   listener.Stop();
        }
    }
}
