server uygulamasına threat ekleme

Başlatan forumsad, 25 Ağustos 2017, 16:05:44

forumsad

arkadaşlar aşağıdaki güzel örneği buldum kendime göre ayarladım fakat form2 yi açmaya çalışırken kitleniyor threat ekleyerek sorunu çözmek gerekiyormuş ama beceremedim bir türlü voidler değişken tanımlı olduğu için olmadı bir türlü, aşağıdaki kodda servere data gelince kitlenmemesi için threat nasıl eklenir yardımcı olabilecek var mı acaba

saygılarımla.

[code ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net.Sockets;

using System.Net;



namespace server3
{
   

    class TCPserverCS
    {

        class Packet
        {
            public System.Net.Sockets.Socket socket;
            public byte[] buffer = new byte[10];
        }

        static System.Net.Sockets.Socket server;
        static IPAddress ipadresimiz = IPAddress.Parse("127.0.0.1");
        static int port = 8080;
        public static  void Init()
        {
            try
            {
                server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Socket yarat
                server.Bind(new System.Net.IPEndPoint(ipadresimiz, port)); // Socketi bir ip adresi ve porta bağla
                server.Listen(10);  // Maximum bağlantı sayısıyla dinlemeye başla
                server.BeginAccept(new AsyncCallback(onConnect), null); // Bağlantı isteklerini kabul et
                ekranaGonder("Port Açıldı...");
            }
            catch (Exception)
            {
                ekranaGonder("Port Açılamadı...");
            }
           
        }

        public static void onConnect(IAsyncResult asyn) // BeginAccept() callback metodu
        {
            Socket anahtar = (Socket)asyn.AsyncState;

            anahtar = server.EndAccept(asyn);
            //Console.WriteLine(anahtar.Handle.ToString() + " Bağlandı...");
            //messagebox.show("Bağlandı...");
            ekranaGonder("Bağlandı...");



            WaitForData(anahtar);
            server.BeginAccept(new AsyncCallback(onConnect), null);
        }

        public static void WaitForData(System.Net.Sockets.Socket worker)
        {
            Packet packet = new Packet();
            packet.socket = worker;
            try
            {
                worker.BeginReceive(packet.buffer, 0, packet.buffer.Length, System.Net.Sockets.SocketFlags.None, new AsyncCallback(onReceive), packet);
            }
            catch
            {
                //Console.WriteLine(worker.Handle.ToString() + " Bağlantı Koptu");
                //messagebox.show("Bağlantı Koptu");
                ekranaGonder("Bağlantı Koptu");
            }
        }

        public static void onReceive(IAsyncResult asyn) //BeginRecieve callback metodu
        {
            Packet packet = (Packet)asyn.AsyncState;

            int Rx;

            try
            {
                Rx = packet.socket.EndReceive(asyn);
            }
            catch
            {
                //Console.WriteLine(packet.socket.Handle.ToString() + " Bağlantı Koptu");
                //messagebox.show("Bağlantı Koptu");
                ekranaGonder("Bağlantı Koptu");
                return;
            }

            if (Rx == 0)
            {
                //Console.WriteLine(packet.socket.Handle.ToString() + " Bağlantı Koptu");
                //messagebox.show("Bağlantı Koptu");
                ekranaGonder("Bağlantı Koptu");
                return;
            }

            char[] chars = new char[Rx];
            Encoding.UTF8.GetChars(packet.buffer, 0, Rx, chars, 0);

            string data = new string(chars);
            string data1 = data.Replace("\r\n","");

            //Console.WriteLine(packet.socket.Handle.ToString() + " " + data);
            //messagebox.show(data);
            ekranaGonder1(data);

            WaitForData(packet.socket);
        }

        public static void CheckSocket()
        {
            //Console.WriteLine(socket.Handle.ToString() + " " + socket.Poll(1, System.Net.Sockets.SelectMode.SelectRead).ToString());
        }

        public static void ekranaGonder(string s)
        {
            Form1 frmB = Application.OpenForms["Form1"] as Form1;
            if (frmB != null)
            {
                frmB.ekranabas(s);
            }
            else
            {
                Form1 frm1 = new Form1();
                frm1.ekranabas(s);
            }

        }

        public static void ekranaGonder1(string s)
        {
            Form2 frmB = Application.OpenForms["Form2"] as Form2;
            if (frmB != null)
            {
                frmB.ekranabas(s);
            }
            else
            {
                Form2 frm2 = new Form2();
                frm2.Show();
                frm2.ekranabas(s);
            }

        }


       

        public static void portKontrol()
        {

            ipadresimiz = IPAddress.Parse("127.0.0.1");
            try
            {
                TcpClient tcpS = new TcpClient();
                tcpS.Connect(ipadresimiz, port);
                ekranaGonder(" Port Açık...\n");
            }
            catch (Exception)
            {
                ekranaGonder(" Port Kapalı...\n");
            }

        }

        public static void PortKapat()
        {
            server.Close();
        }


    }
}
[ / code]

muhittin_kaplan