WinCE设备上用socket连接电脑出问题----现有连接被远程主机强行关闭
在桌面上运行一点问题也没有,但同样的代码在手持机设备上,就不能用了,总是出现 “现有连接被远程主机强行关闭”的提示,在try-catch中也不能捕捉到异常,但实现上Socket可能是没连上的。
代码如下:
namespace SmartDeviceProject1{ public partial class Form1 : Form { private static byte[] result = new byte[2048]; IPAddress ip; Socket socket; Thread myThread; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ip = IPAddress.Parse(this.comboBox1.Text); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.Connect(new IPEndPoint(ip, int.Parse(this.textBox1.Text))); myThread = new Thread(new ThreadStart(ReceiveData)); myThread.Priority = ThreadPriority.BelowNormal; myThread.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button2_Click(object sender, EventArgs e) { if (socket.Connected) { socket.Send(Encoding.UTF8.GetBytes(textBox2.Text)); textBox2.Text =string.Empty ; } } private void ReceiveData() { while (true) { int resultLength = socket.Receive(result); this.Invoke((EventHandler)delegate { this.textBox3.Text +=(DateTime.Now.ToLongTimeString() + Encoding.UTF8.GetString(result, 0, resultLength)); }); Application.DoEvents(); } } }}