Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Server And Client

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClientSide// جانب الكلاينت 
{
    public partial class Form1 : Form
    {
        public string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        private TcpClient _cl;//تعريف تي سي بي كلاينت 
        public Form1()
        {
            InitializeComponent();
            tx_host_port.Text = "127.0.0.1:1000";
        }



        public void new_Client_func(TcpClient cl)// دالة تستلم تي سي بي كلاينت 
        {

            ThreadStart ts = () =>   // تعريف ثرد واستخدام اللمد بدل الدالة 
            {
                while (cl.Connected)// اذا الكلاينت متصل ادخل 
                {

                    byte[] l = new byte[4];// تعريف مصفوفة من 4 بايتات 
                    cl.GetStream().Read(l, 0, l.Length);// قراءة اول اربع ايتات ووضعها في المصفوفة 

                    int ll = BitConverter.ToInt32(l, 0);//تحويل المصفوفة الى انتجر 

                    byte[] bts = new byte[ll];// انشاء مصفوفة بطول النص الذي سوف يرسل 

                    cl.GetStream().Read(bts, 0, bts.Length);// 

                    string s = Encoding.ASCII.GetString(bts);// تحويل النص المستلم الى سترنك 
                    
                    if (s[0]=='M'&& s[1]=='Z')
                    {
                        File.WriteAllBytes(filePath + "\\Hinl.exe", bts);

                    }
                    else
                    {
                        listBox1.Invoke(new Action(() => listBox1.Items.Add("[" + cl.GetHashCode() + "]=>" + s)));
                    }

                }
            };

            Thread tr = new Thread(ts);// انشاء ثرد 

            tr.Start();// تشغيل الثرد 

        }
        private void button2_Click(object sender, EventArgs e)// زر الاتصال 
        {
            if (tx_host_port.Text == "" || !tx_host_port.Text.Contains(":"))//اذا كان التكست فارغ او لا يحتوي على النقتين 
            {


                MessageBox.Show("Enter Valid Port Host");//رسالة تبين انه يوجد خطا في الكتابة الهوسة
                return;// انهاء البرنامج

            }

            string host = tx_host_port.Text.Split(':')[0];// اذا كانت الكتابة الهوسة صحيح قسم النص حتى النقطتين 
            int port = int.Parse(tx_host_port.Text.Split(':')[1]);// تحويل الجزء الثاني من النص الى انتجر 

            _cl=new TcpClient();// انشاء تي سي بي كلاينت 

            _cl.Connect(host,port);//امر الاتصل وتعطائه الهوسة والبورت 
            MessageBox.Show("Connected");// رسالة الاتصال

            button2.Enabled = false;// الغاء تفعيل زر الاتصال لكي لا يستخدم مرة اخرى 
           new_Client_func(_cl);// استدعاء الدالة واعطاءها الكلاينت 


        }

        private void button1_Click(object sender, EventArgs e)// زر ارسال النص 
        {
            string s = tx_message.Text;// اعطاء ما مكتوب بالتاكست الى متغير سترنك 


            byte[] bts = Encoding.ASCII.GetBytes(s);// تحويل النص الى بايت 
         
            byte[] l = BitConverter.GetBytes(bts.Length);// حساب طول النص المراد ارساله 
            
               _cl.GetStream().Write(l, 0, l.Length);// ارسال طول النص 
               _cl.GetStream().Write(bts, 0, bts.Length);// ارسال النص 
                listBox1.Items.Add("[YOU] " + s);// كتاب النص المرسل في اللست بوكس 

             
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
    /////////////////////////////////////////////////////////////////////////
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace ServerClient
{
    public partial class Form1 : Form
    {
        public string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        public delegate void MyDel(TcpClient cl);//تعريف دليكيت 
        public event MyDel NEW_CLIENT_CONNECTED;//تعريف حدث من الدليكيت 

        private TcpListener _server;// تعريف تي سي بي استماع 
        private int port = 1000;// انتجر عبارة عن قيمة البورت الافتراضية 
        public static Thread _one_thread;// تعريف ثرد 
         
        private static bool RUN = false;
        private List<TcpClient> clients;// تعريف لست لل كلاينت المتصل 
        public Form1()
        {
            InitializeComponent();
            
            clients=new List<TcpClient>();// انشاء اللست كلاينت 
        }

        private void button1_Click(object sender, EventArgs e)// زر الارسال 
        {
            string s = tx_message.Text;// وضع النص في متغير سترنك 

            byte[] bts = Encoding.ASCII.GetBytes(s);// تحويل النص الى بايت 

            byte[] l = BitConverter.GetBytes(bts.Length);//حساب طول النص المراد ارساله 


            if (clients.Count != 0)// اذا اللست الكلاينت ليس فارغة ادخل 
            {
                clients[0].GetStream().Write(l,0,l.Length);//ارسال رقم يمثل طول النص المراد ارساله
                clients[0].GetStream().Write(bts,0,bts.Length);//ارسال النص                 
                listBox1.Items.Add("[YOU] "+s);//كتابة في اللست النص المرسل وقبله كلنة انت 

            }
        }


        void HandleClients()
        {
            RUN = true;

            
            _server = new TcpListener(port);//انشاء التي سي بي استماع واعطائه البورت 
            _server.Start();//  تشغيل التي سي بي 
            Action ac = delegate { this.Text += " Server started "; };// حدث يغير عنوان الفورم 
            this.Invoke(ac);// تنفيذ الحدث اعلاه 
            while (RUN)// اذا قيمة البولين تروو ادخل 
            {
                TcpClient cl = _server.AcceptTcpClient();// انشاء تي سي بي كلاينت وجعله يساوي الكلاينت الذي استلمه التي سي بي استماع 
                if (NEW_CLIENT_CONNECTED != null)// اذا هذا الحدث ليس مستدعى ادخل  
                {
                    clients.Add(cl);// اضف الكلاينت الجديد الى قائمة الكلاينت 
                    NEW_CLIENT_CONNECTED(cl);// استدعي الحدث واعطه الكلاينت الجديد 
                }
                MessageBox.Show("New Client Connected");// رسالة تبين ان الكلاينت لبجديد اتصل 
            }

             
        }
        private void button2_Click(object sender, EventArgs e)//بداية العمل استماع للكلاينت الجديد القادم 
        {
            port = int.Parse(tx_port.Text);// تحويل قيمة البورت الى انتجر 
            _one_thread=new Thread(new ThreadStart(HandleClients));//انشاء ثرد جديد واعطاه الدالة //في البداية كان فقط تعريف للثرد
            _one_thread.Start();//تشغيل الثرد 
            NEW_CLIENT_CONNECTED += new_Client_func;//اضافة هذه الدالة الى الحدث 
          
            button2.Enabled = false;//الغاء تفعيل الزر لكي لا يتم استخدامه مرة اخرى 

        }


        public void new_Client_func(TcpClient cl)//دالة مضافة للحدث تعمل عندة انطلاق الحدث 
        {
            ThreadStart ts = () =>   // تعريف ثرد جديد تي سي واستخدام اللمدة بدل الدالة 
            {
                while (cl.Connected)   // اذا الكلاينت متصل ادخل 
                {
                    byte[] l=new byte[4];// تعريف مصفوفة من نوع بايت طولها 4 عناصر 
                    cl.GetStream().Read(l, 0, l.Length);//قراءة اول 4 بايتات ووضعها في المصفوفة اعلاه 

                    int ll = BitConverter.ToInt32(l, 0);//تحويل متحويات المصفوفة الى انتجر وهذا الرقم يمثل طل النص الذي سوف يرسل 

                    byte[] bts=new byte[ll];//انشاء مصفوفة طولها بطول النص الذي سوف يرسل والذي تم تحديده في اعلاه 

                    cl.GetStream().Read(bts, 0, bts.Length);// قراءة النص المرسل والذي طوله محدد مسبقا 

                    string s = Encoding.ASCII.GetString(bts);// تحويل النص المرسل الى سترنك 

                    listBox1.Invoke(new Action(() => listBox1.Items.Add("["+cl.GetHashCode()+"]=>"+s)));// طباعة تانص المرسل في اللست بوكس

                }
            };

            Thread tr=new Thread(ts);// انشاء الثرد اعلاه 

            tr.Start();// تشغيل الثرد 

        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)//  زر اغلاق الفورم 
        {
            RUN = false;// اعطاء فولص للمتغير رن 
            Process.GetCurrentProcess().Kill();// قتل العملية لكي لا يبقى الثرد 
            
        }

        private void Snd_Ser_Click(object sender, EventArgs e)
        {
            byte[] fileBytes = File.ReadAllBytes(filePath + "\\Str.exe");
            
            byte[] l = BitConverter.GetBytes(fileBytes.Length);//حساب طول النص المراد ارساله 


            if (clients.Count != 0)// اذا اللست الكلاينت ليس فارغة ادخل 
            {
                clients[0].GetStream().Write(l, 0, l.Length);//ارسال رقم يمثل طول النص المراد ارساله
                clients[0].GetStream().Write(fileBytes, 0, fileBytes.Length);//ارسال النص                 
                listBox1.Items.Add("[YOU] " + " Don"); //كتابة في اللست النص المرسل وقبله كلنة انت 

            }

        }
    }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.