Beispiel #1
0
        /// <summary>
        /// 在 50050 端口向目的 IP 发送UDP信息。
        /// </summary>
        /// <param name="iPAddress">目标主机的 IP 地址,当填写 IPAddress.Broadcast 时即为广播。</param>
        /// <param name="carrier">需要发送的信息</param>
        public void UDPMessageSender(IPAddress iPAddress, string carrier)
        {
            Socket     socket   = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep1     = new IPEndPoint(iPAddress, 50050);
            HostInfo   hostInfo = new HostInfo();
            IPEndPoint iepHost  = new IPEndPoint(hostInfo.IPv4Address, 50050);

            byte[] data = Encoding.UTF8.GetBytes(carrier);
            socket.Bind(iepHost);                                                                                                                                               //需要绑定一块活动的网卡,不然无法发送信息
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            socket.SendTo(data, iep1);
            socket.Shutdown(SocketShutdown.Send);//发送完数据后关闭socket
            socket.Close();
        }
Beispiel #2
0
        /// <summary>
        /// 向目的 IP 发送本机上线信息。
        /// </summary>
        /// <param name="iPAddress">目标主机的 IP 地址,当填写 IPAddress.Broadcast 时即为广播。</param>
        /// <param name="replyStatus">广播状态字:请使用 ReplyStatus 枚举类型。</param>
        /// <param name="onlineStatus">在线状态字:请使用 OnlineStatus 结构体。</param>
        public void OnlineMessageSend(IPAddress iPAddress, ReplyStatus replyStatus, string onlineStatus)
        {
            /// 上线状态信息由以下部分组成:
            /// 【本机活动的IP地址】 + 【分隔符“英文逗号 , ”】 + 【上下线状态字 UP 或 DP 】 + 【本用户17位UID】+ 【广播状态字】 + 【本用户名】
            string re = "";

            if (replyStatus == ReplyStatus.NeedReply)
            {
                re = "1";
            }
            else
            {
                re = "0";
            }
            HostInfo hostInfo = new HostInfo();
            string   carrier  = hostInfo.IPv4Address.ToString() + onlineStatus.ToString() + CommonFoundations.HostUID + re + CommonFoundations.HostName;

            UDPMessageSender(iPAddress, carrier);
        }
Beispiel #3
0
        /// <summary>
        ///  监听端口的 TCP 信息发送。一次性使用。
        /// </summary>
        /// <param name="port">监听的端口号</param>
        /// <returns></returns>
        public string TCPMessageListener(int port)
        {
            HostInfo    hostInfo    = new HostInfo();
            TcpListener tcpListener = new TcpListener(hostInfo.IPv4Address, port);//本机ip

            tcpListener.Start();

            string message = "";

            try
            {
                TcpClient newClient = tcpListener.AcceptTcpClient();

                using (newClient)
                {
                    byte[]        buffer       = new byte[newClient.ReceiveBufferSize];//缓冲字节数组
                    NetworkStream clientStream = newClient.GetStream();
                    using (clientStream)
                    {
                        clientStream.Read(buffer, 0, buffer.Length);
                        message = Encoding.UTF8.GetString(buffer).Trim('\0');//收到的信息
                        clientStream.Close();
                    }
                    newClient.Close();
                }
            }
            catch (Exception e)
            {
                return("Error!\r\n" + e.ToString());
            }
            finally
            {
                tcpListener.Stop();
            }
            return(message);
        }