首页 > 解决方案 > Windows iot core 发送数据到远程服务器 52.139.250.253 费用网络

问题描述

我编写了一个程序来收集数据并通过 4g 路由器将其传输到我的服务器。但是我发现windows IoT core从服务器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);
    }
}

标签: uwpwindowsiotwindows-iot-core-10

解决方案


在连接初始化之前,您是否注意到转储中的 DNS 查找。看看这是否用于将域解析为该 IP 地址。如果是这样,它看起来与 WNS(Windows 推送通知服务)有关:https ://msdn.microsoft.com/de-de/windows/desktop/mt187203 。


推荐阅读