做一个PDA socket通信的东西,如下 Server端:vs2005 C# windows application
一个按钮,起动监听,端口8000;一个listbox,显示客户端发来的字符client端:vs2005 C# windows CE device application
一个输Server IP地址的textbox;一个输message的textbox
一个Connet按钮,连接服务器;
IPAddress ipRemote = IPAddress.Parse(textBox1.Text);
IPEndPoint iep = new IPEndPoint(ipRemote, 8000);
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(iep);
nsStream = tcpClient.GetStream();
swWriter = new StreamWriter(nsStream); button1.Enabled = false;
button2.Enabled = true; tcpConnect = true;
statusBar1.Text = "Connected!";
到这里没有问题。
一个Send按钮,发送字符
try
{
swWriter.WriteLine(textBox2.Text);
swWriter.Flush();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
这个按钮这一步 swWriter.Flush(); 出错!
System.IO.IOException:Unalbe to write data to the transport connection.
-->System.Net.Sockets.SocketException: An existing connection was forcibly colsed by the remote host 请教是什么缘故?要怎么解决啊?调试时,用ActiveSync连接windows mobile Pocket PC Emulator成功。
在Pocket PC Emulator窗口中跑出我的Client界面,点击Send按钮出错。(在vs2003下,以上都没有问题,能跑通的。用不到ActiveSync)
一个按钮,起动监听,端口8000;一个listbox,显示客户端发来的字符client端:vs2005 C# windows CE device application
一个输Server IP地址的textbox;一个输message的textbox
一个Connet按钮,连接服务器;
IPAddress ipRemote = IPAddress.Parse(textBox1.Text);
IPEndPoint iep = new IPEndPoint(ipRemote, 8000);
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(iep);
nsStream = tcpClient.GetStream();
swWriter = new StreamWriter(nsStream); button1.Enabled = false;
button2.Enabled = true; tcpConnect = true;
statusBar1.Text = "Connected!";
到这里没有问题。
一个Send按钮,发送字符
try
{
swWriter.WriteLine(textBox2.Text);
swWriter.Flush();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
这个按钮这一步 swWriter.Flush(); 出错!
System.IO.IOException:Unalbe to write data to the transport connection.
-->System.Net.Sockets.SocketException: An existing connection was forcibly colsed by the remote host 请教是什么缘故?要怎么解决啊?调试时,用ActiveSync连接windows mobile Pocket PC Emulator成功。
在Pocket PC Emulator窗口中跑出我的Client界面,点击Send按钮出错。(在vs2003下,以上都没有问题,能跑通的。用不到ActiveSync)
int port = 8000;
private Thread thThreadRead;
private TcpListener tlTcpListen;
private bool blistener = true;
private NetworkStream nsStream;
private StreamReader srRead;
private TcpClient tcClient; private void button1_Click(object sender, EventArgs e)
{
thThreadRead = new Thread(new ThreadStart(Listen));
thThreadRead.Start();
button1.Enabled = false;
} public static IPAddress GetServerIP()
{
IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());
return ieh.AddressList[0];
} private void Listen()
{
try
{
IPAddress serverIp = GetServerIP();
IPEndPoint iep = new IPEndPoint(serverIp, port);
tlTcpListen = new TcpListener(iep);
tlTcpListen.Start();
SetText(serverIp.ToString() + ":" + port.ToString() + " is listening now!");
tcClient = tlTcpListen.AcceptTcpClient();
nsStream = tcClient.GetStream();
srRead = new StreamReader(nsStream);
SetText("Connected!"); while (blistener)
{
string sMessage = srRead.ReadLine();
if (sMessage == "STOP")
{
tlTcpListen.Stop();
nsStream.Close();
srRead.Close();
SetText("connection is closed!");
thThreadRead.Abort();
return;
} string sTime = DateTime.Now.ToShortTimeString();
listBox1.Items.Add(sTime + " " + sMessage); } }
catch (System.Security.SecurityException)
{
MessageBox.Show("listen is failure!", "Error");
} }
Client端主要如下:
private StreamWriter swWriter;
private NetworkStream nsStream;
private TcpClient tcpClient;
private bool tcpConnect = false; private void button1_Click(object sender, EventArgs e)
{
IPAddress ipRemote;
try
{
ipRemote = IPAddress.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("IP address is invalid", "Error");
return;
} IPHostEntry ipHost;
try
{
ipHost = Dns.Resolve(textBox1.Text);
}
catch
{
MessageBox.Show("The host is not running!", "Error");
return;
} string sHostName = ipHost.HostName;
try
{
IPEndPoint iep = new IPEndPoint(ipRemote, 8000);
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(iep);
nsStream = tcpClient.GetStream();
swWriter = new StreamWriter(nsStream); button1.Enabled = false;
button2.Enabled = true; tcpConnect = true;
statusBar1.Text = "Connected!";
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return;
} } private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text != "")
{
try
{
swWriter.WriteLine(textBox2.Text);
swWriter.Flush();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("The message is invalid!", "Error");
} }
------------------------------------------------------------------------
安装vs2005,
然后安装Virtual Machine Network Driver for Microsoft Device Emulator
http://www.microsoft.com/downloads/details.aspx?familyid=DC8332D6-565F-4A57-BE8C-1D4718D3AF65&displaylang=en
装好后,网上邻居的本地连接属性里钩上Virtual Machine Network Driver
就可以跑了,,不需要ActiveSync,Windows Mobile 5.0 Pocket PC SDK和Windows Mobile 5.0 Emulator Images for Pocket PC。调试时选Pocket PC 2003 SE Emulator,出现虚拟机画面的时候,[文件]-[构成],[网络]里会有Virtual Machine Network Driver,钩上。多谢!