示例#1
0
        //房主等待每位群成员的消息
        public void host_receive(string his_name, Net_class connected_NC)
        {
            myFileInfo last_fileInfo = new myFileInfo(0, "");

            while (true)
            {
                if (!connected_NC.sock.Connected)
                {
                    return;
                }

                int    info_length;
                byte[] receive_info = connected_NC.Receive_bytes(out info_length);
                string msg          = Encoding.UTF8.GetString(receive_info, 1, info_length - 1);

                //文字消息
                if (info_length > 0 && receive_info[0] == 0)
                {
                    add_msg2list(his_name, msg, Color.Black);
                    send_msg2all(his_name, msg, false);
                }
                //文件
                else if (info_length > 0 && receive_info[0] == 2)
                {
                    if (last_fileInfo.length == 0)
                    {
                        continue;
                    }

                    byte[] file_bytes = new byte[info_length - 1];
                    Buffer.BlockCopy(receive_info, 1, file_bytes, 0, info_length - 1);
                    host_file_list.Add(last_fileInfo, file_bytes);
                    add_file2list(last_fileInfo, his_name, Color.Black);
                    send_fileInfo2all_users(last_fileInfo, his_name);
                }
                //文件长度和文件名
                else if (info_length > 1 && receive_info[0] == 3)
                {
                    string[] msg_split = msg.Split('_');
                    long     file_len;
                    if (!long.TryParse(msg_split[0], out file_len))
                    {
                        continue;
                    }

                    string file_name = msg_split[1];
                    for (int i = 2; i < msg_split.Length; i++)
                    {
                        file_name += ("_" + msg_split[i]);
                    }

                    last_fileInfo = new myFileInfo(file_len, file_name);
                }
                //用户退出消息
                else if (info_length > 1 && receive_info[0] == 4)
                {
                    if (msg == "q")
                    {
                        for (int i = 1; i < listView_members.Items.Count; i++)
                        {
                            string item_name = listView_members.Items[i].SubItems[1].Text;
                            if (item_name == his_name)
                            {
                                listView_members.Items.Remove(listView_members.Items[i]);
                            }
                        }
                        connected_name_NC[his_name].Close();
                        connected_name_NC.Remove(his_name);
                        return;
                    }
                }
                //请求文件
                else if (info_length > 1 && receive_info[0] == 5)
                {
                    string[]   msg_split    = msg.Split('_');
                    myFileInfo get_fileInfo = new myFileInfo();
                    if (!long.TryParse(msg_split[0], out get_fileInfo.length))
                    {
                        continue;
                    }

                    get_fileInfo.name = msg_split[1];
                    for (int i = 2; i < msg_split.Length; i++)
                    {
                        get_fileInfo.name += ("_" + msg_split[i]);
                    }

                    byte[] file_byte;
                    if (!host_file_list.TryGetValue(get_fileInfo, out file_byte))
                    {
                        connected_name_NC[his_name].Send_message_NumString_asy(5, "n");
                    }
                    else
                    {
                        connected_name_NC[his_name].Send_message_NumByte_asy(2, file_byte, file_byte.Length);
                    }
                }
            }
        }