Flex Socket 与 C# 通信
Flex
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
/////////////////////////////////
//private member
private var xmlsocket:XMLSocket= new XMLSocket();
private var alert:Alert;
private var tmpStr:String;
/////////////////////////////////
//连接服务器
private function ConncetServer():void
{
xmlsocket.addEventListener(DataEvent.DATA,OnRecived);
xmlsocket.addEventListener(Event.CONNECT , onConnected);
this.xmlsocket.connect(this.txtIP.text,int(this.txtPort.text));
}
private function onConnected(evt:Event):void
{
xmlsocket.send(this.txtTrueName.text+" has connected");
this.txtContent.text=this.txtContent.text+"连接成功\n";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//发送信息
private function Send():void
{
this.xmlsocket.send(this.txtTrueName.text+"|"+this.txtSendContent.text+"\n");
}
//接收数据
private function OnRecived(event:DataEvent):void
{
trace("等待读取数据信息");
tmpStr = event.text;
//alert=Alert.show("收到新消息","消息");
this.txtContent.text=this.txtContent.text+"\n"+tmpStr;
}
]]>
</mx:Script>
<mx:Panel layout="absolute" right="0" left="0" top="0" bottom="0" horizontalAlign="left">
<mx:TextInput cornerRadius="20" themeColor="#0D79BE" borderColor="#070D04" borderStyle="inset" borderThickness="1" id="txtIP" text="192.168.1.55" left="88" top="23"/>
<mx:TextInput cornerRadius="20" themeColor="#0D79BE" borderColor="#070D04" borderStyle="inset" borderThickness="1" width="94" id="txtPort" text="9992" left="293" top="23"/>
<mx:TextInput cornerRadius="20" themeColor="#0D79BE" borderColor="#070D04" borderStyle="inset" borderThickness="1" width="94" id="txtTrueName" text="小虎" left="432" top="23"/>
<mx:Label text="服务器IP" fontSize="12" left="15" top="23"/>
<mx:Label text="姓名" fontSize="12" left="395" top="23"/>
<mx:Label text="端口" fontSize="12" left="256" top="23"/>
<mx:Button label="登录" fontSize="12" id="btnLogin" left="552" top="23" click="ConncetServer()"/>
<mx:Button label="发送" fontSize="12" id="btnSend" left="552" top="68" click="Send();" />
<mx:TextInput borderColor="#04010C" backgroundColor="#EEDBFC" width="516" borderStyle="inset" cornerRadius="20" alpha="1.0" id="txtSendContent" left="10" top="70"/>
<mx:TextArea borderColor="#010A10" id="txtContent" left="10" top="110" right="10" bottom="10" backgroundColor="#DEEEF3" cornerRadius="12"/>
</mx:Panel>
</mx:Application>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Server
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class Form1 : Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private bool ServiceStartFlag = false;
private Socket m_Socket;
NetworkStream networkStream;
Thread m_Thread;
public delegate void HelpHandler(string str);
public Form1()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
btnStart.Enabled = true;
btnStop.Enabled = false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void btnStart_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int Port = int.Parse(txtPort.Text);
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress LocalServerIP = GetIPAddress();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
IPEndPoint LocalIPEndPoint = new IPEndPoint(LocalServerIP, Port);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m_Socket.Bind(LocalIPEndPoint);
m_Socket.Listen(600);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m_Thread = new Thread(new ThreadStart(AcceptClient));
m_Thread.IsBackground = true;
m_Thread.Start();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
private void AcceptClient()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ServiceStartFlag = true;
while (ServiceStartFlag)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Socket newSocket = m_Socket.Accept();
string onemessge = "<cross-domain-policy><allow-access-from domain=\"" + "*" + "\" to-ports=\"" + txtPort.Text + "\"/></cross-domain-policy>\0";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte[] tmpBytes = Encoding.UTF8.GetBytes(onemessge);
newSocket.Send(tmpBytes);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Thread newThread = new Thread(new ParameterizedThreadStart(ReadMsg));
newThread.IsBackground = true;
object obj = newSocket;
newThread.Start(obj);
}
catch (SocketException ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
}
private void ReadMsg(object obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Socket socket = (Socket)obj;
HelpHandler rHandler = new HelpHandler(ReceivedMsg);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte[] byteMessage = null; ;
while (ServiceStartFlag)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (socket.Connected)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byteMessage = new byte[1000];
int len= socket.Receive(byteMessage);
if (len > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string sTime = DateTime.Now.ToShortTimeString();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string msg = sTime + ":" + "Message from:";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
msg += socket.RemoteEndPoint.ToString() + Encoding.UTF8.GetString(byteMessage);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
BeginInvoke(rHandler, new object[]
{ msg });
byteMessage = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte[] tmpBytes = Encoding.UTF8.GetBytes("Sended Sucessed!\0");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
socket.Send(tmpBytes);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
catch (SocketException ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
BeginInvoke(rHandler, new object[]
{ ex.ToString() });
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
}
private void ReceivedMsg(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.listBox1.Items.Add(str);
}
private void btnStop_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private IPAddress GetIPAddress()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return ieh.AddressList[0];
}
}
}
posted @
2009-07-14 17:00
天纯蓝
阅读(
749)
评论()
编辑
收藏
举报