示例#1
0
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            string filepath = Helper.Search();

            if (filepath.Length > 0)
            {
                kong : string ip = tbLocalIP.Text;
                string port = tbLocalPort.Text;
                if (ip.Trim().Length < 7 || port.Trim().Length < 3)
                {
                    tbLocalIP.Text   = LocalIP;
                    tbLocalPort.Text = LocalPort;
                    goto kong;
                }
                FileStream fStream = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite);
                byte[]     data    = new byte[fStream.Length];
                fStream.Read(data, 0, data.Length);
                fStream.Close();

                Helper_Message msg = new Helper_Message();
                msg.SName     = name;
                msg.SIP       = LocalIP;
                msg.SPort     = LocalPort;
                msg.RIP       = ip;
                msg.RPort     = port;
                msg.Data      = data;
                msg.sendKind  = SendKind.SendFile;
                msg.sendState = SendState.MessageSingle;
                if (canSent)
                {
                    udpSocket.UDPSend(msg);
                }
            }
        }
示例#2
0
        private void btSend_Click(object sender, RoutedEventArgs e)
        {
            kong : string ip = tbLocalIP.Text;
            string port = tbLocalPort.Text;

            if (ip.Trim().Length < 7 || port.Trim().Length < 3)
            {
                tbLocalIP.Text   = LocalIP;
                tbLocalPort.Text = LocalPort;
                goto kong;
            }

            byte[] data = Encoding.Unicode.GetBytes(tbSendMsg.Text);

            Helper_Message msg = new Helper_Message();

            msg.SName     = name;
            msg.SIP       = LocalIP;
            msg.SPort     = LocalPort;
            msg.RIP       = ip;
            msg.RPort     = port;
            msg.Data      = data;
            msg.sendKind  = SendKind.SendMsg;
            msg.sendState = SendState.MessageSingle;
            var dat = Helper_Serializers.Instance.SerializeBinary(msg).ToArray();

            if (canSent)
            {
                udpSocket.UDPSend(msg);
            }
        }
示例#3
0
        // 开启
        private void button6_Click(object sender, RoutedEventArgs e)
        {
            userName.IsEnabled  = false;
            comboBox1.IsEnabled = false;

            name    = userName.Text.Trim();
            LocalIP = comboBox1.SelectedValue.ToString().Split(' ')[1];
            net.ForEach(i => { foreach (var ip in i.IP)
                               {
                                   if (ip == LocalIP)
                                   {
                                       LocalSubNet = i.Subnet[0];
                                   }
                               }
                        });

            udpSocket.UDPRecived += new EventHandler(udpSocket_Recived);
            udpSocket.Message     = new Helper_Message();

            UDPPort?udpPort = udpSocket.UDPStart(LocalIP, portList);

            if (udpPort != null)
            {
                LocalPort = udpPort.Value.Port.ToString();
                Helper_Message message = new Helper_Message();
                message.SName    = name;
                message.sendKind = SendKind.SendLogin;
                message.SIP      = LocalIP;
                message.SPort    = LocalPort;
                message.RIP      = Broadcast();
                message.Data     = Helper.StringToByte("迹");
                portList.ForEach(i =>
                {
                    message.RPort = i.ToString();
                    udpSocket.UDPSend(message);
                });
                canSent           = true;
                button6.Content   = "侦听端口:" + LocalPort;
                button6.IsEnabled = false;
            }
        }
