通过TCP套接字从Windows窗体应用程序发送数据到通用应用程序无法正常工

问题描述:

我将Windows Universal Application作为TCP服务器。所以我想通过TCP套接字发送数据。

I have Windows Universal Application as TCP server. So I want to send data over TCP socket.

如果我从Windows Universal Application作为客户端发送数据,它可以工作。但是我想从Windows Forms Application发送数据而我不能使用StreamSocket,所以我尝试使用客户端发送数据,如下所示:https://msdn.microsoft.com/en-us/library/kb5kfec7(v = vs.110).aspx
并且它不起作用。因此,UWP和经典Windows窗体之间的通信是否存在任何限制,因为我尝试了许多不同的选项来使其工作。因此,只有这样才能让它发挥作用,就像我在UWP双方所说的那样。我检查了功能
(互联网(客户端),互联网(客户端和服务器),专用网络(客户端和服务器))。

It works, if I send data from Windows Universal Application as client. But I want to send data from Windows Forms Application and I can't use StreamSocket, so I tried to send data with client as its shown here: https://msdn.microsoft.com/en-us/library/kb5kfec7(v=vs.110).aspx and it doesn't working. So are there any limitations on comunication between UWP and classic windows forms, because I've tried many diferent options to make it working. So only way to make it work was like I said with UWP on both sides. I did check capabilities (Internet(Client), Internet(Client & Server), Private Networks(Client & Server)).

这是TCP服务器的代码:

Here is code of TCP server:

private readonly int _port;
public int Port { get { return _port; } }

private StreamSocketListener listener;
private DataWriter _writer;

public delegate void DataRecived(string data);
public event DataRecived OnDataRecived;

public delegate void Error(string message);
public event Error OnError;

public SocketServer(int port)
{
	_port = port;
}

public async void Star()
{
	try
	{
		if (listener != null)
		{
			await listener.CancelIOAsync();
			listener.Dispose();
			listener = null;
		}
		
		listener = new StreamSocketListener();
		
		listener.ConnectionReceived += Listener_ConnectionReceived;
		await listener.BindServiceNameAsync(Port.ToString());
	}
	catch (Exception e)
	{
		if (OnError != null)
			OnError(e.Message);
	}
}

private async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
	var reader = new DataReader(args.Socket.InputStream);
	_writer = new DataWriter(args.Socket.OutputStream);
	try
	{
		while (true)
		{
			uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
			if (sizeFieldCount != sizeof(uint))
				return;                    
			uint stringLenght = reader.ReadUInt32();
			uint actualStringLength = await reader.LoadAsync(stringLenght);
			if (stringLenght != actualStringLength)
				return;
			if (OnDataRecived != null)
			{              
				string data = reader.ReadString(actualStringLength);
				OnDataRecived(data);
			}
		}

	}
	catch (Exception ex)
	{
		if (OnError != null)
			OnError(ex.Message);
	}
}

public async void Send(string message)
{
	if (_writer != null)
	{
		_writer.WriteUInt32(_writer.MeasureString(message));
		_writer.WriteString(message);

		try
		{
			await _writer.StoreAsync();
			await _writer.FlushAsync();
		}
		catch (Exception ex)
		{
			if (OnError != null)
				OnError(ex.Message);
		}
	}
}

有没有人有任何建议?

感谢您的帮助。

您好,cc $ b $
[2016年3月30日更新]欢迎来到开发通用Windows应用论坛!

请阅读粘贴帖子,尤其是
发布指南:主题行标签

Windows 10 SDK和工具的已知问题


Hi cepon,

[Updated 3/30/2016]Welcome to the Developing Universal Windows apps forum!
Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools

看好你的描述,似乎是当您的UWP应用程序用作服务器并且Windows窗体应用程序用作客户端时,套接字不起作用。我想知道UWP应用程序和Windows窗体应用程序是否在同一台机器上,当我们使用UWP应用程序作为环回
服务器与客户端在同一台机器上进行进程间通信时,应用程序无法接收任何消息。请参阅以下链接。
$


"注意 环回仅允许用于开发目的。不允许在Visual Studio外部安装Windows运行时应用程序。此外,Windows运行时应用程序只能将IP环回用作客户端网络请求的目标地址。
因此,使用DatagramSocket或StreamSocketListener监听IP环回地址的Windows运行时应用程序无法接收任何传入数据包。"

https://msdn.microsoft.com/en-us/library/windows/apps/hh780593?f=255& MSPPError = -2147217396



最好的问候,

David

Besed on your description, it seems that the socket does not work when your UWP app works as the Server and the Windows Form app works as the client. I wonder if the UWP app and the Windows Form app are in the same machine, when we use the UWP app as a loopback server for interprocess communication on the same machine as the Client, the app is prevented from receiving any messages. Please refer to the following link.

"Note  Loopback is permitted only for development purposes. Usage by a Windows Runtime app installed outside of Visual Studio is not permitted. Further, a Windows Runtime app can use an IP loopback only as the target address for a client network request. So a Windows Runtime app that uses a DatagramSocket or StreamSocketListener to listen on an IP loopback address is prevented from receiving any incoming packets."
https://msdn.microsoft.com/en-us/library/windows/apps/hh780593?f=255&MSPPError=-2147217396

Best Regards,
David