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";
}
//发送信息
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>
<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";
}
//发送信息
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>
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
{
public partial class Form1 : Form
{
private bool ServiceStartFlag = false;
private Socket m_Socket;
NetworkStream networkStream;
Thread m_Thread;
public delegate void HelpHandler(string str);
public Form1()
{
InitializeComponent();
btnStart.Enabled = true;
btnStop.Enabled = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
int Port = int.Parse(txtPort.Text);
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress LocalServerIP = GetIPAddress();
IPEndPoint LocalIPEndPoint = new IPEndPoint(LocalServerIP, Port);
m_Socket.Bind(LocalIPEndPoint);
m_Socket.Listen(600);
m_Thread = new Thread(new ThreadStart(AcceptClient));
m_Thread.IsBackground = true;
m_Thread.Start();
}
private void AcceptClient()
{
ServiceStartFlag = true;
while (ServiceStartFlag)
{
try
{
Socket newSocket = m_Socket.Accept();
string onemessge = "<cross-domain-policy><allow-access-from domain=\"" + "*" + "\" to-ports=\"" + txtPort.Text + "\"/></cross-domain-policy>\0";
byte[] tmpBytes = Encoding.UTF8.GetBytes(onemessge);
newSocket.Send(tmpBytes);
Thread newThread = new Thread(new ParameterizedThreadStart(ReadMsg));
newThread.IsBackground = true;
object obj = newSocket;
newThread.Start(obj);
}
catch (SocketException ex)
{
}
}
}
private void ReadMsg(object obj)
{
Socket socket = (Socket)obj;
HelpHandler rHandler = new HelpHandler(ReceivedMsg);
byte[] byteMessage = null; ;
while (ServiceStartFlag)
{
try
{
if (socket.Connected)
{
byteMessage = new byte[1000];
int len= socket.Receive(byteMessage);
if (len > 0)
{
string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime + ":" + "Message from:";
msg += socket.RemoteEndPoint.ToString() + Encoding.UTF8.GetString(byteMessage);
BeginInvoke(rHandler, new object[] { msg });
byteMessage = null;
byte[] tmpBytes = Encoding.UTF8.GetBytes("Sended Sucessed!\0");
socket.Send(tmpBytes);
}
}
}
catch (SocketException ex)
{
BeginInvoke(rHandler, new object[] { ex.ToString() });
}
}
}
private void ReceivedMsg(string str)
{
this.listBox1.Items.Add(str);
}
private void btnStop_Click(object sender, EventArgs e)
{
}
private IPAddress GetIPAddress()
{
IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());
return ieh.AddressList[0];
}
}
}
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
{
public partial class Form1 : Form
{
private bool ServiceStartFlag = false;
private Socket m_Socket;
NetworkStream networkStream;
Thread m_Thread;
public delegate void HelpHandler(string str);
public Form1()
{
InitializeComponent();
btnStart.Enabled = true;
btnStop.Enabled = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
int Port = int.Parse(txtPort.Text);
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress LocalServerIP = GetIPAddress();
IPEndPoint LocalIPEndPoint = new IPEndPoint(LocalServerIP, Port);
m_Socket.Bind(LocalIPEndPoint);
m_Socket.Listen(600);
m_Thread = new Thread(new ThreadStart(AcceptClient));
m_Thread.IsBackground = true;
m_Thread.Start();
}
private void AcceptClient()
{
ServiceStartFlag = true;
while (ServiceStartFlag)
{
try
{
Socket newSocket = m_Socket.Accept();
string onemessge = "<cross-domain-policy><allow-access-from domain=\"" + "*" + "\" to-ports=\"" + txtPort.Text + "\"/></cross-domain-policy>\0";
byte[] tmpBytes = Encoding.UTF8.GetBytes(onemessge);
newSocket.Send(tmpBytes);
Thread newThread = new Thread(new ParameterizedThreadStart(ReadMsg));
newThread.IsBackground = true;
object obj = newSocket;
newThread.Start(obj);
}
catch (SocketException ex)
{
}
}
}
private void ReadMsg(object obj)
{
Socket socket = (Socket)obj;
HelpHandler rHandler = new HelpHandler(ReceivedMsg);
byte[] byteMessage = null; ;
while (ServiceStartFlag)
{
try
{
if (socket.Connected)
{
byteMessage = new byte[1000];
int len= socket.Receive(byteMessage);
if (len > 0)
{
string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime + ":" + "Message from:";
msg += socket.RemoteEndPoint.ToString() + Encoding.UTF8.GetString(byteMessage);
BeginInvoke(rHandler, new object[] { msg });
byteMessage = null;
byte[] tmpBytes = Encoding.UTF8.GetBytes("Sended Sucessed!\0");
socket.Send(tmpBytes);
}
}
}
catch (SocketException ex)
{
BeginInvoke(rHandler, new object[] { ex.ToString() });
}
}
}
private void ReceivedMsg(string str)
{
this.listBox1.Items.Add(str);
}
private void btnStop_Click(object sender, EventArgs e)
{
}
private IPAddress GetIPAddress()
{
IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());
return ieh.AddressList[0];
}
}
}