Ejemplo n.º 1
0
        /// <summary>
        /// A listener object that responds when ever there has been a connection established
        /// </summary>
        /// <param name="sender">
        ///
        /// </param>
        /// <param name="args">
        ///
        /// </param>
        private async void ConnectionReceived(Windows.Networking.Sockets.StreamSocketListener sender, Windows.Networking.Sockets.StreamSocketListenerConnectionReceivedEventArgs args)
        {
            string request;

            using (var streamReader = new StreamReader(args.Socket.InputStream.AsStreamForRead()))
            {
                request = await streamReader.ReadLineAsync();
            }

            this.Output(MessageHelper.MessageType.Data, request);

            // if responder exists then respond else don't bother
            if (responderObject != null)
            {
                // deal with the responce appropriately
                string responce = responderObject.Respond(ref request);

                using (var streamWriter = new StreamWriter(args.Socket.OutputStream.AsStreamForWrite()))
                {
                    // call the responder object and send back the responce
                    streamWriter.WriteLine(responce);

                    // output the message to the helper if it exists
                    this.Output(MessageHelper.MessageType.Status, string.Format("server acted on the response: \"{0}\" appropiatly", responce));
                }
            }
        }
        /// <summary>
        /// this function should only be run in its own thread. It is triggered
        /// by the start function.
        /// </summary>
        private void Listen()
        {
            // Buffer for reading data
            Byte[] bytes = new Byte[256];

            // keep listening until the user is done with this object or until a
            // exception is thrown
            try
            {
                TcpClient client = Server.AcceptTcpClient();
                while (IsListening)
                {
                    // the command will sit and wait until you can connect
                    // You could also user server.AcceptSocket() here.

                    // clean up the data value
                    data = "";
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    // this object handels reading and writing
                    NetworkStream stream = client.GetStream();



                    while (stream.DataAvailable || data == "")
                    {
                        // this feild holds the bytes recived by the packet
                        int numberOfBytesRead = stream.Read(bytes, 0, bytes.Length);

                        // Convert the bytes back to a string and save them to the output buffer
                        sb.Append(System.Text.Encoding.ASCII.GetString(bytes, 0, numberOfBytesRead));
                        data = sb.ToString();
                    }

                    // if an output option exists output the message recived
                    if (Output != null)
                    {
                        Output.DisplayMessage(MessageHelper.MessageType.Data, data);
                    }

                    // respond to the current message
                    if (Responder != null)
                    {
                        Responder.Respond(stream, data);
                    }
                }

                // close the client
                client.Close();
            }
            catch (SocketException exc)
            {
                Console.WriteLine("SocketException: {0}", exc);
            }
            finally
            {
                // stop the server after an before this method ends
                Server.Stop();
            }
        }