0

I am trying to create an UWP client and Server application. Both client and server are hosted on Different machines.

Network connection with UWP Apps

Using this link i was able to connect to the server and can send string from client to server. This piece is absolutely fine.

But now question is how to send the data from server to client. I could not find any proper sample . Can some one throw a light on sending data from server to the client.

Any piece of code or link is Much appreciated.

Community
  • 1
  • 1
user3012974
  • 43
  • 2
  • 8
  • Is your client always connected to the server? – Suneesh Jul 12 '16 at 09:41
  • No. on Click i am connecting to the server. I am devloping proof of concept(POC) and need to extend this POC. My requirement is client and server hosted on the different machines. When server recive a data from the client , it immediately need to send back the response to the client. – user3012974 Jul 12 '16 at 09:49
  • 1
    That is simple, use an asynchronous socket to connect to the server, then send the data, do not disconnect, wait to read the data from the same socket. Example https://msdn.microsoft.com/en-us/library/windows/apps/hh202858%28v=vs.105%29.aspx – Suneesh Jul 12 '16 at 10:09

2 Answers2

0

From the code you have mentioned, Modify ConnectSocket() method to read response from server.

I have not run the code. modifications may be required

private async Task ConnectSocket()
    {
        StreamSocket socket = new StreamSocket();

        socket.Control.KeepAlive = false;

        HostName host = new HostName("localhost");

        try
        {
            await socket.ConnectAsync(host, "5463");

            Stream streamOut = socket.OutputStream.AsStreamForWrite();
            StreamWriter writer = new StreamWriter(streamOut);
            string request = "Test Self App \n";
            await writer.WriteLineAsync(request);
            await writer.FlushAsync();
            // Code for reading
            Stream streamIn = socket.OutputStream.AsStreamForRead();
            StreamReader reader = new StreamReader(streamIn);
            char[] result = new char[reader.BaseStream.Length];
            await reader.ReadAsync(result, 0, (int)reader.BaseStream.Length);
            // Your data will be in results
            socket.Dispose();
        }
        catch (Exception ex)
        {
            txb_Events.Text += ex.Message;
            //Logs.Add(ex.Message)
        }


    }
Suneesh
  • 469
  • 7
  • 21
0

But now question is how to send the data from server to client.

Since you are using StreamSocket to establish the connection between server and client, there is a official Socket activity trigger stream socket sample which include both client app and server app and perfectly shows how to use the Socket Activity Stream Socket API to keep a socket connection alive beyond the lifetime of the application.

For sending data from server to client part, in the StreamSocketListenerServer (server) project:

private async void SendButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        DataWriter writer = new DataWriter(connectedSocket.OutputStream);
        writer.WriteBytes(Encoding.UTF8.GetBytes(SendMessageTextBox.Text));
        SendMessageTextBox.Text = "";
        await writer.StoreAsync();
        writer.DetachStream();
        writer.Dispose();
    }
    catch(Exception exception)
    {
        rootPage.NotifyUser(exception.Message, NotifyType.ErrorMessage);
    }
}
Grace Feng
  • 16,564
  • 2
  • 22
  • 45