private void button1_Click(object sender, EventArgs e)
{
if (Listen.Text == "Listen")
{ Listen.Text = "UnListen";
con = new conVal(); con.andon = new LinkAndon(); con.sendmes = new Queue<byte[]>(); con.buffer= new byte[1024]; mt = new Mutex(true, "mymt");
// Thread T2 = new Thread(SendMessage);
AsyncCallback ac = new AsyncCallback(AcceptData); Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Server.Bind(serverIP); Server.Listen(10); Server = Server.Accept(); //erver.Receive(data);
Server.BeginReceive(con.buffer, 0, con.buffer.Length, SocketFlags.None, ac, (object)con);
System.Threading.Timer tm = new System.Threading.Timer(tm_Elapsed, (object)con, 0, 5000); // T2.Start((object)con);
}
else
{ Server.Disconnect(false); Listen.Text = "Listen";
}
} class conVal
{
//public Socket con; public ANDON.LinkAndon andon; public Queue<byte[]> sendmes; public bool go=true; public byte[] buffer;
} private conVal con;
void tm_Elapsed(object or)
{ conVal cc = (conVal)or; Dictionary<string, string>[] par = cc.andon.MachineStatusWrite(); foreach (Dictionary<string, string> para in par)
{
string ex = "<Andon_alarm "; int i=1; string pa=""; if (para["DEVICE_ID"] == "1")
{
pa+="Bstate="+para["STATE"]+" ";
}else pa+="Lstate="+para["STATE"]+" "; i++;
ex += "tokens=" + i.ToString() + pa.ToString()+"comm='test'/>"; Command cd = new Command(9, para["DEVICE_ID"], ex); byte[] combyte = Encoding.ASCII.GetBytes(cd.CommandText); byte[] ct = new byte[] { 0x0, 0x0, 0x0, (byte)combyte.Length }; IEnumerable<byte> sou = ct.Concat(combyte); con.buffer=sou.ToArray(); Server.BeginSend(con.buffer,0,con.buffer.Length, SocketFlags.None, SendMessage, (object)con.buffer); // cc.sendmes.Enqueue(sou.ToArray()); cc.andon.updateEventState(para["ID"]);
}
} private ANDON.LinkAndon ad; private void SendMessage(IAsyncResult result)
{
try
{ //conVal con = (conVal)result.AsyncState; //while (con.go)
//{ // if (con.sendmes.Count() != 0)
// { // // mt.WaitOne(); // byte[] sou = con.buffer; // //Server.Send(sou, sou.Length, SocketFlags.None); // // mt.ReleaseMutex();
// } //}
}
catch (Exception e)
{
}
} private void AcceptData(IAsyncResult result)
{
// while (true)
// {
string txt; conVal con = (conVal)result.AsyncState; txt=Encoding.ASCII.GetString(con.buffer); Command cd = new Command(con.buffer); switch(cd.act)
{
case action.ANDON_ALARM: ad = new ANDON.LinkAndon(); Dictionary<string, string> par = new Dictionary<string, string>(); par.Add("EQUIP_ID", cd.equpstr); par.Add("EVENT_COMMENT", cd.tokens["Comm"]); par.Add("EVENT_TYPE", cd.tokens["type"]); par.Add("MSG_ID", cd.times); par.Add("STATE", cd.tokens["state"]); par.Add("TIST", cd.timestr); par.Add("USER_ID", cd.tokens["user"]); par.Add("type",cd.tokens["type"]);
ad.AlarmRecive(par);break;
} string ret = cd.FeedBackCommand(); byte[] combyte = Encoding.ASCII.GetBytes(ret); byte[] cx = new byte[] { 0x0, 0x0, 0x0, (byte)combyte.Length }; IEnumerable<byte> sou = cx.Concat(combyte); // mt.WaitOne(); // con.sendmes.Enqueue(sou.ToArray());
con.buffer = sou.ToArray();
// mt.ReleaseMutex();
//Server.Send(sou.ToArray(),sou.Count(),SocketFlags.None); Server.BeginSend(con.buffer, 0, con.buffer.Length, SocketFlags.None, SendMessage, (object)con.buffer); SetText(txt,1); SetText(ret, 0); // }
上面的程序几个方法执行完了 窗口就突然关闭了 求教阿 手足无措
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货