Windows8 StreamSocket Server

        private StreamSocket socket;
        private bool connected = false;
        private async void StartListener_Click(object sender, RoutedEventArgs e)
        {
            StreamSocketListener listener = new StreamSocketListener();
            listener.ConnectionReceived += OnConnection;
            
            // Start listen operation.
            try
            {
                await listener.BindServiceNameAsync(this.ServicePortForListener.Text);
                this.showTextBlock.Text = "Start Listen";
            }
            catch (Exception exception)
            {
                if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                    throw;
            }
        }


        private async void OnConnection(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
        {
            connected = true;
            socket = args.Socket;
            DataReader reader = new DataReader(args.Socket.InputStream);
            try
            {
                while (true)
                {
                    // Read first 4 bytes (length of the subsequent string).
                    uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
                    if (sizeFieldCount != sizeof(uint))
                        return;
                    uint stringLength = reader.ReadUInt32();

                    // Read the string.
                    uint actualStringLength = await reader.LoadAsync(stringLength);
                    if (stringLength != actualStringLength)
                        return;
                    string recMessage = reader.ReadString(actualStringLength);

                    Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, 
                            () =>
                            {
                                this.reMsgTextBlock.Text = recMessage;
                            }
                        );
                }
            }
            catch (Exception exception)
            {
                if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                    throw;
            }
        }


        private async void SendMessage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string sendData = this.sendMsgTextBlock.Text;
                if (connected == true)
                {
                    DataWriter writer = new DataWriter(socket.OutputStream);

                    uint msgLength = writer.MeasureString(sendData);
                    writer.WriteUInt32(msgLength);
                    writer.WriteString(sendData);

                    await writer.StoreAsync();
                    await writer.FlushAsync();
                    writer.DetachStream();
                    writer.Dispose();
                }
            }catch (Exception ex){ }
        }

 

posted on 2013-03-28 09:06  JackSlaterYu  阅读(241)  评论(0编辑  收藏  举报