Beispiel #1
0
        /// <summary>
        /// 获取手机视频窗体
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public PhoneVideoForm GetPhoneVideoForm(string username)
        {
            PhoneVideoForm form = null;

            foreach (Form tempForm in this.OwnedForms)
            {
                if (tempForm is PhoneVideoForm)
                {
                    PhoneVideoForm tempForm2 = tempForm as PhoneVideoForm;
                    if (tempForm2.UserName == username)
                    {
                        form = tempForm2;
                        break;
                    }
                }
            }
            return(form);
        }
Beispiel #2
0
 private void phoneslistView_MouseDoubleClick(object sender, MouseEventArgs e)
 {
     if (phoneslistView.SelectedItems != null &&
         phoneslistView.SelectedItems.Count > 0)
     {
         ListViewItem tempItem     = phoneslistView.SelectedItems[0];
         string       tempUserName = tempItem.SubItems[1].Text;
         int          tempIndex    = GetPhoneUserIndex(tempUserName);
         if (tempIndex >= 0)
         {
             UserClass tempUser = (UserClass)(PhoneUsersArray[tempIndex]);
             if (tempUser != null)
             {
                 PhoneVideoForm form = new PhoneVideoForm(tempUser);
                 this.AddOwnedForm(form);
                 form.Show();
             }
         }
     }
 }
Beispiel #3
0
        /// <summary>
        /// 处理客户端传递数据及处理事情
        /// </summary>
        private void ProcessClient()
        {
            Socket client    = clientSocket;
            bool   keepalive = true;

            while (keepalive)
            {
                Thread.Sleep(50);
                Byte[] buffer = null;
                bool   tag    = false;
                try
                {
                    buffer = new Byte[1024];                              //client.Available
                    int count = client.Receive(buffer, SocketFlags.None); //接收客户端套接字数据
                    if (count > 0)                                        //接收到数据
                    {
                        tag = true;
                    }
                }
                catch (Exception e)
                {
                    keepalive = false;
                    if (client.Connected)
                    {
                        client.Disconnect(true);
                    }
                    client.Close();
                }
                if (!tag)
                {
                    if (client.Connected)
                    {
                        client.Disconnect(true);
                    }
                    client.Close();
                    keepalive = false;
                }

                string clientCommand = "";
                try
                {
                    clientCommand = System.Text.Encoding.UTF8.GetString(buffer); //转换接收的数据,数据来源于客户端发送的消息
                    if (clientCommand.Contains("%7C"))                           //从Android客户端传递部分数据
                    {
                        clientCommand = clientCommand.Replace("%7C", "|");       //替换UTF中字符%7C为|
                    }
                }
                catch
                {
                }
                //分析客户端传递的命令来判断各种操作
                string[] messages = clientCommand.Split('|');
                if (messages != null && messages.Length > 0)
                {
                    string tempStr = messages[0];  //第一个字符串为命令
                    if (tempStr == "PHONECONNECT") //手机连接服务器
                    {
                        try
                        {
                            string tempClientName = messages[1].Trim();
                            PhoneClientSockets.Remove(messages[1]);      //删除之前与该用户的连接
                            PhoneClientSockets.Add(messages[1], client); //建立与该客户端的Socket连接

                            UserClass tempUser = new UserClass();
                            tempUser.UserName  = tempClientName;
                            tempUser.LoginTime = DateTime.Now;
                            Socket tempSocket = (Socket)PhoneClientSockets[tempClientName];
                            tempUser.IPAddress = tempSocket.RemoteEndPoint.ToString();

                            int tempIndex = PhoneUserNamesArray.IndexOf(tempClientName);
                            if (tempIndex >= 0)
                            {
                                PhoneUserNamesArray[tempIndex] = tempClientName;
                                PhoneUsersArray[tempIndex]     = tempUser;
                                MemoryStream stream2 = (MemoryStream)StreamArray[tempIndex];
                                if (stream2 != null)
                                {
                                    stream2.Close();
                                    stream2.Dispose();
                                }
                            }
                            else//新增加
                            {
                                PhoneUserNamesArray.Add(tempClientName);
                                PhoneUsersArray.Add(tempUser);
                                StreamArray.Add(null);
                            }
                            RefreshPhoneUsers();
                        }
                        catch (Exception except)
                        {
                        }
                    }
                    else if (tempStr == "PHONEDISCONNECT")//某个客户端退出了
                    {
                        try
                        {
                            string tempClientName = messages[1];
                            RemovePhoneUser(tempClientName);

                            int tempPhoneIndex = PhoneUserNamesArray.IndexOf(tempClientName);
                            if (tempPhoneIndex >= 0)
                            {
                                PhoneUserNamesArray.RemoveAt(tempPhoneIndex);
                                MemoryStream memStream = (MemoryStream)StreamArray[tempPhoneIndex];
                                if (memStream != null)
                                {
                                    memStream.Close();
                                    memStream.Dispose();
                                }
                                StreamArray.RemoveAt(tempPhoneIndex);
                            }
                            Socket tempSocket = (Socket)PhoneClientSockets[tempClientName];//第1个为客户端的ID,找到该套接字
                            if (tempSocket != null)
                            {
                                tempSocket.Close();
                                PhoneClientSockets.Remove(tempClientName);
                            }
                            keepalive = false;
                        }
                        catch (Exception except)
                        {
                        }
                        RefreshPhoneUsers();
                    }
                    else if (tempStr == "PHONEVIDEO")//接收手机数据流
                    {
                        try
                        {
                            string tempClientName = messages[1];
                            string tempForeStr    = messages[0] + "%7C" + messages[1] + "%7C";
                            int    startCount     = System.Text.Encoding.UTF8.GetByteCount(tempForeStr);
                            try
                            {
                                MemoryStream stream = new MemoryStream();
                                if (stream.CanWrite)
                                {
                                    stream.Write(buffer, startCount, buffer.Length - startCount);
                                    int len = -1;
                                    while ((len = client.Receive(buffer)) > 0)
                                    {
                                        stream.Write(buffer, 0, len);
                                    }
                                }
                                stream.Flush();

                                int tempPhoneIndex = PhoneUserNamesArray.IndexOf(tempClientName);
                                if (tempPhoneIndex >= 0)
                                {
                                    MemoryStream stream2 = (MemoryStream)StreamArray[tempPhoneIndex];
                                    if (stream2 != null)
                                    {
                                        stream2.Close();
                                        stream2.Dispose();
                                    }
                                    StreamArray[tempPhoneIndex] = stream;

                                    PhoneVideoForm form = GetPhoneVideoForm(tempClientName);
                                    if (form != null)
                                    {
                                        form.DataStream = stream;
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                        catch (Exception except)
                        {
                        }
                    }
                }
                else//客户端发送的命令或字符串为空,结束连接
                {
                    try
                    {
                        client.Close();
                        keepalive = false;
                    }
                    catch
                    {
                        keepalive = false;
                    }
                }
            }
        }