Tekst Chat klient - serwer.

0

Witam,

mam napisanego klienta i serwer dla mojego chatu, jednak gdy próbuję coś wysłać to wyskakuje mi błąd:

Additional information: Cross-thread operation not valid: Control 'listBox1' accessed from a thread other than the thread it was created on.

kody poniżej, zaznaczyłem miejsce gdzie jest ten listBox1, w fun().

Proszę o pomoc.

SERWER


using System;
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 serwer___audio
{
    public partial class Form1 : Form
    {
        static byte[] Buffer { get; set; }
        static Socket SocketServer;
        Thread th;

        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {

            SocketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            SocketServer.Bind(new IPEndPoint(0, 5000));

            SocketServer.Listen(100);

            th = new Thread(new ThreadStart(fun));
            th.Start();
        }

        private void fun()
        {
            Socket Accepted = SocketServer.Accept();

            while (true)
            {
                Buffer = new byte[Accepted.SendBufferSize];
                int bytesRead = Accepted.Receive(Buffer);

                byte[] formatted = new byte[bytesRead];

                for (int i = 0; i < bytesRead; i++)
                {
                    formatted[i] = Buffer[i];
                }

                string strData = Encoding.ASCII.GetString(formatted);
                if (strData.Length > 0)
                    listBox1.Items.Add(strData); // TU JEST LISTBOX1 !!!!!!

            }
            //SocketServer.Close();
            Accepted.Close();
        }

        private void close_Click(object sender, EventArgs e)
        {
            SocketServer.Close();
            //Accepted.Close();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}


 

KLIENT

 

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

namespace klient___audio
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Socket Socketclient;


        void Connect(string ServerIP, int Port)
        {
            try
            {
                Socketclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint Server_EndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), Port);
                Socketclient.Connect(Server_EndPoint);
                remark.Text = "Połączono z " + ServerIP;

            }
            catch
            {
                remark.Text = "Nie można się połączyć";
            }           
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Connect("192.168.1.65", 5000);
        }

        private void remark_TextChanged(object sender, EventArgs e)
        {

        }

        private void text_send_TextChanged(object sender, EventArgs e)
        {

        }

        private void send_Click(object sender, EventArgs e)
        {

                string Text = text_send.Text;

                byte[] data = Encoding.ASCII.GetBytes(Text);

                Socketclient.Send(data);

        }

        private void close_Click(object sender, EventArgs e)
        {
            Socketclient.Close();
        }  
    }
}


0

Komunikat jest jasny, z wątku innego niż główny gui nie można modyfikować elementów gui.
Szukaj haseł: InvokeRequired i Invoke - obie właściwość i metoda pochodzą chyba z klasy Control.

0
if(listBox1.InvokeRequired)
{
listBox1.Invoke(new EventHandler(delegate{
listBox1.Items.Add(strData);
}));
}
else
{
listBox1.Items.Add(strData);
}

Mam nadzieje że pomogłem.

0

Witam,

mam teraz trochę inny problem.

Mój klient łączy się z serwerem, wysyła pięknie mu dane.

ale chciałbym aby teraz ten klient też odbierał tene od serwera.

Poniżej mój początkowy kod z łączeniem się.

gdy jest bez:

Socketclient.Bind(Server_EndPoint);
Socketclient.Listen(100);

to łączy klient się z serwerem doskonale.

Jednak jak dodaję właśnie ten kod aby nasłuchiwać to już nie da się w ogóle połączyć z serwerem.

Proszę o pomoc.

 Socketclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint Server_EndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), Port);
                Socketclient.Connect(Server_EndPoint);
                remark.Text = "Połączono z " + ServerIP;

                Socketclient.Bind(Server_EndPoint);
                Socketclient.Listen(100);

1 użytkowników online, w tym zalogowanych: 0, gości: 1