Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void Listener_MessageSend(object sender, EventArgs args, string msg)
        {
            //クライアントに接続応答する
            //NetworkStreamを取得
            var writer = serverSocket.GetStream();


            //送信文字列をByte型配列に変換
            Encoding enc = System.Text.Encoding.UTF8;

            byte[] sendBytes = enc.GetBytes(msg + '\n');
            //データを送信する
            writer.Write(sendBytes, 0, sendBytes.Length);
            writer.FlushAsync();
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FtpConnection"/> class.
        /// </summary>
        /// <param name="server">The server this connection belongs to</param>
        /// <param name="socket">The socket to use to communicate with the client</param>
        /// <param name="encoding">The encoding to use for the LIST/NLST commands</param>
        public FtpConnection([NotNull] FtpServer server, [NotNull] ITcpSocketClient socket, [NotNull] Encoding encoding)
        {
            Server        = server;
            _socket       = socket;
            RemoteAddress = new Address(socket.RemoteAddress, socket.RemotePort);
            SocketStream  = OriginalStream = socket.GetStream();
            Encoding      = encoding;
            Data          = new FtpConnectionData(this);
            var commandHandlers = Server.CommandsHandlerFactory.CreateCommandHandlers(this).ToList();

            CommandHandlers = commandHandlers
                              .SelectMany(x => x.Names, (item, name) => new { Name = name, Item = item })
                              .ToDictionary(x => x.Name, x => x.Item, StringComparer.OrdinalIgnoreCase);

            // Add stand-alone extensions
            AddExtensions(Server.CommandsHandlerFactory.CreateCommandHandlerExtensions(this));

            // Add extensions provided by command handlers
            foreach (var commandHandler in commandHandlers)
            {
                AddExtensions(commandHandler.GetExtensions());
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Listeners the connection.
        /// </summary>
        /// <param name="s">S.</param>
        /// <param name="e">E.</param>
        public void ListenerConnection(Object s, EventArgs e)
        {
            bool disconnected = false;
            var  listener     = new TcpSocketListener();

            Listener_Rady.Text      = "接続待機中";
            Listener_Rady.TextColor = Color.Blue;

            // ホスト名を取得する
            var TcpLocator = DependencyService.Get <IGeolocator>();

            Listener_Disp.Text = TcpLocator.getIPAddress();

            // when we get connections, read bytes until we get -1 (eof)
            listener.ConnectionReceived += async(sender, args) =>
            {
                //サーバー用ソケットの取得
                serverSocket = /*(Sockets.Plugin.TcpSocketClient)*/ args.SocketClient;//*/ GetSocketClient(args);

                string Adress    = args.SocketClient.RemoteAddress;
                var    port      = args.SocketClient.RemotePort;
                var    bytesRead = -1;
                var    buf       = new byte[1024];

                // read from the 'ReadStream' property of the socket client to receive data
                bytesRead = await args.SocketClient.ReadStream.ReadAsync(buf, 0, buf.Length);

                //受信したデータを文字列に変換
                Encoding enc    = System.Text.Encoding.UTF8;
                string   resMsg = enc.GetString(buf, 0, bytesRead);

                //末尾の\nを削除
                resMsg = resMsg.TrimEnd('\n');

                if (resMsg == "+OK to BondotConnectio!" | disconnected == false)// ID Check
                {
                    string listenerMsg = string.Format("クライアントに接続しました IpAdress{0} Port {1}" + '\n', Adress, port);

                    Device.BeginInvokeOnMainThread(() =>
                    {
                        DisplayAlert("For listener Message ", listenerMsg, "OK");

                        Listener_Rady.Text      = "接続中";
                        Listener_Rady.TextColor = Color.Red;
                        Listener_Send.IsEnabled = true;
                    });

                    //クライアントに接続応答する
                    //NetworkStreamを取得
                    System.IO.Stream writer = serverSocket.GetStream();

                    //クライアントに送信する文字列を作成
                    string sendMsg = resMsg;
                    //文字列をByte型配列に変換
                    byte[] sendBytes = enc.GetBytes(sendMsg + "listener-Anser" + '\n');
                    //データを送信する
                    writer.Write(sendBytes, 0, sendBytes.Length);
                    disconnected = true;
                }
                else
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Listener_Disp.Text = resMsg;
                        DisplayAlert("For Client Message ", resMsg, "OK");
                    });
                }
                //閉じる
                // ns.Dispose();
                // serverSocket.Dispose();
            };

            //IPアドレスとポートをバインドして接続待機
            /// bind to the listen port across all interfaces
            listener.StartListeningAsync(Port);
        }