I have developed a server/client application. Everythnig is cool but whenever the internet connection is halt or no connection. It show the error message from try/catch and if internet problem is ok and there is connection, program is continue normally but sometimes it is halt.If the error closed this application or I push the close button, windows form is closed but program still runnig in the background and whenever I try to open new program, it get the tcp/ip port problem because I still runnig and use this special port. I know it use the thread and has endless while loop for listen the port but I want to use this problem. When I close it or close wtih program error. I want It does not work on background.

Thank you...

Recommended Answers

All 2 Replies

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


namespace server
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.ListBox listBox2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;


        public Sistem mySystem = new Sistem();
        Teknoser teknoser = new Teknoser();
        TeknoserKapama TeknoserKapat = new TeknoserKapama();

        DBconnect connect = new DBconnect();
        Thread kanal;
        string logGelenIP = null;
        string logGelenIstek = null;
        string logGonderilenBilgi = null;
        string logKaydi = null;
        DateTime tarih;

        public Form1()
        {
            InitializeComponent();

            Control.CheckForIllegalCrossThreadCalls = false;
            CagriKapatma kapatma = new CagriKapatma();
            //string cevap = null;

            kanal = new Thread(new ThreadStart(dinle));
            try
            {
                kanal.Start();
                kanal.Priority = ThreadPriority.Highest;
                //MessageBox.Show("Server açıldı");
            }
            catch (ThreadAbortException hata)
            {
                kanal.Abort();
                hata.Message.ToString();
                MessageBox.Show("Bekleyen paket yok");
            }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.listBox2 = new System.Windows.Forms.ListBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // listBox1
            // 
            this.listBox1.HorizontalScrollbar = true;
            this.listBox1.Location = new System.Drawing.Point(8, 48);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(168, 199);
            this.listBox1.TabIndex = 0;
            // 
            // listBox2
            // 
            this.listBox2.Location = new System.Drawing.Point(200, 48);
            this.listBox2.Name = "listBox2";
            this.listBox2.Size = new System.Drawing.Size(176, 199);
            this.listBox2.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.label1.ForeColor = System.Drawing.Color.Maroon;
            this.label1.Location = new System.Drawing.Point(24, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(136, 23);
            this.label1.TabIndex = 2;
            this.label1.Text = "GELEN İSTEKLER";
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.label2.ForeColor = System.Drawing.Color.Maroon;
            this.label2.Location = new System.Drawing.Point(224, 24);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(152, 16);
            this.label2.TabIndex = 3;
            this.label2.Text = "Gönderen IP ve Port No";
            // 
            // label3
            // 
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.label3.ForeColor = System.Drawing.Color.Maroon;
            this.label3.Location = new System.Drawing.Point(224, 256);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(136, 16);
            this.label3.TabIndex = 4;
            this.label3.Text = "Gelen istek sayısı";
            // 
            // label4
            // 
            this.label4.BackColor = System.Drawing.SystemColors.ScrollBar;
            this.label4.Location = new System.Drawing.Point(240, 280);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(100, 23);
            this.label4.TabIndex = 5;
            // 
            // button1
            // 
            this.button1.BackColor = System.Drawing.SystemColors.Info;
            this.button1.Location = new System.Drawing.Point(16, 264);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(72, 48);
            this.button1.TabIndex = 6;
            this.button1.Text = "Dinlemeye ara ver";
            this.button1.UseVisualStyleBackColor = false;
            this.button1.Click += new System.EventHandler(this.button1_Click_1);
            // 
            // button2
            // 
            this.button2.BackColor = System.Drawing.SystemColors.Info;
            this.button2.Location = new System.Drawing.Point(96, 264);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(72, 48);
            this.button2.TabIndex = 7;
            this.button2.Text = "Dinlemeye devam et";
            this.button2.UseVisualStyleBackColor = false;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.LightSalmon;
            this.ClientSize = new System.Drawing.Size(392, 317);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.listBox2);
            this.Controls.Add(this.listBox1);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "SERVER";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        CagriKapatma kapatma = new CagriKapatma();
        const int port = 15000;
        private void dinle()
        {
            string ip;
            string gelen;
            string cevap;

            try
            {
                TcpListener dinleyici = new TcpListener(port);
                byte[] gelenveri_dizi = new byte[81];

                while (true)
                {
                    dinleyici.Start();
                    Socket soket = dinleyici.AcceptSocket();
                    soket.Receive(gelenveri_dizi, gelenveri_dizi.Length, 0);
                    gelen = Encoding.UTF8.GetString(gelenveri_dizi);

                    logKaydi = "İşlem Tarihi:  " + tarih + "\r\n" + "Gelen IP/Port:  " + logGelenIP + "\r\n" + "Gelen istek :  " + logGelenIstek + "\r\n" + "Gönderilen bilgi :  " + logGonderilenBilgi + "\r\n" + "\r\n" + "\r\n" + "\r\n";

                    string bugun = DateTime.Today.ToShortDateString();
                    string dosyaADI = "C://PSHlog//" + bugun + ".txt";
                    StreamWriter metin = new StreamWriter(dosyaADI, true);
                    metin.Write(logKaydi);
                    metin.Close();

                    listBox1.Items.Add(gelen);
                    logGelenIstek = gelen;
                    listBox2.Items.Add(soket.RemoteEndPoint);
                    label4.Text = listBox2.Items.Count.ToString();
                    cevap = paketFiltre(gelen);
                    logGonderilenBilgi = cevap;
                    listBox1.Items.Add(cevap);
                    tarih = DateTime.Now;


                    ip = soket.RemoteEndPoint.ToString();
                    byte[] gonderveri_dizi2 = Encoding.Default.GetBytes(cevap);
                    soket.Send(gonderveri_dizi2, gonderveri_dizi2.Length, 0);
                    dinleyici.Stop();
                    soket.Close();
                }
            }
            catch (SocketException sex)
            {
              
                MessageBox.Show("Şu hatalara rastladık: " + sex.Message);
                kanal.Abort();

            }
            
        }
        
   
        public void ekrandaGoster(string gelengoster)
        {
            listBox1.Items.Add(gelengoster);
            label4.Text = listBox1.Items.Count.ToString();
        }
        public string paketFiltre(string gelen)
        {
            char[] c = gelen.ToCharArray();
            string islenecekStr = null;
            string gidecekStr = null;
            string istenenPaket = null;
            int istekPaket = 0;
            
            if (c[0].ToString() == "1" && c[1].ToString()=="1")
            {
                string kontrol = null;
                if ((Convert.ToInt16(c[2].ToString()) == 0))
                    istenenPaket = c[2].ToString();
                if ((Convert.ToInt16(c[1].ToString()) != 0))
                    istenenPaket = (c[2].ToString() + c[3].ToString());

                islenecekStr = (c[4].ToString());
                istekPaket = Convert.ToInt16(istenenPaket);
                string bolgeCheck = null;
                int islenen = Convert.ToInt16(islenecekStr);

                if (islenen == 1)
                    bolgeCheck = "Lefkosa";
                if (islenen == 2)
                    bolgeCheck = "Girne";
                if (islenen == 3)
                    bolgeCheck = "Magosa";
                if (islenen == 4)
                    bolgeCheck = "Guzelyurt";


                kontrol = connect.cagriKontrol(bolgeCheck);
                int secim = Convert.ToInt16(kontrol);

                switch (secim)
                {
                    case 10100:
                        gidecekStr = kontrol;
                        break;

                    case 1:
                        gidecekStr = mySystem.AcikCagrilar(istekPaket, islenecekStr);
                        break;
                }
            }

            if (c[0].ToString() == "1" && c[1].ToString() == "2")
            {

                if ((Convert.ToInt16(c[2].ToString()) == 0))
                    istenenPaket = c[2].ToString();
                if ((Convert.ToInt16(c[1].ToString()) != 0))
                    istenenPaket = (c[2].ToString() + c[3].ToString());

                islenecekStr = (c[4].ToString());

                if (islenecekStr == "1")
                {
                    gidecekStr = teknoser.TeknoserCagrisiGoster("LEFKOŞA", Convert.ToInt16(istenenPaket));
                }

                if (islenecekStr == "2")
                {
                    gidecekStr = teknoser.TeknoserCagrisiGoster("GİRNE", Convert.ToInt16(istenenPaket));
                }

                if (islenecekStr == "3")
                {
                    gidecekStr = teknoser.TeknoserCagrisiGoster("MAGOSA", Convert.ToInt16(istenenPaket));
                }

                if (islenecekStr == "4")
                {
                    gidecekStr = teknoser.TeknoserCagrisiGoster("GÜZELYURT", Convert.ToInt16(istenenPaket));
                }

            }


                if (c[0].ToString() == "2" && c[1].ToString() == "1") //ARIZA ÇAĞRISI KAPAT
                {
                    gidecekStr = kapatma.ArizaCagrisiKapat(gelen);
                }

                if (c[0].ToString() == "3" && c[1].ToString() == "1") // GERİ ALIM(SÖKÜM) ÇAĞRISI KAPAT
                {
                    gidecekStr = kapatma.GeriAlimCagrisiKapat(gelen);
                }

                if (c[0].ToString() == "4" && c[1].ToString() == "1") // KURULUM ÇAĞRISI KAPAT
                {

                    gidecekStr = kapatma.KurulumCagrisiKapat(gelen);
                }

                if (c[0].ToString() == "5" && c[1].ToString() == "1") // DEĞİŞİM ÇAĞRISI KAPAT
                {
                    gidecekStr = kapatma.DegisimCagrisiKapat(gelen);
                }

                if (c[0].ToString() == "6" && c[1].ToString() == "1") // MALZEME ÇAĞRISI KAPAT
                {
                    gidecekStr = kapatma.MalzemeCagrisiKapat(gelen);
                }

                if (c[0].ToString() == "7" && c[1].ToString() == "1") // VERSİYON ÇAĞRISI KAPAT
                {
                    gidecekStr = kapatma.VersiyonCagrisiKapat(gelen);
                }

                if (c[0].ToString() == "2" && c[1].ToString() == "2") // TEKNOSER ÇAĞRISI KAPAT
                {
                    gidecekStr = TeknoserKapat.TeknoserCagriKapama(gelen);
                }

                if (c[0].ToString() == "3" && c[1].ToString() == "2") // TEKNOSER ÇAĞRISI KAPAT
                {
                    gidecekStr = TeknoserKapat.TeknoserCagriKapama(gelen);
                }

                if (c[0].ToString() == "4" && c[1].ToString() == "2") // TEKNOSER ÇAĞRISI KAPAT
                {
                    gidecekStr = TeknoserKapat.TeknoserCagriKapama(gelen);
                }

                if (c[0].ToString() == "5" && c[1].ToString() == "2") // TEKNOSER ÇAĞRISI KAPAT
                {
                    gidecekStr = TeknoserKapat.TeknoserCagriKapama(gelen);
                }

                if (c[0].ToString() == "6" && c[1].ToString() == "2") // TEKNOSER ÇAĞRISI KAPAT
                {
                    gidecekStr = TeknoserKapat.TeknoserCagriKapama(gelen);
                }

                if (c[0].ToString() == "7" && c[1].ToString() == "2") // TEKNOSER ÇAĞRISI KAPAT
                {
                    gidecekStr = TeknoserKapat.TeknoserCagriKapama(gelen);
                }

                if (c[0].ToString() == "8" && c[1].ToString() == "1")
                {
                    string kontrol = null;
                    if ((Convert.ToInt16(c[2].ToString()) == 0))
                        istenenPaket = c[2].ToString();
                    if ((Convert.ToInt16(c[1].ToString()) != 0))
                        istenenPaket = (c[2].ToString() + c[3].ToString());

                    islenecekStr = (c[4].ToString());
                    istekPaket = Convert.ToInt16(istenenPaket);
                    string bolgeCheck = null;
                    int islenen = Convert.ToInt16(islenecekStr);

                    if (islenen == 1)
                        bolgeCheck = "Lefkosa";
                    if (islenen == 2)
                        bolgeCheck = "Girne";
                    if (islenen == 3)
                        bolgeCheck = "Magosa";
                    if (islenen == 4)
                        bolgeCheck = "Guzelyurt";


                    kontrol = connect.cagriKontrol(bolgeCheck);
                    int secim = Convert.ToInt16(kontrol);

                    switch (secim)
                    {
                        case 10100:
                            gidecekStr = kontrol;
                            break;

                        case 1:
                            gidecekStr = mySystem.TakipteCagrilar(istekPaket, islenecekStr);
                            break;
                    }
                }


            return gidecekStr;

        }

        private void Form1_Load(object sender, System.EventArgs e)
        {


        }

        private void button1_Click_1(object sender, System.EventArgs e)
        {
            kanal.Suspend();
            button1.Enabled = false;
            button2.Enabled = true;
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            kanal.Resume();
            button2.Enabled = false;
            button1.Enabled = true;

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            kanal.Abort(true);
            Application.Exit();
            
        }
    }
}

Hi,
I have following suggestions for your problem:
1. In private void dinle(), use finally block where close the socket and release other resources.
2. Whenever you are closing application there also dont just abort the thread but also release all resources like closing sockets etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.