加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flash与c# socket通信,win2003,webForm,winForm

发布时间:2020-12-15 06:53:57 所属栏目:百科 来源:网络整理
导读:Flash端代码: var mySocket: XML Socket = new XMLSocket(); mySocket.connect("localhost",13000); btnSend.onRelease = function() { mySocket.send(txtInput.text); trace("send"); }; mySocket.onConnect = function(myStatus) { trace(myStatus); if (
Flash端代码:
var mySocket:XMLSocket = new XMLSocket(); mySocket.connect("localhost",13000); btnSend.onRelease = function() { mySocket.send(txtInput.text); trace("send"); }; mySocket.onConnect = function(myStatus) { trace(myStatus); if (myStatus) { ? trace("连接13000成功!"); ? mySocket.send("Iloveyou!n"); ? trace("发送13000成功!"); } else { ? trace("连接13000失败!"); } }; mySocket.onData = function(msg:String) { trace(msg); txtInput.text = msg; }; ////////////////////////////////////////// var mySocketa:XMLSocket = new XMLSocket(); mySocketa.connect("localhost",13500); btn_send.onRelease = function() { mySocketa.send(txtInput.text); trace("send"); }; mySocketa.onConnect = function(myStatus) { trace(myStatus); if (myStatus) { ? trace("连接13500成功!"); ? mySocketa.send("Iloveyou!n"); ? trace("发送13500成功!"); } else { ? trace("连接13500失败!"); } }; mySocketa.onData = function(msg:String) { trace(msg); txtInput.text = msg; }; c#端代码: using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; namespace XMLsocket { ? ? class Program ? ? { ? ? ? ? static void Main(string[] args) ? ? ? ? { ? ? ? ? ? ? TcpListener server = null; ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? // Set the TcpListener on port 13000. ? ? ? ? ? ? ? ? Int32 port = 13500; ? ? ? ? ? ? ? ? IPAddress localAddr = IPAddress.Parse("127.0.0.1"); ? ? ? ? ? ? ? ? // TcpListener server = new TcpListener(port); ? ? ? ? ? ? ? ? server = new TcpListener(localAddr,port); ? ? ? ? ? ? ? ? // Start listening for client requests. ? ? ? ? ? ? ? ? server.Start(); ? ? ? ? ? ? ? ? // byte[] msg = System.Text.Encoding.Default.GetBytes("Game over!!!"); ? ? ? ? ? ? ? ? /// stream.Write(msg,msg.Length); ? ? ? ? ? ? ? ? // Enter the listening loop. ? ? ? ? ? ? ? ? while (true) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Console.Write("Waiting for a connection... "); ? ? ? ? ? ? ? ? ? ? TcpClient client = server.AcceptTcpClient(); ? ? ? ? ? ? ? ? ? ? // Perform a blocking call to accept requests. ? ? ? ? ? ? ? ? ? ? // You could also user server.AcceptSocket() here. ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("Connected!"); ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? NetworkStream stream = client.GetStream(); ? ? ? ? ? ? ? ? ? ? // Get a stream object for reading and writing ? ? ? ? ? ? ? ? ? ? stream = client.GetStream(); ? ? ? ? ? ? ? ? ? ? // Set the TcpListener on port 13000. ? ? ? ? ? ? ? ? ? ? // Buffer for reading data ? ? ? ? ? ? ? ? ? ? int i; ? ? ? ? ? ? ? ? ? ? Byte[] bytes = new Byte[256]; ? ? ? ? ? ? ? ? ? ? String data = null; ? ? ? ? ? ? ? ? ? ? while ((i = stream.Read(bytes,bytes.Length)) != 0) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //Console.WriteLine("*******"+i); ? ? ? ? ? ? ? ? ? ? ? ? // Translate data bytes to a ASCII string. ? ? ? ? ? ? ? ? ? ? ? ? data = System.Text.Encoding.Default.GetString(bytes,i); ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("Received: {0}",data); ? ? ? ? ? ? ? ? ? ? ? ? // Process the data sent by the client. ? ? ? ? ? ? ? ? ? ? ? ? data = data.ToUpper(); ? ? ? ? ? ? ? ? ? ? ? ? /////////////////////////////////////////////return back ? ? ? ? ? ? ? ? ? ? ? ? data = "C# say: " + data; ? ? ? ? ? ? ? ? ? ? ? ? byte[] msg = System.Text.Encoding.Default.GetBytes(data); ? ? ? ? ? ? ? ? ? ? ? ? /// Send back a response. ? ? ? ? ? ? ? ? ? ? ? ? stream.Write(msg,msg.Length); ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("Sent: {0}",data); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? // Shutdown and end connection ? ? ? ? ? ? ? ? ? ? // client.Close(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch (SocketException e) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("SocketException: {0}",e); ? ? ? ? ? ? } ? ? ? ? ? ? finally ? ? ? ? ? ? { ? ? ? ? ? ? ? ? // Stop listening for new clients. ? ? ? ? ? ? ? ? server.Stop(); ? ? ? ? ? ? } ? ? ? ? ? ? Console.WriteLine("nHit enter to continue..."); ? ? ? ? ? ? Console.Read(); ? ? ? ? } ? ? } } 另外,对于Form application,可以参考以下主要的代码: flashThread = new Thread(new ThreadStart(startFlashComm)); flashThread.Start(); ? ?? ? ? ? ? private void startFlashComm() ? ? ? ? { ? ? ? ? ? ? //MessageBox.Show("Player!!!!=" + player+"offset="+offset); ? ? ? ? ? ? IPAddress localAddr = IPAddress.Parse("127.0.0.1"); ? ? ? ? ? ? Int32 port = 13000 + (int)offset; ? ? ? ? ? ? listener = new TcpListener(localAddr,port); ? ? ? ? ? ? listener.Start(); ? ? ? ? ? ? Thread thread = new Thread(new ThreadStart(recieve)); ? ? ? ? ? ? thread.Start(); ? ? ? ? } ? ? ? ? private void recieve() ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? while (true) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? socket = listener.AcceptSocket(); ? ? ? ? ? ? ? ? ? ? socket.Blocking = false; ? ? ? ? ? ? ? ? ? ? log.Debug("listener.AcceptSocket();"); ? ? ? ? ? ? ? ? ? ? if (socket.Connected) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? socket.BeginReceive(readerBuffer,readerBuffer.Length,new System.AsyncCallback(DoRecevive),this); ? ? ? ? ? ? ? ? ? ? ? ? while (true) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (messageQueue.Count > 0) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String[] values = (String[])messageQueue.ToArray(typeof(String)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? string messageToSend = values[0]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? messageToSend += ""; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? log.Debug("messageToSend ::: " + messageToSend); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? messageQueue.RemoveAt(0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? byte[] byteMessage; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? byteMessage = Encoding.ASCII.GetBytes(messageToSend); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int i = socket.Send(byteMessage,byteMessage.Length,0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? log.Debug("socket.Send? end ----------------" + i); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? Thread.Sleep(100); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception e) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? log.Debug(e.Message + "2222211*********************n"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void AfterSend(IAsyncResult ar) ? ? ? ? { ? ? ? ? ? ? log.Debug("end send--------------------------- "); ? ? ? ? } ? ? ? ? private void DoRecevive(IAsyncResult ar) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? String data = Encoding.ASCII.GetString(readerBuffer); ? ? ? ? ? ? ? ? log.Debug("data read:::" + data); ? ? ? ? ? ? ? ? string[] cmd_Array = null; ? ? ? ? ? ? ? ? cmd_Array = data.Split(';'); ? ? ? ? ? ? ? ? log.Debug("-------------------" + cmd_Array[0]); ? ? ? ? ? ? ? // MessageBox.Show("Flash say:"+cmd_Array[0]); ? ? ? ? ? ? ? ? /*string btnState = ""; ? ? ? ? ? ? ? ? btnState ="bright"+ "-" + "pengpeng"; ? ? ? ? ? ? ? ? messageQueue.Add(btnState); ? ? ? ? ? ? ? MessageBox.Show(data);*/ ? ? ? ? ? ? ? ? switch (cmd_Array[0]) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? /////svcServer.BroadCastMessage(IC_Games.IGC_ServiceSockets.Protocol.IO_PROCOTOL.BUTTONPRESS,selectedAction + ";1;" + player); ? ? ? ? ? ? ? ? ? ? case "collect": ? ? ? ? ? ? ? ? ? ? ? // MessageBox.Show("collect"); ? ? ? ? ? ? ? ? ? ? ? ? svcServer.BroadCastMessage(IC_Games.IGC_ServiceSockets.Protocol.IO_PROCOTOL.BUTTONPRESS,1 + ";" + 8 + ";" + player); ? ? ? ? ? ? ? ? ? ? ? ? // Lamp(1,true); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "betone": ? ? ? ? ? ? ? ? ? ? ? //MessageBox.Show("betone"); ? ? ? ? ? ? ? ? ? ? ? ? svcServer.BroadCastMessage(IC_Games.IGC_ServiceSockets.Protocol.IO_PROCOTOL.BUTTONPRESS,10 + ";" + 10 + ";" + player); ? ? ? ? ? ? ? ? ? ? ? ? //? Lamp(10,true); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "betten": ? ? ? ? ? ? ? ? ? ? ? // MessageBox.Show("betten"); ? ? ? ? ? ? ? ? ? ? ? ? svcServer.BroadCastMessage(IC_Games.IGC_ServiceSockets.Protocol.IO_PROCOTOL.BUTTONPRESS,12 + ";" + 12 + ";" + player); ? ? ? ? ? ? ? ? ? ? ? ? // Lamp(12,true); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "antebet": ? ? ? ? ? ? ? ? ? ? ? // MessageBox.Show("antebet0"); ? ? ? ? ? ? ? ? ? ? ? ? svcServer.BroadCastMessage(IC_Games.IGC_ServiceSockets.Protocol.IO_PROCOTOL.BUTTONPRESS,2+ ";" + 3 + ";" + player); ? ? ? ? ? ? ? ? ? ? ? ? //svcServer.BroadCastMessage(IC_Games.IGC_ServiceSockets.Protocol.IO_PROCOTOL.BUTTONPRESS,3 + ";" + 4 + ";" + player); ? ? ? ? ? ? ? ? ? ? ? ? //Lamp(2,true); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "deal": ? ? ? ? ? ? ? ? ? ? ? ? //MessageBox.Show("deal"); ? ? ? ? ? ? ? ? ? ? ? ? svcServer.BroadCastMessage(IC_Games.IGC_ServiceSockets.Protocol.IO_PROCOTOL.BUTTONPRESS,3 + ";" + 4 + ";" + player); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "cancel": ? ? ? ? ? ? ? ? ? ? ? ? svcServer.BroadCastMessage(IC_Games.IGC_ServiceSockets.Protocol.IO_PROCOTOL.BUTTONPRESS,5 + ";" +6 + ";" +player); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "check": ? ? ? ? ? ? ? ? ? ? ? /// MessageBox.Show("check"); ? ? ? ? ? ? ? ? ? ? ? ? svcServer.BroadCastMessage(IC_Games.IGC_ServiceSockets.Protocol.IO_PROCOTOL.BUTTONPRESS,5 + ";" + 6 + ";" + player); ? ? ? ? ? ? ? ? ? ? ? ? // Lamp(5,true); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "bonus": ? ? ? ? ? ? ? ? ? ? ? ? svcServer.BroadCastMessage(IC_Games.IGC_ServiceSockets.Protocol.IO_PROCOTOL.BUTTONPRESS,6 + ";" + 7 + ";" + player); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception e) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? log.Debug(e.Message + "*********************n"); ? ? ? ? ? ? } ? ? ? ? ? ? socket.BeginReceive(readerBuffer,this); ? ? ? ? }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读