pda 与远程pc通信问题
小弟最近在做一个pda与pc 的通信问题,最终要解决的问题是将pda中一xml文件上传给远程pc服务器。
我现在做的测试就是用socket实现pda与pc间的简单通信,代码如下:
pda端:
TcpClient Client = new TcpClient();
Client.Connect(IPAddress.Parse("202.114.171.104"), 2005);
string senddata = textBox3.Text.Trim();
string recvdata = "";
byte[] buffer = new byte[1024];
buffer = Encoding.Default.GetBytes(senddata);
byte[] inbuff = new byte[1024];
Client.GetStream().Write(buffer, 0, buffer.Length);
while (Client.GetStream().DataAvailable == false)
Application.DoEvents();
if (Client.GetStream().DataAvailable == true)
{
Client.GetStream().Read(inbuff, 0, inbuff.Length);
MessageBox.Show("3");
recvdata = "服务程序已经成功收到指令,在" + Encoding.Default.GetString(inbuff, 0, inbuff.Length);
MessageBox.Show(recvdata);
}
Client.Close();
pc server端:
TcpListener Listener = new TcpListener(IPAddress.Parse("202.114.171.104"), 2005);
Listener.Start();
Console.WriteLine("正在 202.114.171.104:2005 处侦听.....");
while (true)
{
Socket s = Listener.AcceptSocket();
string data = "";
int byterecvd = 0;
int totalbytes = 0;
byte[] recvdata = new byte[1024];
do
{
byterecvd = s.Receive(recvdata, 0, 1024, SocketFlags.None);
if (byterecvd > 0)
{
data += Encoding.ASCII.GetString(recvdata);
totalbytes += byterecvd;
}
Console.WriteLine(data);
} while (byterecvd == 1024);
string response = "this is server";
Byte[] senddata = Encoding.ASCII.GetBytes(response.ToCharArray());
s.Send(senddata, senddata.Length, SocketFlags.None);
}
可是这样总是通信不成功,提示错误为:Unable to write data to the transport connection ,不用write用send也是不行的。
但是,我试过将pda端的程序复制重写为一般应用程序(不是智能设备应用程序),结果是可以通信的。难道是pda与pc通信有特别之处 ?希望高手能够帮小弟解答下,跪求……
[解决办法]
问题是PDA没有设置固定IP,你最好搞个路由器,让PC和PDA都连接它,然后设置一下IP,就OK了!