我正在通过无线从智能设备 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();
}
}
我不知道如何解决这个错误请帮助