尝试对无法访问的主机窗口CE进行套接字操作

时间:2013-04-28 22:52:51

标签: c# sendfile

我通过无线方式将文件从智能设备Windows CE发送到PC,文件通过TCP发送,当我通过我的笔记本电脑上的活动同步部署应用程序进行测试时,它可以正常工作。但是当应用程序安装在设备内部时,它没有发送文件,它向我显示异常:尝试对无法访问的主机执行套接字操作 代码在

之下
private void button1_Click(object sender, EventArgs e)
{
    string IPAddress = "10.1.1.102";
    string filPath = "\\student.XML";
    int PortNumber = 5656;

    SendTCP(filPath, IPAddress, PortNumber);            
}

public void SendTCP(string M, string IPA, Int32 PortN)
{
    byte[] SendingBuffer = null;
    TcpClient client = null;
    NetworkStream netstream = null;

    try
    {
        client = new TcpClient(IPA, PortN);
        MessageBox.Show("Connected to the Server...\n");

        netstream = client.GetStream();
        FileStream Fs = new FileStream(M, FileMode.Open, FileAccess.Read);
        int NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Fs.Length) / Convert.ToDouble(BufferSize)));

      //  progressBar1.Maximum = NoOfPackets;

        int TotalLength = (int)Fs.Length, CurrentPacketLength, counter = 0;
        for (int i = 0; i < NoOfPackets; i++)
        {
            if (TotalLength > BufferSize)
            {
                CurrentPacketLength = BufferSize;
                TotalLength = TotalLength - CurrentPacketLength;
            }
            else
                CurrentPacketLength = TotalLength;

            SendingBuffer = new byte[CurrentPacketLength];
            Fs.Read(SendingBuffer, 0, CurrentPacketLength);
            netstream.Write(SendingBuffer, 0, (int)SendingBuffer.Length);
        }

        // label1.Text = label1.Text + "Sent " + Fs.Length.ToString() + " bytes to the server";
        MessageBox.Show("Sent " + Fs.Length.ToString() + " bytes to the server");

        Fs.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        netstream.Close();
        client.Close();
    }
}

我不知道如何修复此错误有帮助

1 个答案:

答案 0 :(得分:0)

您确定设备已连接到网络且目标IP是否正确?

请注意,当通过ActiveSync / Windows Mobile Center连接时,会建立与设备的单独网络连接,并且设备会获得另一个(不同的)IP地址,通过该IP地址与主机通信。