JBTALKS.CC
标题:
c# 問題!!Server and Client
[打印本页]
作者:
狂天使
时间:
2011-5-30 02:36 AM
标题:
c# 問題!!Server and Client
各位
當我Client 按 connect 的時候我的Server 出現這個問題,請問怎樣解決
這是 Server
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ChatServer
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class ChatServer : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private int listenport = 5555;
private TcpListener listener;
private System.Windows.Forms.ListBox lbClients;
private ArrayList clients;
private Thread processor;
private Socket clientsocket;
private Thread clientservice;
public ChatServer()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
clients = new ArrayList();
processor = new Thread(new ThreadStart(StartListening));
processor.Start();
}
/// <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.lbClients = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// lbClients
//
this.lbClients.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lbClients.ItemHeight = 16;
this.lbClients.Location = new System.Drawing.Point(16, 8);
this.lbClients.Name = "lbClients";
this.lbClients.Size = new System.Drawing.Size(264, 228);
this.lbClients.TabIndex = 0;
//
// ChatServer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lbClients});
this.Name = "ChatServer";
this.Text = "ChatServer";
this.ResumeLayout(false);
}
#endregion
protected override void OnClosed(EventArgs e)
{
try
{
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, "QUIT|");
cl.Sock.Close();
cl.CLThread.Abort();
}
listener.Stop();
if(processor != null)
processor.Abort();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString() );
}
base.OnClosed(e);
}
private void StartListening()
{
listener = new TcpListener(listenport);
listener.Start();
while (true) {
try
{
Socket s = listener.AcceptSocket();
clientsocket = s;
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
}
catch(Exception e)
{
Console.WriteLine(e.ToString() );
}
}
//listener.Stop();
}
private void ServiceClient()
{
Socket client = clientsocket;
bool keepalive = true;
while (keepalive)
{
Byte[] buffer = new Byte[1024];
client.Receive(buffer);
string clientcommand = System.Text.Encoding.ASCII.GetString(buffer);
string[] tokens = clientcommand.Split(new Char[]{'|'});
Console.WriteLine(clientcommand);
if (tokens[0] == "CONN")
{
for(int n=0; n<clients.Count; n++) {
Client cl = (Client)clients[n];
SendToClient(cl, "JOIN|" + tokens[1]);
}
EndPoint ep = client.RemoteEndPoint;
//string add = ep.ToString();
Client c = new Client(tokens[1], ep, clientservice, client);
clients.Add(c);
string message = "LIST|" + GetChatterList() +"\r\n";
SendToClient(c, message);
//lbClients.Items.Add(c.Name + " : " + c.Host.ToString());
lbClients.Items.Add(c);
}
if (tokens[0] == "CHAT")
{
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, clientcommand);
}
}
if (tokens[0] == "PRIV") {
string destclient = tokens[3];
for(int n=0; n<clients.Count; n++) {
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(tokens[3]) == 0)
SendToClient(cl, clientcommand);
if(cl.Name.CompareTo(tokens[1]) == 0)
SendToClient(cl, clientcommand);
}
}
if (tokens[0] == "GONE")
{
int remove = 0;
bool found = false;
int c = clients.Count;
for(int n=0; n<c; n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, clientcommand);
if(cl.Name.CompareTo(tokens[1]) == 0)
{
remove = n;
found = true;
lbClients.Items.Remove(cl);
//lbClients.Items.Remove(cl.Name + " : " + cl.Host.ToString());
}
}
if(found)
clients.RemoveAt(remove);
client.Close();
keepalive = false;
}
}
}
private void SendToClient(Client cl, string message)
{
try{
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(message.ToCharArray());
cl.Sock.Send(buffer,buffer.Length,0);
}
catch(Exception e){
cl.Sock.Close();
cl.CLThread.Abort();
clients.Remove(cl);
lbClients.Items.Remove(cl.Name + " : " + cl.Host.ToString());
//MessageBox.Show("Could not reach " + cl.Name + " - disconnected","Error",
//MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private string GetChatterList()
{
string chatters = "";
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
chatters += cl.Name;
chatters += "|";
}
chatters.Trim(new char[]{'|'});
return chatters;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ChatServer());
}
}
}
复制代码
它說這兒有問題
lbClients.Items.Add(c);
复制代码
錯誤信息
Cross-thread operation not valid: Control 'lbClients' accessed from a thread other than the thread it was created on.
我該怎樣解決
作者:
宅男-兜着走
时间:
2011-5-30 07:47 PM
各位
當我Client 按 connect 的時候我的Server 出現這個問題,請問怎樣解決
這是 Server它說這兒有問 ...
狂天使 发表于 2011-5-30 02:36 AM
因为是 threading, UI 是在另外个thread, 你必须用 Invoke Method.
上网查 C# Threading Invoke.
作者:
goodday
时间:
2011-5-30 10:09 PM
delegate 搞定她
你的 delegate 不强吧
呵呵 你过来 我这里 我教你
呵呵
作者:
goodday
时间:
2011-5-30 10:11 PM
我还有一大堆的 书
我请人荒 现在能做的是 引诱你
呵呵
作者:
狂天使
时间:
2011-5-31 12:00 AM
goodday... 你不會吧 =.= 你這樣引誘我,真的很吸引我也很想過來幫你啊。但是現在不行。哈哈。
現在還在請人荒哦,沒人來應徵嗎?
作者:
goodday
时间:
2011-5-31 10:23 AM
有两个了,
一个充 大头鬼
说会什么 会什么的
我给我的 VS 他, 拿走其中的一段
compile 问他 bug 在哪里
找不出来
另外几个 是 HTML javascripts 罢了
我不4是没请, 我丢给了我师父罢了
我师父也是很厉害的 programmer
他们住的地方比较 近我师傅
跟过我师傅的出来都是 超厉害的
包括本人
我背后还有2间公司都请人
请人 哦
作者:
zechs
时间:
2011-5-31 12:21 PM
回复
6#
goodday
有jb的公司吗
?
作者:
goodday
时间:
2011-6-2 11:46 AM
JB 的要 ASP 而已
你要的话 通知我
呵呵
作者:
goodday
时间:
2011-6-2 11:47 AM
上来找我啦
我拿我的 书借你 office 看
不能借回家
很多人没还我
欢迎光临 JBTALKS.CC (https://www.jbtalks.cc/)
Powered by Discuz! X2.5