-1

I did a client server asynchro. the connection is open and the client send the server a string. but now i want to send him back something when the user click on buttonOK

I try to do a await task to send a message to my client from my server like this :

private async void buttonOk_ClickAsync(object sender, RoutedEventArgs e)
    {
    // Get all strokes on the InkCanvas.
    IReadOnlyList<InkStroke> currentStrokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
    Boolean flag = false;


    if (currentStrokes.Count > 0)
    {
        Windows.Storage.StorageFolder picturesLibrary = KnownFolders.PicturesLibrary;
        StorageFolder savedPicturesFolder = await picturesLibrary.CreateFolderAsync("New Pictures", CreationCollisionOption.OpenIfExists);
        StorageFile imageFile = await savedPicturesFolder.CreateFileAsync("NewImage.jpg", CreationCollisionOption.ReplaceExisting);

        CanvasDevice device = CanvasDevice.GetSharedDevice();
        CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96);

        using (var ds = renderTarget.CreateDrawingSession())
        {
            ds.Clear(Colors.White);
            ds.DrawInk(inkCanvas.InkPresenter.StrokeContainer.GetStrokes());
        }

        using (var fileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Jpeg, 1f);
        }

        flag = true;
    }

    if (flag)
    {
        var res = await senddata();
        flag = false;
    }

    inkCanvas.InkPresenter.StrokeContainer.Clear();
    GrdLogo.Visibility = Visibility.Visible;
    GrdApp.Visibility = Visibility.Collapsed;

}

private async Task<string> senddata()
  {
  Stream streamOut = socket.OutputStream.AsStreamForWrite();
  StreamWriter writer = new StreamWriter(streamOut);
  string request = "Hello client";
  await writer.WriteLineAsync(request);
  await writer.FlushAsync();
  return null;
  }

but my server dont send and hangs on. Could someone explain me why ? thanks for your help

djillius
  • 23
  • 8

2 Answers2

0

@Xavier Xie : here is my server side code

string response;
using(Stream inputStream = streamSocket.InputStream.AsStreamForRead())    
{
   using(StreamReader streamReader = new StreamReader(inputStream))
   {
      response = await streamReader.ReadLineAsync();
   }
}
this.txtBlk_Events.Text += response;

and i become this message : "an existing connection had to be closed by the remote host"

djillius
  • 23
  • 8
  • If this is your service side code, I did not see any code that will send data to your client side. It just read data from socket inputStream. – Xie Steven Jul 19 '18 at 07:05
0

Ok, I find my solution. The mistake i did, i forget to put the client in listening mode. After doing that, my server can send message to my client too. This post help me : Network connection with UWP Apps

djillius
  • 23
  • 8