我编写了一个程序来收集数据,并通过4g路由器将其传输到我的服务器。但是我发现Windows IoT核心从服务器52.139.250.253发送和接收数据,它花费大量网络,我进行了大量搜索,找不到有用的信息,不知道会发生什么,如何禁用它?
这是我的代码,非常简单
public class TcpConnection:IConnection
{
Socket _TcpClient;
IPEndPoint _ServerIp;
string _IpOrUrl = string.Empty;
int _PortNO;
Boolean IsRecieve { get; set; } = false;
private event Action<byte[]> DataRecieved = null;
private int _TcpTimeout { set; get; } = 5000;
public TcpConnection(string ipOrUrl, int portNO)
{
_ServerIp = new IPEndPoint(IPAddress.Parse(ipOrUrl), portNO);
_TcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_TcpClient.SendTimeout = this._TcpTimeout;//等待测试
this._IpOrUrl = ipOrUrl;
this._PortNO = portNO;
this.ConnectAsync();
}
public async void ConnectAsync()
{
try
{
if (!_TcpClient.Connected)
{
await this._TcpClient.ConnectAsync(_ServerIp);
AsynRecive();
}
}
catch { }
}
private void AsynRecive()
{
byte[] data = new byte[2048];
_TcpClient.BeginReceive(data, 0, data.Length, SocketFlags.None, asyncResult =>
{
try
{
int length = this._TcpClient.EndReceive(asyncResult);
if (length > 0)
{
byte[] bytesBack = data.Take(length).ToArray();
try
{
DataRecieved.Invoke(bytesBack);
}
catch (Exception Ex)
{
LogHelper.Log(Ex.Message.ToString(), LogLevel.Debug);
}
}
AsynRecive();
}
catch (Exception)
{
}
}, null);
}
public Boolean Send(byte[] data)
{
try
{
if (!this.IsConnect())
{
if (this.Reset())
{
this._TcpClient.Send(data);
}
}
else
{
this._TcpClient.Send(data);
}
return true;
}
catch { return false; }
}
public void Close()
{
this._TcpClient.Close();
}
public void Connect()
{
try
{
if (!_TcpClient.Connected)
{
this._TcpClient.Connect(_ServerIp);
AsynRecive();
}
}
catch(Exception Ex)
{ }
}
public bool IsConnect()
{
return _TcpClient.Connected;
}
public void AddRecieveHandle(Action<Byte[]> dataEvent)
{
this.DataRecieved -= dataEvent;
this.DataRecieved += dataEvent;
//throw new NotImplementedException();
}
public Boolean Reset()
{
try
{
_TcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_TcpClient.SendTimeout = this._TcpTimeout;
this.Connect();
return true;
}
catch {
return false;
}
}
public Task<bool> SendAsync(byte[] data)
{
return Task.FromResult(true);
}
}
答案 0 :(得分:0)
您是否在初始化连接之前注意到转储中的DNS查找。看一下这是否用于将域解析为该IP地址。 如果确实如此,则似乎与WNS(Windows推送通知服务)有关:https://msdn.microsoft.com/de-de/windows/desktop/mt187203。