c# server ve client uygulaması (c# tcp ip)

Server

using System;
using System.IO;
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;
using System.Net.Sockets;
using System.Threading;



namespace tcp5
{
    public partial class Form1 : Form
    {
        //Gerekli Siniflarin Nesneleri tanimlaniyor
        Thread t;
        IPAddress ipadresimiz;
        TcpListener dinle;
        Socket soket;
        NetworkStream ag;
        StreamReader oku;
        StreamWriter yaz;
        public delegate void ricdegis(string text);
        public Form1()
        {
            InitializeComponent();
        }
        // Serverde Method_1
        public void okumayabasla()
        {
            soket = dinle.AcceptSocket();
            ag = new NetworkStream(soket);
            oku = new StreamReader(ag);
            while (true)
            {
                try
                {
                    string yazi = oku.ReadLine();
                    ekranabas(yazi);
                }
                catch
                {
                    return;
                }
            }
        }

        // Serverde Method_2 (Gelen Veriyi richTextBox icine yazdirmak icin)
        public void ekranabas(string s)
        {
            if (this.InvokeRequired)
            {
                ricdegis degis = new ricdegis(ekranabas);
                this.Invoke(degis, s);
            }
            else
            {
                s = "Cilent: " + s;
                richTextBox1.AppendText(s + "\n");
            }

        }

        // Serverde Method_3 (Serverimizin Port dinlemesine baslamsi icin)
        private void dinlemeye_basla()
        {
            try
            {
                ipadresimiz = IPAddress.Parse("127.0.0.1");
                dinle = new TcpListener(ipadresimiz, Convert.ToInt16(txtport.Text));
                dinle.Start();
                t = new Thread(new ThreadStart(okumayabasla));
                t.Start();
                richTextBox1.AppendText(DateTime.Now.ToString() + " Dinleme baslatildi..\n");
            }
            catch (Exception)
            {

                MessageBox.Show("Dinleme baslatilamadi");
            }
        }
        

        private void btnbaglan_Click_1(object sender, EventArgs e)
        {
            dinlemeye_basla();
        }

        private void btnayril_Click_1(object sender, EventArgs e)
        {
            ag.Close();
        }

        private void btngonder_Click_1(object sender, EventArgs e)
        {
            if (txtgonder.Text == "")

                //Burda bos alan göndermeyi önlüyoruz...
                return;
            else
            {
                yaz = new StreamWriter(ag);
                yaz.WriteLine(txtgonder.Text);
                yaz.Flush();
                richTextBox1.AppendText(txtgonder.Text + "\n");
                txtgonder.Text = "";
            }
        }
    }
}
-----------------------------------------------------------------------------------------------------------
Client
using System;
using System.IO;
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;
using System.Net.Sockets;
using System.Threading;

namespace cilent5

{
    public partial class Form1 : Form
    {
        //Yine gerekli Siniflarin nesneleri tanimlaniyor
        Thread t;
        TcpClient baglantikur;
        NetworkStream ag;
        StreamReader oku;
        StreamWriter yaz;
        public delegate void ricdegis(string text);
        public Form1()
        {
            InitializeComponent();
        }
        // Clintde Method_1 (Gelen veri okunuyor)
        public void okumayabasla()
        {
            ag = baglantikur.GetStream();
            oku = new StreamReader(ag);
            while (true)
            {
                try
                {
                    string yazi = oku.ReadLine();
                    ekranabas(yazi);
                }
                catch
                {
                    return;
                }
            }
        }

        // Clientde Method_2 (Okunan Veri richTextBox icine yaziliyor)
        public void ekranabas(string s)
        {
            if (this.InvokeRequired)
            {
                ricdegis degis = new ricdegis(ekranabas);
                this.Invoke(degis, s);
            }
            else
            {
                s = "Server: " + s;
                richTextBox1.AppendText(s + "\n");
            }
        }

        // Clientde Method_3 (Istenilen IP'ye istenen Port üzerinden baglaniliyor)
        public void baglanti_kur()
        {
            try
            {
                //Ben Lochalhos üzerinde deneme yapacagim icin 127.0.0.1 verdim
                baglantikur = new TcpClient("127.0.0.1", Convert.ToInt16(txtport.Text));
                t = new Thread(new ThreadStart(okumayabasla));
                t.Start();
                richTextBox1.AppendText(DateTime.Now.ToString() + " Baglanti kuruldu...\n");
            }
            catch (Exception)
            {

                MessageBox.Show("Server ile baglanti kurulurken hata olustu !");
            }
        }

            
        private void btnbaglan_Click_1(object sender, EventArgs e)
        {
            baglanti_kur();
        }

        private void btnayril_Click_1(object sender, EventArgs e)
        {
            baglantikur.Client.Close();
        }

        private void btngonder_Click_1(object sender, EventArgs e)
        {
            if (txtgonder.Text == "")
                //Burda bos alan göndermeyi önlüyoruz...
                return;
            else
            {
                yaz = new StreamWriter(ag);
                yaz.WriteLine(txtgonder.Text);
                yaz.Flush();
                richTextBox1.AppendText(txtgonder.Text + "\n");
                txtgonder.Text = "";
            }
        }
    }
}

Yorumlar

Unknown dedi ki…
This way my colleague Wesley Virgin's biography starts in this shocking and controversial video.

You see, Wesley was in the army-and shortly after leaving-he found hidden, "SELF MIND CONTROL" secrets that the CIA and others used to get anything they want.

THESE are the same tactics tons of celebrities (especially those who "became famous out of nowhere") and elite business people used to become rich and famous.

You probably know that you only use 10% of your brain.

Really, that's because most of your brainpower is UNCONSCIOUS.

Maybe this conversation has even taken place IN YOUR very own head... as it did in my good friend Wesley Virgin's head about 7 years back, while driving an unlicensed, beat-up bucket of a car without a driver's license and with $3 on his debit card.

"I'm absolutely frustrated with going through life paycheck to paycheck! When will I finally succeed?"

You've been a part of those those types of conversations, ain't it right?

Your own success story is going to happen. You just have to take a leap of faith in YOURSELF.

CLICK HERE To Find Out How To Become A MILLIONAIRE
Murat Cengiz dedi ki…
Teşekkürler .Eline sağlık. :)İnşallah işeme yarayacak.
Adsız dedi ki…
Merhaba çok güzel olmuş elinize sağlık benim sorum bu kod ile başlık alabiliyoruz başlık yerine gelen mesaj içeriğini tama nasıl alabiliriz

Bu blogdaki popüler yayınlar

c# Delay fonksiyonu - c# bekletme (not sleep)

C# formun açık olup olmadığını denetlemek