简体   繁体   English

在UWP中将数据从服务器发送到客户端

[英]Sending data from server to client in UWP

I am trying to create an UWP client and Server application. 我正在尝试创建UWP客户端和服务器应用程序。 Both client and server are hosted on Different machines. 客户端和服务器都托管在不同的计算机上。

Network connection with UWP Apps 与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. 任何代码或链接都非常感谢。

From the code you have mentioned, Modify ConnectSocket() method to read response from server. 在您提到的代码中,修改ConnectSocket()方法以读取服务器的响应。

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)
        }


    }

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. 由于您正在使用StreamSocket建立服务器与客户端之间的连接,因此有一个官方的Socket活动触发流套接字示例 ,其中包括客户端应用程序和服务器应用程序,并完美展示了如何使用Socket Activity Stream Socket API保持套接字连接有效超出应用程序的生命周期。

For sending data from server to client part, in the StreamSocketListenerServer (server) project: 要将数据从服务器发送到客户端,请在StreamSocketListenerServer (服务器)项目中:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM