//创建 1个客户端套接字 和1个负责监听服务端请求的线程
public static Socket socketClient = null;
Thread threadClient = null;
int ConnectionResult = -2;
static Dictionary<string, ServiceState> dic_ip = new Dictionary<string, ServiceState>();
public FTitlePage1()
{
InitializeComponent();
ConnectionSocket();
}
/// <summary>
/// 开启socket连接
/// </summary>
public void ConnectionSocket()
{
try
{
Start();
threadClient = new Thread(MonitorSocker);
threadClient.IsBackground = true;
threadClient.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " " + ex.StackTrace);
}
}
/// <summary>
/// 连接服务端
/// </summary>
private void Connect(IAsyncResult ar)
{
try
{
ServiceState obj = new ServiceState();
Socket client = ar.AsyncState as Socket;
obj.serviceSocket = client;
//获取服务端信息
client.EndConnect(ar);
//添加到字典集合
if (dic_ip.ContainsKey("192.168.0.81:4001"))
{
dic_ip.Remove("192.168.0.81:4001");
}
dic_ip.Add(client.RemoteEndPoint.ToString(), obj);
//接收连接Socket数据
client.BeginReceive(obj.buffer, 0, ServiceState.bufsize, SocketFlags.None, new AsyncCallback(ReadCallback), obj);
ConnectionResult = 0;
}
catch (SocketException ex)
{
ConnectionResult = ex.ErrorCode;
Console.WriteLine(ex.Message + " " + ex.StackTrace);
}
}
/// <summary>
/// 数据接收
/// </summary>
private void ReadCallback(IAsyncResult ar)
{
ServiceState obj = ar.AsyncState as ServiceState;
Socket s_socket = obj.serviceSocket;
try
{
if (s_socket.Connected)
{
//接收数据处理
int bytes = s_socket.EndReceive(ar);
if (bytes != 99)
{
ShowErrorNotifier("数据接收不完整");
return;
}
byte[] data = obj.buffer;
}
}
catch (SocketException ex)
{
ConnectionResult = ex.ErrorCode;
Console.WriteLine(ex.Message + " " + ex.StackTrace);
}
}
/// <summary>
/// 发送数据
/// </summary>
private void Send(Socket s_socket, byte[] by)
{
try
{
//发送
s_socket.BeginSend(by, 0, by.Length, SocketFlags.None, asyncResult =>
{
try
{
//完成消息发送
int len = s_socket.EndSend(asyncResult);
}
catch (SocketException ex)
{
ConnectionResult = ex.ErrorCode;
Console.WriteLine(ex.Message + " " + ex.StackTrace);
}
}, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + " " + ex.StackTrace);
}
}
/// <summary>
/// 连接socket
/// </summary>
public void Start()
{
try
{
string ip = "192.168.0.81";
int port = int.Parse("4001");
var endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketClient.BeginConnect(endPoint, new AsyncCallback(Connect), socketClient);
}
catch (SocketException ex)
{
Console.WriteLine("error ex=" + ex.Message + " " + ex.StackTrace);
}
}
//监听Socket
void MonitorSocker()
{
while (true)
{
if (ConnectionResult != 0 && ConnectionResult != -2)//通过错误码判断
{
Start();
dic_ip = new Dictionary<string, ServiceState>();
}
Thread.Sleep(1000);
}
}
private void uiButton1_Click(object sender, EventArgs e)
{
byte[] data = new byte[8];
data[0] = 0x01;
data[1] = 0x04;
data[2] = 0x00;
data[3] = 0x00;
data[4] = 0x00;
data[5] = 0x2f;
data[6] = 0xd6;
data[7] = 0xb1;
Send(dic_ip["192.168.0.81:4001"].serviceSocket,data);
}
我在登陆后就在初始化那里进行连接了设备,然后点击按钮发送指令,第一次可以获取数据,第二次就不行了,不走ReadCallback方法了,这是什么原因?求大佬解惑