我已经构建了可以正常工作的UWP套接字服务器和UWP客户端。我正在尝试将字符串从Windows窗体传递到UWP服务器。是否可以使用与UWP客户端发送字符串相同的方式发送字符串?下面显示了UWP如何发送字符串以及Windows窗体发送字符串的方式。
//send from win form which results in out of memory error
private void buttonSendMessage_Click(object sender, EventArgs e)
{
try
{
string msg = richTextTxMessage.Text;
//// New code to send strings from windows form to uwp server
NetworkStream networkStream = new NetworkStream(m_clientSocket);
System.IO.StreamWriter streamWriter = new
System.IO.StreamWriter(networkStream);
streamWriter.WriteLine(msg);
streamWriter.Flush();
//Use the following code to send bytes
//byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);
//if (m_clientSocket != null)
//{
// m_clientSocket.Send(byData);
//}
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
//起作用的UWP发送按钮
private void btnSend_Click(object sender, RoutedEventArgs e)
{
_socket.Send(txtMessage.Text);
}
// UWP客户端发送的是:
public async void Send(string message)
{
//Envia o tamanho da string
_writer.WriteUInt32(_writer.MeasureString(message));
//Envia a string em si
_writer.WriteString(message);
try
{
//Faz o Envio da mensagem
await _writer.StoreAsync();
//Limpa para o proximo envio de mensagem
await _writer.FlushAsync();
}
catch (Exception ex)
{
if (OnError != null)
OnError(ex.Message);
}
}
这几乎可以正常工作,不会使服务器出错,但是接收到的数据永远不会在UWP服务器上执行
public async void Send(string message)
{
try
{
StreamWriter writer = new StreamWriter(richTextTxMessage.Text);
//Send the message
await writer.WriteAsync(message);
await writer.FlushAsync();
writer.Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
抛出异常:ServerSocket.winmd中的'System.OutOfMemoryException'