示例#4
0
        // 接受处理事件
        void udpSocket_Recived(object sender, EventArgs e)
        {
            if (udpSocket.Message.Data != null)
            {
                switch (udpSocket.Message.sendKind)//发送类型
                {
                    #region 发送文件 SendKind.SendFile
                case SendKind.SendFile:
                {
                    switch (udpSocket.Message.sendState)
                    {
                    case SendState.Start:
                    {
                        //文件名字
                        info.Clear();
                        break;
                    }

                    case SendState.Sending:
                    {
                        info.AddRange(udpSocket.Message.Data);
                        break;
                    }

                    case SendState.End:
                    {
                        //文件类型
                        byte[] bt = info.ToArray();
                        using (MemoryStream ms = new MemoryStream(bt))
                        {
                            BitmapDecoder decoder = BitmapDecoder.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                            var           img     = decoder.Frames[0];
                            //ImageSourceConverter imageSourceConverter = new ImageSourceConverter();
                            //var img = (BitmapSource)imageSourceConverter.ConvertFrom(ms);
                            img.Freeze();
                            Dispatcher.Invoke((Action) delegate
                                    {
                                        //Helper.SaveImageCapture((BitmapSource)imageSourceConverter.ConvertFrom(ms));
                                        image4.Source = img;
                                    });
                        }
                        break;
                    }

                    default: break;
                    }
                }
                break;

                    #endregion
                    #region 发送消息 SendKind.SendMsg
                case SendKind.SendMsg:
                {
                    switch (udpSocket.Message.sendState)
                    {
                    case SendState.MessageSingle:
                    {
                        Dispatcher.Invoke((Action) delegate
                                {
                                    tbMsg.Text = Encoding.Unicode.GetString(udpSocket.Message.Data);
                                });
                        break;
                    }

                    case SendState.Start:
                    {
                        info.Clear();
                        break;
                    }

                    case SendState.Sending:
                    {
                        info.AddRange(udpSocket.Message.Data);
                        break;
                    }

                    case SendState.End:
                    {
                        Dispatcher.Invoke((Action) delegate
                                {
                                    tbMsg.Text = Encoding.Unicode.GetString(info.ToArray());
                                });
                        break;
                    }

                    default: break;
                    }
                    break;
                }

                    #endregion
                    #region 发送命令 SendKind.SendCommand
                case SendKind.SendCommand:
                {
                    switch (udpSocket.Message.msgCommand)
                    {
                        #region 发送视频 MsgCommand.Videoing
                    case MsgCommand.Videoing:
                    {
                        switch (udpSocket.Message.sendState)
                        {
                        case SendState.Start:
                        {
                            info.Clear();
                            break;
                        }

                        case SendState.Sending:
                        {
                            info.AddRange(udpSocket.Message.Data);
                            break;
                        }

                        case SendState.End:
                        {
                            byte[] bt = info.ToArray();
                            using (MemoryStream ms = new MemoryStream(bt))
                            {
                                // pictureBox1.Image = Image.FromStream(ms);
                            }
                            break;
                        }

                        default: break;
                        }
                        break;
                    }

                        #endregion
                    default: break;
                    }
                }
                break;
                    #endregion

                case SendKind.SendLogin:
                {
                    Dispatcher.Invoke((Action) delegate
                        {
                            var u = Users.FirstOrDefault(i => i.SIP.Equals(udpSocket.Message.SIP) && i.SPort.Equals(udpSocket.Message.SPort));
                            if (u == null)
                            {
                                Users.Add(udpSocket.Message);

                                Helper_Message message = new Helper_Message();
                                message.SName          = name;
                                message.sendKind       = SendKind.SendLogin;
                                message.SIP            = LocalIP;
                                message.SPort          = LocalPort;
                                message.RIP            = udpSocket.Message.SIP;
                                message.Data           = Helper.StringToByte("迹");

                                message.RPort = udpSocket.Message.SPort;
                                udpSocket.UDPSend(message);
                            }
                        });

                    break;
                }

                case SendKind.SendLeave:
                {
                    Dispatcher.Invoke((Action) delegate
                        {
                            var u = Users.FirstOrDefault(i => i.SIP.Equals(udpSocket.Message.SIP) && i.SPort.Equals(udpSocket.Message.SPort));
                            if (u != null)
                            {
                                Users.Remove(u);
                            }
                        });

                    break;
                }

                default: break;
                }
            }
        }