本文介绍了如何发送的.NET 4.5和.NET 4.0套接字的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在的问题是,我的服务器是用旧的框架,所以我不能用SocketStream那里,我用System.Net.Sockets.TcpClient代替。客户端是写在新的框架,其中,不支持的TcpClient和整个的System.Net.Sockets。在新的框架,我们有Windows.Networking.Sockets。确切的问题是:如何从客户端将数据发送到服务器

下面是当用户点击发送按钮会发生什么:

  VAR作家=新DataWriter(socket.OutputStream);
writer.WriteString(消息);
VAR RET =等待writer.StoreAsync();
writer.DetachStream();
的LogMessage(的String.Format(已发送({0}){1},socket.Information.RemoteHostName.DisplayName,消息));
 

在服务器端:

  srReceiver =新就是System.IO.StreamReader(tcpClient.GetStream());
strResponse = srReceiver.ReadLine();
 

解决方案

套接字只允许8进制双向流。这是独立的API:Win32 API的可以与POSIX插座互操作很愉快(考虑对来自Apache的Windows服务在Linux上运行浏览内容)

什么更多的是由客户端和服务器同意(消息编码等)。

IE浏览器。进行连接(带有不同的API),那么发送数据的公共格式,这将被接收到。

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();
解决方案

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.

这篇关于如何发送的.NET 4.5和.NET 4.0套接字的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:56