How to send data between .NET 4.5 and .NET 4.0 with Sockets?

南笙酒味 提交于 2019-12-12 19:44:24

问题


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.



来源:https://stackoverflow.com/questions/10348745/how-to-send-data-between-net-4-5-and-net-4-0-with-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!