How to send data between .NET 4.5 and .NET 4.0 wit

2019-07-23 18:02发布

问题:

The problem is that my server is written in the old Framework, so I can not use SocketStream there, I use System.Net.Sockets.TcpClient instead. The Client is written in the new framework, where TcpClient and the whole System.Net.Sockets are not supported. In the new framework we have Windows.Networking.Sockets. The exact question is: How to send data from the Client to the Server?

Here is what happens when the user clicks on Send button:

var writer = new DataWriter(socket.OutputStream);
writer.WriteString(message);
var ret = await writer.StoreAsync();
writer.DetachStream();
LogMessage(string.Format("Sent (to {0}) {1}", socket.Information.RemoteHostName.DisplayName, message));

At server side:

srReceiver = new System.IO.StreamReader(tcpClient.GetStream());
strResponse = srReceiver.ReadLine();

回答1:

Sockets just allow a bidirectional stream of octets. This is independent of the API: Win32 APIs can interoperate with POSIX sockets quite happily (consider browsing content on Windows served from Apache running on Linux).

Anything more is up to the client and server to agree (message encoding etc.).

Ie. make the connection (with different APIs), then send data in the common format and it will be received.