这个是MyQQserver里面的代码,里面也有调用UDPnamespace MyQQServer
红色部分是我书上看见的,但是蓝色部分的LV_SysUser.SelectedItems.Cast中没有Cast,请问是不是代码错了啊?
private void 删除用户ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (LV_SysUser.SelectedItems.Count > 0)//存在被选择的项
{
if (LV_SysUser.SelectedItems.Cast<ListViewItem>().Where<ListViewItem>(item = item.SubItems[4].Text == "12").Count() > 0)//选择项中存在“Close”状态的项
{
if (MessageBox.Show("确定要删除吗?", "软件提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
foreach (ListViewItem lvi in LV_SysUser.SelectedItems)//循环所有被选中的项
{
if (lvi.SubItems[4].Text == "12")//若该用户处于Close状态,则可以删除
{
ClassOptionData OptionData = new ClassOptionData();//实例化数据操作类
OptionData.ExSQL("delete tb_CurreneyUser Where ID = " + lvi.SubItems[0].Text);//删除指定的数据记录
LV_SysUser.Items.Remove(lvi);//在ListView控件中移除选中项
MessageBox.Show("删除成功!");//提示删除成功
}
}
}
}
else
{
MessageBox.Show("在选择的项中,没有处于Close状态的项!");
}
}
else
{
MessageBox.Show("没有项被选择!");
}
}
namespace MyQQServer
{
public partial class F_Server : Form
{
public F_Server()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void SendMsgToAll(ClassMsg msg)//发送消息给所有用户
{
try
{
foreach (System.Windows.Forms.ListViewItem item in this.LV_SysUser.Items)
{
ip = (item.SubItems[1].Text);
int port = Convert.ToInt32(item.SubItems[2].Text);
stream = new ClassSerializers().SerializeBinary(msg);
QQClass.UDPSocket udp = new QQClass.UDPSocket();
udp.Send(ip, port, stream.ToArray());
}
}
catch { }
}
private void SendMsgToOne( ip, int port, ClassMsg msg)//发送消息给一个用户
{
try
{
stream = new ClassSerializers().SerializeBinary(msg);
QQClass.UDPSocket udp = new QQClass.UDPSocket();
udp.Send(ip, port, stream.ToArray());
}
catch { }
}
private void 开始服务ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (((ToolStripMenuItem)sender).Text == "开始服务")
{
((ToolStripMenuItem)sender).Text = "结束服务";
udpSocket1.Active = true;
}
else
{
((ToolStripMenuItem)sender).Text = "开始服务";
udpSocket1.Active = false;
}
}
private void Server(bool IsServer)//开始或停止服务
{
}
private void sockUDP1_DataArrival(byte[] Data, Ip, int Port)
{
DataArrivaldelegate outdelegate = new DataArrivaldelegate(DataArrival);
this.BeginInvoke(outdelegate, new object[] { Data, Ip, Port });
}
private delegate void DataArrivaldelegate(byte[] Data, Ip, int Port);
private void DataArrival(byte[] Data, Ip, int Port) //当有数据到达后的处理进程
{
try
{
ClassMsg msg = new ClassSerializers().DeSerializeBinary((new (Data))) as ClassMsg;
switch (msg.sendKind)
{
case SendKind.SendCommand://命令
{
switch (msg.msgCommand)
{
case MsgCommand.Registering://注册用户
RegisterUser(msg, Ip, Port);
break;
case MsgCommand.Logining://登录用户
UserLogin(msg, Ip, Port, 1);
break;
case MsgCommand.UserList://用户列表
SendUserList(msg, Ip, Port);
break;
case MsgCommand.SendToOne://发送消息给单用户
SendUserMsg(msg, Ip, Port);
break;
case MsgCommand.Close://下线
UpdateUserState(msg, Ip, Port);
break;
}
break;
}
case SendKind.SendMsg://消息
{
switch (msg.msgCommand)
{
case MsgCommand.SendToOne://发送消息给单用户
SendUserMsg(msg, Ip, Port);
break;
}
break;
}
case SendKind.SendFiel://文件
{
break;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void UserLogin(ClassMsg msg, Ip, int Port, int State)
{
RegisterMsg registermsg = (RegisterMsg)new ClassSerializers().DeSerializeBinary(new MemoryStream(msg.Data));
ClassOptionData OptionData = new ClassOptionData();
MsgCommand msgState = msg.msgCommand;
String UserName = registermsg.UserName;
String PassWord = registermsg.PassWord;
String vIP = Ip.ToString();
SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser Where Name = " + "'" + UserName + "'" + " and PassWord = "
+ "'" + PassWord + "'");
DataReader.Read();
string ID = Convert.ToString(DataReader.GetInt32(0));
if (DataReader.HasRows)
{
OptionData.ExSQL("Update tb_CurreneyUser Set Sign = " + Convert.ToString((int)(MsgCommand.Logined)) + ",IP = " + "'" + vIP + "',Port = " + "'" + Port.ToString() + "'" + " Where ID = " + ID);
msg.msgCommand = MsgCommand.Logined;
msg.SID = ID;
SendMsgToOne(Ip, Port, msg);
UpdateUserState(msg, Ip, Port);
}
OptionData.Dispose();
UpdateUser();
}
private void RegisterUser(ClassMsg msg, Ip, int Port)
{
msg = InsertUser(msg, Ip, Port);
UpdateUserList(msg, Ip, Port);
}
private ClassMsg InsertUser(ClassMsg msg, Ip, int Port)
{
RegisterMsg registermsg = (RegisterMsg)new ClassSerializers().DeSerializeBinary(new MemoryStream(msg.Data));
ClassOptionData OptionData = new ClassOptionData();
MsgCommand Sate = msg.msgCommand;
String UserName = registermsg.UserName;
String PassWord = registermsg.PassWord;
String vIP = Ip.ToString();
OptionData.ExSQL("insert into tb_CurreneyUser (IP,Port,Name,PassWord,Sign) values ('" + vIP + "'," +
Port.ToString() + ",'" + UserName + "','" + PassWord + "'," + Convert.ToString((int)(MsgCommand.Registered)) + ")");
SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser");
UpdateUser();
OptionData.Dispose();
msg.msgCommand = MsgCommand.Registered;
SendMsgToOne(Ip, Port, msg);
return msg;
}
private void Form1_Load(object sender, EventArgs e)
{
UpdateUser();
}
private void UpdateUser()
{
ClassOptionData OptionData = new ClassOptionData();
SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser");
LV_SysUser.Items.Clear();
while (DataReader.Read())
{
ListViewItem listItem = new ListViewItem();
listItem.Text = Convert.ToString(DataReader.GetInt32(0));
listItem.SubItems.Add(DataReader.GetString(1));
listItem.SubItems.Add(Convert.ToString(DataReader.GetInt32(2)));
listItem.SubItems.Add(DataReader.GetString(3));
listItem.SubItems.Add(Convert.ToString(DataReader.GetInt32(5)));
LV_SysUser.Items.Add(listItem);
}
OptionData.Dispose();
}
private void F_Server_FormClosed(object sender, FormClosedEventArgs e)
{
udpSocket1.Active = false;
ClassOptionData OptionData = new ClassOptionData();
OptionData.ExSQL("Update tb_CurreneyUser Set Sign =12 Where ID >0");
OptionData.Dispose();
}
private void udpSocket1_DataArrival(byte[] Data, IPAddress Ip, int Port)
{
DataArrivaldelegate outdelegate = new DataArrivaldelegate(DataEvent);
this.BeginInvoke(outdelegate, new object[] { Data, Ip, Port });
}
private void DataEvent(byte[] Data, Ip, int Port)
{
//MessageBox.Show(Encoding.Unicode.GetString(Data));
//this.Text = Encoding.Unicode.GetString(Data);
//MessageBox.Show(this.Text);
}
private void SendUserList(ClassMsg msg, Ip, int Port)
{
ClassUsers Users = new ClassUsers();
ClassOptionData OptionData = new ClassOptionData();
SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser");
while (DataReader.Read())
{
ClassUserInfo UserItem = new ClassUserInfo();
UserItem.UserID = Convert.ToString(DataReader.GetInt32(0));
UserItem.UserIP = DataReader.GetString(1);
UserItem.UserPort = Convert.ToString(DataReader.GetInt32(2));
UserItem.UserName = DataReader.GetString(3);
UserItem.State = Convert.ToString(DataReader.GetInt32(5));
Users.add(UserItem);
}
OptionData.Dispose();
msg.Data = new ClassSerializers().SerializeBinary(Users).ToArray();
udpSocket1.Send(Ip, Port, new ClassSerializers().SerializeBinary(msg).ToArray());
}
private void UpdateUserList(ClassMsg msg, Ip, int Port)
{
ClassUsers Users = new ClassUsers();
ClassOptionData OptionData = new ClassOptionData();
SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser");
while (DataReader.Read())
{
ClassUserInfo UserItem = new ClassUserInfo();
UserItem.UserID = Convert.ToString(DataReader.GetInt32(0));
UserItem.UserIP = DataReader.GetString(1);
UserItem.UserPort = Convert.ToString(DataReader.GetInt32(2));
UserItem.UserName = DataReader.GetString(3);
UserItem.State = Convert.ToString(DataReader.GetInt32(5));
}
msg.Data = new ClassSerializers().SerializeBinary(Users).ToArray();
DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser Where Sign = " + MsgCommand.Logined);
while (DataReader.Read())
{
udpSocket1.Send(IPAddress.Parse(DataReader.GetString(1)), DataReader.GetInt32(2), new ClassSerializers().SerializeBinary(msg).ToArray());
}
OptionData.Dispose();
}
private void SendUserMsg(ClassMsg msg, Ip, int Port)
{
ClassOptionData OptionData = new ClassOptionData();
SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser Where ID = " + msg.RID);
DataReader.Read();
string ip = DataReader.GetString(1);
int port = DataReader.GetInt32(2);
udpSocket1.Send(IPAddress.Parse(ip), port, new ClassSerializers().SerializeBinary(msg).ToArray());
OptionData.Dispose();
DataReader.Dispose();
}
private void UpdateUserState(ClassMsg msg, Ip, int Port)
{
ClassOptionData OptionData = new ClassOptionData();
OptionData.ExSQL("Update tb_CurreneyUser Set Sign = " + Convert.ToString((int)(msg.msgCommand)) + " Where ID = " + msg.SID);
SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser Where Sign = " + Convert.ToString((int)(MsgCommand.Logined)));
if (msg.msgCommand == MsgCommand.Close)
msg.msgID = "Down";
else if (msg.msgCommand == MsgCommand.Logined)
msg.msgID = "Up";
msg.msgCommand = MsgCommand.UpdateState;
while (DataReader.Read())
{
udpSocket1.Send(IPAddress.Parse(DataReader.GetString(1)),
DataReader.GetInt32(2), new ClassSerializers().SerializeBinary(msg).ToArray());
}
OptionData.Dispose();
UpdateUser();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}