مشکل با سوکت ها

senaps

عضو جدید
کاربر ممتاز
سلام....
پروژه ی اخر ترم من،در مورد سوکت ها و چند نخی هاست....برنامه رو نوشتم،خیلی هم ساده نوشته شده!!
ولی متاسافنه با این وجود،امکان برقراری ارتباط میان دو برنامه ی کلاینت و سرور وجود نداره!!!
ارور میده که تکست باکس(مقصد!)،توسط ترد دیگه ای ساخته شده!!

میخواستم ببینم اگر ممکنه،کسی میتونه راهنماییم کنه....
نمیدونم باید کد بذارم یا فایل،برا همین،هردوش رو میذارم!

کد:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
 
namespace serverr
{
    public partial class Form1 : Form
    {
        IPAddress ip;
        IPEndPoint ipend;
        Socket socket1, socket2;
        Thread thredrec, thredacc;
        public Form1()
        {
            InitializeComponent();
        }
         public void recmethod()
        {
            byte[] data = new byte[1024];
            while (true)
            {
                socket2.Receive(data);
                textBox2.AppendText(Encoding.ASCII.GetString(data)  );
            }
 
        }
         public void accmethod()
         {
             while (true)
             {
                 socket2 = socket1.Accept();
                 thredacc = new Thread(new ThreadStart(recmethod));
                 thredacc.Start();
 
             }
         }
 
         private void button1_Click(object sender, EventArgs e)
         {
             ip = IPAddress.Parse(textBox1.Text);
             ipend = new IPEndPoint(ip, 5060);
             socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
             socket1.Bind(ipend);
             socket1.Listen(3);
             thredacc = new Thread(new ThreadStart(accmethod));
             thredacc.Start();
         }
 
         private void button2_Click(object sender, EventArgs e)
         {
              
         }
    }
}

مشاهده پیوست myproject.rar
 

کربلایی

مدیر بازنشسته
در سی شارپ نمیتونید از یک نخ به کنترل های نخ دیگه دسترسی داشته باشید، برای حل مشکل باید از Delegate ها استفاده کنید.
گوگل و msdn میتونه منبع خوبی برای کار با دلگیت ها باشه

more Info:
http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx
http://www.akadia.com/services/dotnet_delegates_and_events.html
http://blogs.msdn.com/b/csharpfaq/archive/2004/03/17/91685.aspx


کد:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ThreadingUIUpdate
{
    public partial class frmMain : Form
    {
        Thread thread;
        TextBox textBox1;
        Button btnStart;
        
        // delegate is used to communicate with UI Thread from a non-UI thread
        public delegate void UpdateTextCallback(string message);

        public frmMain()
        {
            InitializeComponent();

            AddControlsToForm();
        }        

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (btnStart.Text == "Start")
            {
                // Creates a new thread
                thread = new Thread(new ThreadStart(TestThread));
                thread.IsBackground = true;
                thread.Start();

                btnStart.Text = "Stop";
            }
            else
            {
                thread.Abort();

                btnStart.Text = "Start";
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Aborts the thread if the form is closed while the thread is running
            thread.Abort();
        }

        private void TestThread()
        {
            for (int i = 0; i <= 1000; i++)
            {
                Thread.Sleep(1000);
                textBox1.BeginInvoke(new UpdateTextCallback(UpdateText), new object[] { i.ToString() });
            }
        }

        private void UpdateText(string message)
        {
            textBox1.Text = message;
        }

        private void AddControlsToForm()
        {
            textBox1 = new TextBox();
            textBox1.Name = "textBox1";
            textBox1.Text = "Click Start";
            textBox1.Location = new Point(91, 78);
            textBox1.Size = new Size(100, 20);

            btnStart = new Button();
            btnStart.Text = "Start";
            btnStart.Name = "btnStart";
            btnStart.Location = new Point(101, 129);
            btnStart.Size = new Size(75, 23);
            btnStart.Click += new EventHandler(btnStart_Click);

            this.Controls.Add(textBox1);
            this.Controls.Add(btnStart);

            this.ActiveControl = btnStart;
        }
    }
}
موفق باشی
 
آخرین ویرایش:
بالا