PDA

View Full Version : سوال: مراحل لازم برای ساخت برنامه چت تحت شبکه با پروتکل udp



FVVSBN
پنج شنبه 02 دی 1389, 11:20 صبح
سلام
من

این کد مشکلش کجاست؟
ارسال میکنه ولی دریافت نمیکنه


publicpartialclassFormClient : Form1
{
UdpClient udpClient;

Thread newThread;
UnicodeEncoding uniCodeEncoding = newUnicodeEncoding();
bool ChattingStatus = false;
public FormClient()
{
InitializeComponent();
udpClient = newUdpClient();

newThread = newThread(newThreadStart(ClientListener));
newThread.Start();
}

void ClientListener()
{
while (ChattingStatus)
{
IPEndPoint ipep;
if (txtIP.Text.Trim() != "")
{
ipep = newIPEndPoint(IPAddress.Parse(txtIP.Text.Trim()), 3977);

}
else
{
ipep = null;
}

byte[] byteReciver = udpClient.Receive(ref ipep);
toolStripStatusLabel1.Text = "Reciver\tCalculating";
txtRecivedMessage.Text += "\r\n" + uniCodeEncoding.GetString(byteReciver);
toolStripStatusLabel1.Text = "Recived";

}
}

privatevoid btnSendMessage_Click(object sender, EventArgs e)
{
byte[] byteSender = uniCodeEncoding.GetBytes(txtSendMessage.Text);
IPEndPoint ipep = newIPEndPoint(IPAddress.Parse(txtIP.Text.Trim()), 3977);
udpClient.Send(byteSender, byteSender.Length,ipep);
ChattingStatus = true;
toolStripStatusLabel1.Text = "Sent";
txtSendMessage.Clear();
txtSendMessage.Focus();
}

privatevoid txtSendMessage_TextChanged(object sender, EventArgs e)
{
if (txtSendMessage.Text.Length > 0 )
{
btnSendMessage.Enabled = true;
}
else
{
btnSendMessage.Enabled = false;
}
}

privatevoid txtSendMessage_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && btnSendMessage.Enabled == true)
{
btnSendMessage_Click(null, null);
}
}

privatevoid FormClient_FormClosing(object sender, FormClosingEventArgs e)
{
udpClient.Close();
}
privatevoid exitToolStripMenuItem_Click(object sender, EventArgs e)
{
FormClient_FormClosing(null, null);
}






}

mahboube
پنج شنبه 02 دی 1389, 12:19 عصر
اينم يه چته كه كار ميكنه.
ازcode projet:
63994

FVVSBN
چهارشنبه 08 دی 1389, 15:09 عصر
خطایی که میده اینه:

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

در این خط:

txtRecivedMessage.Text += "\r\n" + uniCodeEncoding.GetString(byteReciver);

FVVSBN
جمعه 10 دی 1389, 20:39 عصر
خطایی که میده اینه:

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

در این خط:

txtRecivedMessage.Text += "\r\n" + uniCodeEncoding.GetString(byteReciver);


آقا این مشکل ما حل شدنی هست؟