wince6 TCP通信
计算机作为服务端,wince作为客户端tcp通信程序,用usb连接计算机同步的时候可以实现tcp通信,但是拔掉usb后tcp通信就失败了,用wince的tcp通信程序只是用网线连接计算机不用usb,然后通信就报错“试图对无法访问的计算机执行套接字操作”。这是什么原因,求解决。。。
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net; //TCP/IP
using System.Net.Sockets;
using System.IO; //流读写
using System.Threading;//多线程
using System.Text.RegularExpressions;//正则表达式
namespace SmartDeviceProject9
{
public partial class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 定义变量
/// </summary>
public Thread thread;
public TcpClient tcpClient;
public NetworkStream netStream;
public StreamWriter streamWriter;
public StreamReader streamReader;
/// <summary>
/// 构造函数
/// </summary>
public Form1()
{ //初始化界面
InitializeComponent();
}
/// <summary>
/// 连接服务器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{
Socket newclient=new Socket (AddressFamily.InterNetwork ,SocketType.Stream , ProtocolType.Tcp) ;
tcpClient = new TcpClient("192.168.1.31", 45671);
IPEndPoint ie = new IPEndPoint(IPAddress.Parse("192.168.1.31"),45671);
// newclient.Bind(ie);
//newclient.Listen(100);
newclient.Connect(ie);
netStream = tcpClient.GetStream();//取得网络流
streamWriter = new StreamWriter(netStream);//创建streamWriter,向流中写字符
streamReader = new StreamReader(netStream);//创建streamReader,向流中读字符
textBox1.Text = "连接成功";
thread = new Thread(new ThreadStart(recieveData));//创建接收线程
thread.Start();//启动接收线程,死循环接收数据
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
try
{
streamReader.Close();
streamWriter.Close();
netStream.Close();
tcpClient.Close();
textBox1.Text = "连接已经断开";
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
try
{
// string s = getData();//跨线程
// streamWriter.Write(s);//写入流
streamWriter.Write(textBox1 .Text );//写入流
streamWriter.Flush(); //刷新当前数据流中的数据
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
/// <summary>
/// 接收数据并处理
/// </summary>
#region
private void recieveData()
{
try
{
while (true)
{
// Receive the TcpServer.response.
byte[] data = new Byte[tcpClient.ReceiveBufferSize];// Buffer to store the response bytes.
// Receive the TcpServer.response.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
netStream.Read(data, 0, (int)tcpClient.ReceiveBufferSize);
responseData = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);//重编码
setData(responseData);//跨线程调用控件,处理数据
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
/// <summary>
/// 跨线程操作控件(设置)
/// </summary>
/// <param name="data"></param>
#region
private delegate void SET(string data);//要求签名相同
public void setData(string data)
{
if (textBox1.InvokeRequired) //检查控件的属性
{
SET ex = new SET(setData); //创建本方法的委托实例
textBox1.Invoke(ex, new string[] { data }); //调用控件的唤醒操作,传递相应参数
}
else
{
textBox1.Text = data; ; //控件操作
}
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 跨线程调用控件(获取)
/// </summary>
/// <returns></returns>
#region
private delegate string GET(); //要求签名相同
public string getData()
{
string data = "";
if (textBox1.InvokeRequired) //检查控件的属性
{
GET ex = new GET(getData); //创建本方法的委托实例
textBox1.Invoke(ex, new string[] { }); //调用控件的唤醒操作,传递相应参数
}
else
{
data = textBox1.Text; //操作控件
}
return (data);
}
#endregion
}
} WinCE TCP/IP
[解决办法]
[解决办法]